3

I am new to shell scripting and created a script to check arguments are passed or not using if else condition . But it is always giving a error 'syntax error near unexpected token `fi' '

is it always required to use ';' after if condition brackets.
I runned it online on http://www.compileonline.com/execute_bash_online.php it is working well but not on my system(Centos 6.2). Am i missing something in env settings or is it something else.

Code

#!/bin/bash
echo "print a message"
if [ $# -eq 1 ]; then
    echo "Argument are passed to shell script"
else
    echo "No arguments are passed to shell script"
fi

Error message

[root@local shell_scripts]# sh test.sh 12 13
test.sh: line 7: syntax error near unexpected token `fi'
test.sh: line 7: `fi'
[root@local shell_scripts]#

my env details

[root@local shell_scripts]# env
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
SSH_CLIENT=192.168.1.17 49656 22
PERL5LIB=/home/bharat/perl5/lib/perl5/x86_64-linux-thread-multi:/home/bharat/perl5/lib/perl5
PERL_MB_OPT=--install_base /home/bharat/perl5
SSH_TTY=/dev/pts/6
USER=bharat
PATH=/home/bharat/perl5/bin:/usr/kerberos/sbin:/home/bharat/perl5/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/bharat/bin
MAIL=/var/spool/mail/bharat
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HOME=/root
SHLVL=4
PERL_LOCAL_LIB_ROOT=/home/bharat/perl5
LOGNAME=bharat
CVS_RSH=ssh
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
PERL_MM_OPT=INSTALL_BASE=/home/bharat/perl5
_=/bin/env
[root@local shell_scripts]#
Bharat Pahalwani
  • 1,404
  • 3
  • 25
  • 40
  • Try adding `;` before `fi` (am on windows now, so cannot test myself). – Axel Sep 16 '13 at 07:55
  • Can you show the exact error you are getting ? – Ashish Gaur Sep 16 '13 at 08:03
  • Are you sure that you added `; then` in the `if` statement in your script? – devnull Sep 16 '13 at 08:04
  • Code can be indented with four spaces. It is not necessary to use HTML for this. –  Sep 16 '13 at 08:23
  • you may want to run this script with `bash`, not `sh`. Though it will unlikely solve your problem. – sshilovsky Sep 16 '13 at 08:24
  • 1
    @Bharat Was your script created on Windows? If so, it probably contains DOS line endings, which can confuse the `bash` parser. Remove them with `dos2unix` or a similar tool. – chepner Sep 16 '13 at 13:16
  • 1
    @chepher yes i created my script on Window7 using notepad++ when i opened my script in vi editor on server i found that it was having character at the beginning of file. After removing it things went well. Thanks http://stackoverflow.com/questions/7256049/notepad-converting-ansi-encoded-file-to-utf-8 – Bharat Pahalwani Sep 18 '13 at 06:54

2 Answers2

3

Answering the first of your questions: ; is not required after if condition. Syntactically, then should go to a separate line. If then is in the 1st line, you use ;. The following code is correct:

if [ $# -eq 1 ]
then
    echo ok
fi

Also, these brackets are not actually if condition brackets. If condition is any valid bash command, and brackets are equivalent to test, see man test for ref.

The following are valid if statement beginnings:

if cp a.txt b.txt ; then ...
if test $# -eq 1 ; then ...

The second one is equivalent to your if statement.

sshilovsky
  • 958
  • 2
  • 9
  • 22
1

Works for me:

$ cat > f.sh
#!/bin/bash
echo "print a message"
if [ $# -eq 1 ]; then
    echo "Argument are passed to shell script"
else
    echo "No arguments are passed to shell script"
fi
$ bash f.sh 
print a message
No arguments are passed to shell script