Syntax error: "elif" unexpected (expecting "then")

Viewed 55

I have shell script which i am trying to run. This is the first time I am working on shellscript so it might be a silly mistake, please understand.

Below is my script

    // some commands 
    f1 = $? 
    // some commands
    f2 = $?
    if [ $f1 -eq 0 ] && [ $f2 -eq 0 ] ; then
        //do something

    elif [ $f1 -eq 0 ] || [ $f2 -ne 0 ] ; then
        //do something
    
    else
        echo "operation has failed..!"
    fi

I want to know two things

how shall I pass all those parameters via Ubuntu for windows How shall I fix the above error I am getting in if elif

2 Answers

Not sure if this going to help, but here are alternatives to dos2unix

tr

tr -d '\r' < script.sh > newscript.sh

awk

awk '1' RS='(\r\n?|\n)' ORS='\n' script.sh > newscript.sh

There is no problem in your elif usage, as far as I can see. But there are some missing parts in your code, so it is hard to know. Try to create a minimal example of your problem, and post it, without '//' lines.

One problem for example may come from your f1 = $? lines. There should be no spaces here (else it is interpreted as f1 command to be executed with arguments = and whatever value has $?)

Unless you do have a f1 command, that should have leaded to a "f1, command not found error", tho, not something about elif. But if you do have a f1 command, then your if [ $f1 -eq 0 ] are executed while f1 has no value. And since bash works by a series of substitution. that command then means if [ -eq 0 ] which leads to a syntax error.

Also, another thing we cannot verify from your partial code, is that all //do something lines must really do something. An empty line would also lead to a syntax error, since the following is incorrect

if [ condition ] ; then
elif [ anothercondition ] ; then
   doSomething
fi

(This example would complain about elif being unexpected, but not expecting then) If //do something is nothing, then use : for that (: is doing nothing)

But, well, I am speculating here. For what you gave, the only visible errors are the two fx = $? lines. And the // do something obviously, if they are literal (// is not a comment in bash), but I suppose they are not.

Oh, and unimportant side note: $10 is ok. And so is ${10}, and $9 and ${9}, hence the "unimportantness" of that remark. But there is no need to use {} specifically for two digits position parameter, as it seems your are doing :-).

Related