futureweb futureweb - 3 years ago 133
Bash Question

cpanel shell script on cloud server centos no such file or dir

I have a shell script copy_files.sh which I call once a day using a cron job.

However it has never worked I keep getting no such file or directory.

!#/bin/sh
for f in /home/site1/public_html/admin/data/*.csv
do
cp -v "$f" /home/site2/app/cron_jobs/data/"${f%.csv}".csv
done


I have checked via ssh that all paths are correct I have varified the path to /bin/sh using
ls -l /bin/sh
I have set perms and user and group to root for copy_files.sh I have disabled php open_basedir protection.

the shell script is in /home/site2/

Any ideas why I am still getting no such file or directory?
Is there anyway to check open_basedir protection is off that said considering the script is owned by root I don't see that being the problem unless it's executed as site2 user and not root?

Answer Source

Because of the way you use shell expansion, the variable in your for loop contains the absolute path to your files. Having the absolute path, means there is no need to use string manipulation (%) nor do you need to add the ".csv" to the filename, just get rid of it all together and provide the directory to which you're copying as your second argument to cp, see the example below.

#!/bin/sh for f in /home/site1/public_html/admin/data/*.csv; do cp -v "$f" /home/site2/app/cron_jobs/data done

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download