1

I keep getting a syntax error (unexpected end of file). The syntax looks good to me but something is obviously wrong here. I was hoping someone from the outside might be able to provide from insight.

read -p "Create default hosts file?: " yn

       case $yn in
        [Yy] )  echo "Creating default hosts file"
            sleep 1
            cat <<-EOF1 > /etc/hosts
               Do not remove the following line, or various programs that  require network functionality will fail.
               127.0.0.1    localhost.localdomain localhost

               192.168.1.1      test01
               192.168.1.2      test02
               192.168.1.3      test03
               EOF1
                if [ "$(wc -m /etc/hosts)" == "215 /etc/hosts" ] ; then
               echo -e "Default Hosts file\e[1;32m COMPLETE \e[0m"     
            else
               echo -e "Default hosts file\e[1;31m FAILED \e[0m"
               sleep 1
               echo "Please correct before continuing"
               echo "EXITING..."
               sleep 1
               exit
            fi;;
        [Nn] )  echo 'Searching "/etc/hosts" for test03 entry'
            sleep 1
            grep "test03" /etc/hosts >/dev/null
            if [ "$?" -eq 0 ] ; then
               echo "Entry found!"
               echo "Setup Continuing..."
            else
               echo '"test03" entry not found'
               sleep 1
               echo "Please correct before continuing"
               sleep 1
               echo "EXITING..."
               sleep 1
               exit
            fi;;
         *   )  echo "Please answer [Yy] or [Nn].";;
       esac

1 Answer 1

3

The end-of here document marker should be the first thing on a line except for tab characters:

cat <<-EOF1 > /etc/hosts
           Do not remove the following line, or various programs that  require network functionality will fail.
           127.0.0.1    localhost.localdomain localhost

           192.168.1.1      test01
           192.168.1.2      test02
           192.168.1.3      test03
EOF1
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't have to be the first thing -- it can also have literal tab characters (NOT SPACES) before it, being <<- rather than <<; that said, given how hard it is to distinguish between kinds of whitespace, this is indeed not a best practice to rely on.
Thank you! The EOF1 ending the end-of here document had a few spaces after the tab. I tried to line it up with the top marker...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.