简体   繁体   中英

C Shell: "if: Expression Syntax"

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

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

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

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

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