Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

With the following shell script, why I am getting errors

syntax error near unexpected token `else'

Shell Script

echo "please enter username"
read user_name
echo "please enter password"
read -s pass
echo ${ORACLE_SID}
SID=${ORACLE_SID}
if ["${ORACLE_SID}" != 'Test'] then
sqlplus -s -l $USER_NAME/$PASS@$SID <<EOF
copy from scott/tiger@orcl insert EMP using select * from EMP
exit
EOF
else
echo "Cannot copy"
fi
share|improve this question
    
paste.ubuntu.com/6013802 –  Rahul Patil Aug 22 '13 at 12:04
    
you may want to edit the line "copy from ...." as it may be currently showing something you don't want to show. (However, I do hope those are already modified infos, as they would be really poor security wise) –  Olivier Dulac Aug 22 '13 at 16:24
    
@OlivierDulac If you are referring to username and password in that line then those are known to all Oracle database users. It is common and well known since the beginning of Oracle database. –  Polppan Aug 23 '13 at 6:36
    
@Polppan : oh, thanks for that info ^^ –  Olivier Dulac Aug 23 '13 at 7:22
    
@OlivierDulac You are welcome, some info about this dba-oracle.com/t_scott_tiger.htm –  Polppan Aug 23 '13 at 8:06

2 Answers 2

up vote 4 down vote accepted

You have to terminate the condition of if like this:

if [ "${ORACLE_SID}" != 'Test' ]; then

or like this:

if [ "${ORACLE_SID}" != 'Test' ]
then

Note: you also have to put spaces after [ and before ].

The reason for the ; or linebreak is that the condition part of the if statement is just a command. Any command of any length to be precise. The shell executes that command, examines the exit status of the command, and then decides whether to execute the then part or the else part.

Because the command can be of any length there needs to be a marker to mark the end of the condition part, that is the then keyword, or token, or syntax. For the shell to recognize the then as a token it needs to standing in a line on its own or be separated by a ;.

The reason for the spaces after [ is because [ is also not syntax. It is a simple command. Usually a builtin of the shell. The shell executes the command [ with the rest as parameters, including the ] as mandatory last parameter. If you do not put a space after [ the shell will try to execute [whatever as command and fail.

The reason for space before the ] is similar. Because otherwise it will not be recognized as a parameter of its own.

share|improve this answer
    
That was spot on, however now I am getting test.sh: line 6: [: missing ]'` –  Polppan Aug 22 '13 at 12:00
    
Thanks, it helped. –  Polppan Aug 22 '13 at 12:10
    
@lesmana. A good way to provide a wrong answer first, then keep editing before someone else provides the correct answer. Please try to provide the correct answer the first time. –  val0x00ff Aug 22 '13 at 12:14
    
I do not consider my first answer as wrong. In fact, it was "spot on". It just did not solve all the problems in the question. –  lesmana Aug 22 '13 at 12:39
    
if is syntax, it isn't an ordinary command. It's a reserved word. Unlike many other programming languages, the shell doesn't recognize reserved words everywhere, only when they're the first word of a command (with a few subtleties). –  Gilles Aug 22 '13 at 22:02

When writing #sh you'd want

if [ "$ORACLE_SID" != "Test" ];
then
 ...
fi

When writing #bash

   if [[ "$ORACLE_SID" != "Test" ]];
    then
     .....
   fi

Mind the spaces please. There must be a space between [[ and first operator.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.