jens
jens

Reputation: 17635

Simple Shell Script does not append to file

The following script does not work:

#!/bin/sh
FILE="/root/.bashrc"
if [ -f $FILE ]
 then

 COMMAND="alias ls='ls -la --color=always --human'"

 if grep -q "$COMMAND" $FILE
 then
  echo "NOT CHANGED, Already existing: $COMMAND in $FILE"
 else
  $FILE << $COMMAND
  echo "CHANGED: $COMMAND in $FILE"
 fi

else
  echo "$FILE does not exist, will not apply changes: $COMMAND"
fi

I get this Error: 32: Syntax error: end of file unexpected (expecting "fi")

I would be very thankful for advice. I am a complete shell newbie, I did some tests but do not understand why this error comes.

Thanks!! Jens

Upvotes: 2

Views: 796

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799490

The syntax on line 12 does not append, it starts a heredoc.

echo "$COMMAND" >> "$FILE"

Upvotes: 3

Related Questions