“Here Document” in Bash-Script made stupid problems

In a bash-script I wrote I had a stupid problem with the “<<” (here document) operator. And this is only because I am too structured :-) I wanted to write an ftp-call inside a for-loop, something like that:

#!/bin/sh
for filename in `awk '{print $1}' /my/directory/myfilewithfilenames.dat`
do
    ftp -n <<EOFTP
        open my.ftp.host
        user myusername mypwd
        put $filename
    EOFTP
done

Do you see the problem? Well, it is simply, that the end of “here document”, the “EOFTP” must not be indented. It must look like this instead.

#!/bin/sh
for filename in `awk '{print $1}' /my/directory/myfilewithfilenames.dat`
do
    ftp -n <<EOFTP
        open my.ftp.host
        user myusername mypwd
        put $filename
EOFTP
done

This cost me half a day. And it is so ugly, insn’t it? I think I don’t like shell-scripting at all. I will return to python, or maybe perl, whereever I can. Grmpf.

~ by runtimeexception on December 2, 2008.

2 Responses to ““Here Document” in Bash-Script made stupid problems”

  1. Haha, thats good to know! Supposedly it is just one of many things, which make bash scripting difficult; small details, which are of importance in archaic programming languages like Fortran also make a big difference in shell scripts. Yes, I guess line indentation is of importance also at other places in shell scripts. Or consider at least the space which must not be there in shell scripts after and before the “=” when setting a variable…
    Btw, in “Here documents” indentation is also of importance in Perl: http://perldoc.perl.org/perlop.html#Quote-Like-Operators

  2. Thanks for your reply! Confusing, that your name is Oliver: I thought something like: did I write this myself? is there another perysonality of mine? but then … I don’t think so.

    And yes, you are soo right. In my earliest days of scripting I had exactly the problem with the “=”. And yeah, seems like perl is no alternative here if I just want to get rid of indentation problems.

    Well, maybe I will have a new job in a month, then we’ll see what it brings. Hopefully sunshine and evenings in outdoor areas of some bars with a nice beer and stuff. You never know … perhaps some new languages.

Leave a Reply