The shell script, I am trying to create, should be run by crontab regularly, and it should call a perl script which saves email attachments to a predefined directory.
Here's what I came up with:
backup_day=`date +%Y-%m-%d`
backup_dir=/private-backup/email_attachment_cleaner/korrespondenztest/$backup_day
if [ ! -d $backup_dir ]
then
mkdir -p $backup_dir
fi
if [ ! -d $backup_dir ]
then
echo "Error"
exit 1
fi
output_file=/root/email_attachment_cleaner/history.txt
perl /root/email_attachment_cleaner/email_attachment_cleaner.pl -O $backup_dir -lots-of-further-parameters >> $output_file
When I save this script in /root/email_attachment_cleaner/
and run it with sudo sh /root/email_attachment_cleaner/email_attachment_cleaner.sh
, I get the following output:
: not found_attachment_cleaner/email_attachment_cleaner.sh: 2: /root/email_attachment_cleaner/email_attachment_cleaner.sh:
: not found_attachment_cleaner/email_attachment_cleaner.sh: 4: /root/email_attachment_cleaner/email_attachment_cleaner.sh:
/root/email_attachment_cleaner/email_attachment_cleaner.sh: 19: /root/email_attachment_cleaner/email_attachment_cleaner.sh: Syntax error: end of file unexpected (expecting "then")
It appears to me that the given line numbers are probably "wrong" and should be 1, 3 and 18 instead. However, this output does not help me at all.
What's going on here?
I needed quite some time to figure out that empty lines in the script should contain at least a space. Is that right?
sudo sh /root/email_attachment_cleaner/email_attachment_cleaner.sh 2>&1 | cat -v
do you see any^M
characters? – Mark Plotnick Jun 17 '15 at 14:36^M
(carriage-returns) in the file. Get rid of those usingsed -i 's/\r$//' /root/email_attachment_cleaner/email_attachment_cleaner.sh
.Otherwise, there are no serious problems with the script. Christopher's advice is sound, but strictly speaking, not needed.
– Otheus Jun 17 '15 at 16:46