Search code examples
shellcsh

Cannot get if statement to work


Cannot figure out how to get csh if statements to work using expect.

This line of code works

if(4 > 3) echo "4 is greater than 3."

$ 4 is greater than 3.

This line of code does not work

if (4 > 3) $myvar = 5

0: Command not found (i.e. myvar = 0)

Neither does this one

if (4 > 3) then $myvar = 5

IF: Improper then

What am I doing wrong? I preferably need all this on one line or someway to get this into the send command for an expect script. Do I use \ characters if I can't get it on one line?


Solution

  • You seem to be confusing csh syntax with Bourne shell syntax.

    You assign variables with set name = value. $name = value doesn't work (actually, that also doesn't work in Bourne shell, where it's name=value).

    You only need the then for multi-line ifs. Leave it out for single-line ifs.

    Putting it all together:

    if (4 > 3) set myvar = 5
    

    or:

    if (4 > 3) then
        set myvar = 5
    endif