LeoPardus
LeoPardus

Reputation: 143

Executing perl script inside bash script

I inherited a long bash script that I recently needed to modify. The bash script is run as a cronjob on a daily basis. I am decent with bash scripting, but I do not know much about Perl.

I had to substitute all "rm" commands with a call to a perl script that does something similar (for security purposes). This script was not written by me, so there is no -f flag to skip the confirmation prompt. Therefore, to automate this script I pipe "yes" to the script.

Here is an example where I am sequentially deleting two directories:

echo REMOVING FILES TO SAVE DISK SPACE
echo "yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir1>"
yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir1>
echo "yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir2>"
yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir2>
echo DONE.

In my output file, I see the following:

REMOVING FILES TO SAVE DISK SPACE
yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir1>
yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir2>
DONE.

It does not appear that the perl script has run. Yet when I copy and paste those two commands into the terminal, they both run fine.

Any help is appreciated. Thank you in advance.

Upvotes: 0

Views: 4253

Answers (2)

LeoPardus
LeoPardus

Reputation: 143

Thanks for all the comments. I ended up changing the group and permissions of the tool and all output files. This allowed me to run the perl script without using "sudo," which others pointed out is bad practice.

Upvotes: 0

Sunny Patel
Sunny Patel

Reputation: 545

You simply put do

yes | ./myscript.pl

Upvotes: 1

Related Questions