0

I'm in need to pass arguments to a shell script, which after validation, need to be passed on to a function in the script either as same or after modification.

#!/bin/sh
func1(){
echo "called func1 with $1" ;
exit 0 ;
}

func2(){
echo "called func2 with $1" ;
exit 0 ;
}

if [ $# -eq 0 ]
then
    echo "Usage:" ;
    echo "script.sh arg1 arg2" ;
    exit -1 ;
fi

if [ "$1" -eq "txt1" ]
then
    func1 $2 ;
    exit 0 ;
fi

if ["$1" -eq "txt2" ]
then
    func2 $2 ;
    exit 0;
fi

The response I get is

sh script.sh txt1
sh: txt1: bad number
sh: txt1: bad number
Ananth
  • 21
  • 8

1 Answers1

1

You are using the wrong operator for string comparison. You want to use =, not -eq (which is comparing integers).

Note that [ is just an internal command of the shell, so you need to separate that with whitespace from its arguments. (At the third test in your script.)

holgero
  • 2,640
  • 1
  • 13
  • 17
  • 1
    Might also mention the need for whitespace (missing in the second invocation). – Charles Duffy Dec 21 '15 at 20:18
  • Thanks for the quick help. I'm new to both STCK OVFL and linux shell. Also, I thought the response would help some of the newbies like me who search for the response rather than a question list? – Ananth Dec 22 '15 at 21:28