Home > Blockchain >  C Shell: "if: Expression Syntax"
C Shell: "if: Expression Syntax"

Time:09-28

I have to write a C Shell script for work, and I have no idea why my if statement is apparently malformed. I have something like this:

    #!/bin/csh -f
    
    foreach line("`cat file.txt`")
    
        set curr_line = `echo $line | awk '{print substr($0, 1, 20)}'`
    
        if ($curr_line == "string of text...") then
            echo "Success!"
        endif
     end

CodePudding user response:

Same question asked in another post: if: Expression Syntax - in C Shell script

You need to wrap the commands in ' to use the result in the condition.

So this is what it should be:

if (`$curr_line == "string of text..."`) then
    echo "Success!"
endif

CodePudding user response:

Figured it out, I just needed quotes around the string variable:

if ("$curr_line" == "string of text...") then
            echo "Success!"
        endif
  • Related