5

I got bash script, but when I run it I got some error:

line 54: warning: here-document at line 42 delimited by end-of-file (wanted `EOF')

#!/bin/bash
echo "PODAJ IMIE"
read imie
echo  "PODAJ NAZWISKO"
read nazwisko

alias="$imie$nazwisko"
echo "$alias"


while [ 1 ]
do

        i=1

        if [ `egrep $alias /etc/passwd |wc -l` -gt 0 ]; then i=0
        fi

        if [ $i -eq 0  ]; then
                echo "Uzytkownik o podanym aliasie już istnieje!"
                echo "PODAJ INNY ALIAS np. "$alias"1"
                read alias

        else
                echo "PODAJ HASLO"
                read -s password
                echo "PODAJ NUMER POKOJU"
                read pokoj

                #adduser --disabled-password --gecos "$imie $nazwisko,$pokoj, ," $alias
                adduser --quiet --disabled-password -shell /bin/bash --home /home/$alias --gecos "$imie $nazwisko, $pokoj, , " $alias
                echo "$alias:$password" | chpasswd
                #echo -e "$haslo\n$haslo\n" | sudo passwd $alias


                dir=/home/$alias
                mkdir $dir/public_html
                mkdir $dir/samba
                cd $dir/public_html
                mkdir private_html

                cat <<EOF >>$dir/.bashrc

                echo "alias ps =/bin/ps"
                echo "alias ls =/bin/ls"
                t=\`date +%F.%H:%M:%S\`
                echo "WITAJ\n"
                echo "DZISIAJ MAMY:\$t"
                echo "TWOJE OSTATNIE LOGOWANIE:" && " lastlog -u \$USER | tail -n 1 | awk '{print       \$4" "\$5" "\$6" "\$7" "\$9}'"
                EOF

                break
        fi
done
Mat
  • 202,337
  • 40
  • 393
  • 406
Budrys
  • 98
  • 1
  • 1
  • 10
  • 3
    You hardly needed to post 40 lines of unrelated code before the problem, but since you did, I'll comment on your cargo-cult use of `[`. Basically every occurrence of `[` in your script is superfluous. `while true; do` and `if ! grep -Eq "$alias" /etc/passwd; then` – tripleee Nov 29 '14 at 10:19
  • What's the usefulness of those aliases? `ps` and `ls` should already run those commands; is your `PATH` broken? – tripleee Nov 29 '14 at 11:20

3 Answers3

10

The EOF needs to be at the start of a line, and with nothing after (including no whitespace).

Some of the echos in that block are suspicions. The text up to the end marker will be copied into the file as-is after variable substitution, not executed - and it doesn't look like you want substitution here.

Try this: (notice the quotes around "EOF" on the cat line)

            cat <<"EOF" >>$dir/.bashrc
alias ps=/bin/ps
alias ls=/bin/ls
t=$(date +%F.%H:%M:%S)
echo "WITAJ\n"
echo "DZISIAJ MAMY: $t"
echo "TWOJE OSTATNIE LOGOWANIE:" $(lastlog -u $USER | tail -n 1 | awk '{print       $4" "$5" "$6" "$7" "$9}')
EOF
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    "and with nothing after (including no whitespace)." I had started going mad because I was sure I had inserted a TAB before my EOF. Finally this helped to get unstuck and move on! – nass Aug 02 '15 at 16:43
8

The EOF delimiter needs to be aligned in the leftmost column.

Here's what I often do for alignment.

Lots
    Of
        Indents
            cat <<-____________HERE
                boo
____________HERE
        done
    done
done
tripleee
  • 175,061
  • 34
  • 275
  • 318
6

You can also use

     <Tab><Tab>cat <<-EOF >>$dir/.bashrc
#--------------------^- added a "-" char
     <Tab><Tab>  echo " ... "
     <Tab><Tab>  echo " ... "
     <Tab><Tab>EOF

where EOF is now preceded with a - character. This indicates to the shell that the closing Here-file token will be (can be) indented with tab characters. If you write, by accident, <Tab><Tab><Space>EOF it will not match, but 0,1,2,...,100 <Tab>EOF will match.

IHTH

mklement0
  • 382,024
  • 64
  • 607
  • 775
shellter
  • 36,525
  • 7
  • 83
  • 90