OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Missing Quotes

If we edit our script and remove the trailing quote from the argument following the first

echo command:


#!/bin/bash


# trouble: script to demonstrate common errors number=1

if [ $number = 1 ]; then

echo "Number is equal to 1.

else

echo "Number is not equal to 1."

fi

#!/bin/bash


# trouble: script to demonstrate common errors number=1

if [ $number = 1 ]; then

echo "Number is equal to 1.

else

echo "Number is not equal to 1."

fi


Watch what happens:



[me@linuxbox ~]$ trouble

/home/me/bin/trouble: line 10: unexpected EOF while looking for matching `"'

/home/me/bin/trouble: line 13: syntax error: unexpected end of file

[me@linuxbox ~]$ trouble

/home/me/bin/trouble: line 10: unexpected EOF while looking for matching `"'

/home/me/bin/trouble: line 13: syntax error: unexpected end of file


It generates two errors. Interestingly, the line numbers reported by the error messages are not where the missing quote was removed, but rather much later in the program. If we follow the program after the missing quote, we can see why. bash will continue looking for the closing quote until it finds one, which it does, immediately after the second echo command. After that, bash becomes very confused. The syntax of the subsequent if command is broken because the fi statement is now inside a quoted (but open) string.

In long scripts, this kind of error can be quite hard to find. Using an editor with syntax highlighting will help since, in most cases, it will display quoted strings in a distinctive manner from other kinds of shell syntax. If a complete version of vim is installed, syntax highlighting can be enabled by entering the command:



:syntax on

:syntax on


Top OS Cloud Computing at OnWorks: