Wednesday, December 10, 2008

Careful with HERE doc in shell scripts

We have a shell script scheduled as a cron job to do certain backup activities. As part of backup it uploads the data to another server via ftp and we used HERE doc for managing ftp session.

Being a nice Java developer, we formatted the script nicely forgetting the fact that for HERE doc to recognize the end of stream the token should start on a line itself; not even spaces are allowed !!

Here is an example to make this clear:
01 if [ $data = 0 ]
02 then
03   echo "Moving to backup server"
04   ftp -i -v -n myServer <<EOF
05   user $username $password
06   put $file.zip
07   bye
08   EOF
09 fi

Note how EOF is aligned on line 08. This way the stream will never end and will result in an error. Below is how it should be aligned
01 if [ $data = 0 ]
02 then
03   echo "Moving to backup server"
04   ftp -i -v -n myServer <<EOF
05   user $username $password
06   put $file.zip
07   bye
08 EOF
09 fi

No comments:

Post a Comment