简体   繁体   中英

utterly confused regarding bash script command line arguments

I have the following bash script file callee.sh which is being called from another script file caller.sh.

The callee.sh is as follows:

if [ $1 -eq  1 ];
then    
    echo  inside $1
    source ~/MYPROGRAMSRC/standAloneWordCount.sh $2
    #echo "inside standalone branch"
    #echo $1

elif [  $1 -eq  2  ];
then
    #echo "inside distributed branch"
    #echo $1

else
    echo invalid option for first argument-\n Options:\n "distributed"\n or\n "standalone"\n 


fi  

As most people might be able to tell, this is a script I use to decide whether to run hadoop in distributed or standAlone mode depending on the arguments.

This script is called from caller.sh as follows

source callee.sh $2 $counterGlobal

where $2 is a number either 1 or 2 and $counterGlobal is some integer.

My problem is that the if condition in callee.sh never evaluates to True and hence my script standAloneWordCount.sh which I call from within callee.sh is never called. I am running with bash shell and have tried many variants of the if statement like:

if [ $(($1 == 1 )) ]  -- (1)

In an echo statement just above the line -- (1) , the expression $(($1 == 1)) evaluates to 1 so I am baffled as to why I am unable to satisfy the if condition.

Also I keep getting the error where it says:

syntax error near unexpected token `else'

if anyone could help me out with these two errors, it would be much appreciated. As I've run out of ideas.

Thanks in advance!

have tried many variants of the if statement like:

if [ $(($1 == 1 )) ]

You should instead be saying:

if (($1 == 1)); then
  ...
fi

Regarding the Syntax error near unexpected token else'`, it's not because of any code that you've shown above. It seems to originate from some other portion of your script.

If you're using bash, try using double square brackets:

if [[ $1 -eq 1 ]]; then
    echo "inside 1"
fi

As for the syntax error , you need quotes around your text (which also means escaping the existing quotes or use single quotes):

echo -e "invalid option for first argument-\n Options:\n \"distributed\"\n or\n \"standalone\"\n"

The -e flag is there to let bash know you want the \\n to evaluate to a newline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM