Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EOT in conditional section of bash script

Tags:

bash

ftp

So, i'm trying to get an ftp script working but i'm hitting a snag. Here's the script:

#!/bin/bash
HOST='192.168.178.122'
USER='ftpuser'
PASSWD='passa.2015'
DATE=`date +%d-%m-%Y`
FILE="archive-"$DATE".tar.gz"

prep=0
echo "File is="$FILE
echo "Prepare_val="$prep
if [ $prep -eq 0 ]
    then
        find Web -maxdepth 1 -mindepth 1 -not -type l -print0 | tar --null --files-from - -cpzvf $FILE

        ftp -n $HOST << EOT
        user $USER $PASSWD
        put $FILE
        quit
        bye
        EOT
fi

When i try and run this script, it returns the following error:

ftp-script.sh: 22: ftp-script.sh: Syntax error: end of file unexpected (expecting "fi")

If i remove the EOT section, it executes fine, but the EOT is the only means by which the ftp commands can be run without needing user intervention. Does anyone know how to place an EOT in a conditional without causing the error I get.

like image 991
anark10n Avatar asked Dec 14 '22 07:12

anark10n


1 Answers

You can persist your indentation to have better readability like this:

contents of script.bash:

#!/bin/bash
#normal usage
cat <<EOF
abcd
xyz
EOF
echo "*************************"
#using heredoc without script indentation
if [[ true ]]; then
    cat <<EOF
abcd
xyz
EOF
fi
echo "*************************"
#using heredoc with script indentation
if [[ true ]]; then
    cat <<-EOF
    abcd
    xyz
    EOF
fi

Output:

$ ./script.bash 
abcd
xyz
*************************
abcd
xyz
*************************
abcd
xyz
$

Bottom line: use <<-EOT instead of <<EOT (Note the hyphen) to persist your indentation

like image 57
riteshtch Avatar answered Dec 27 '22 23:12

riteshtch