2

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?

Anthon
  • 79,293
Jan
  • 121
  • it gets worse... – mikeserv Jun 17 '15 at 13:49
  • 1
    which means exactly? – Jan Jun 17 '15 at 13:50
  • Nothing meaningful - I thought your question was titled funny. – mikeserv Jun 17 '15 at 13:50
  • If you do 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
  • "I needed quite some time to figure out that empty lines in the script should contain at least a space. Is that right?" No, not at all. The problem, as Mark notes, is that when you transferred the file from Windows, you kept all the ^M (carriage-returns) in the file. Get rid of those using sed -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
  • Otheus is correct by pointing out Windows' CR as the original problem. I am sorry that I did not figure this out, but with the output's error messages, I was obviously and unfortunately not able to look for the correct keywords. How should I proceed with this duplicate question? Edit? Delete? – Jan Jun 18 '15 at 15:30

0 Answers0