Difference between words and not between numbers

Hi,

Sorry in advance for propably a silly question, but I am a bit lost.

On some of the linux job flow I have the following check:

if ($file != 1500) then
    echo ERROR

It works ok, all times $file is not equal to 1500 I have the error message.

I try to do something similar with words

if ($file != test) then
    echo ERROR

But the message I have is:

if: Expression Syntax.

How can I do the same kind of check with words?

Here is a good thread explaing the difference and pointing also to the Advanced Bash Scripting Guide. Check for Jim Mcnamara's answers:

So you might want to change it to:

$> if [ "$file" != "test" ]; then echo ERROR; fi
ERROR

# or

$> if [[ $file != test ]]; then echo ERROR; fi
ERROR

Round brackets are for arithmetic expressions. If you want to compare strings, use square brackets.

You should use "test" instead of test
... since test is a shell command !
choose another string that is not a shell command (ex : "mytest") to avoid any confusion ...

[[ "$file" != "mytest" ]] && echo ERROR

Indeed test is an unlucky example - put it into quotes as ctsgnb said to be on the safe side.

Hi,

Thank you for helping me. The "test" example was just that.... An example. So that is not a problem, it was just to simplify.

The full thing:

1) extracting a number from a file and checking if it is different from what is supposed to be

set thenumber = `grep "something" $file | awk '{print $9}'`
if ($thenumber != 1500) then
    echo ERROR

The above works ok

2) same as before, but in this case the thing to check is a word (or even more than one, but now I am testing with 1 word only). It does not seem working even with square brackets

set theword = `grep "somethingelse" $file | awk '{print $6}'`
if [$theword != NOT] then
echo ERROR

At stage two I have

if: Expression Syntax.

I tried to quote "NOT", no difference

---------- Post updated at 04:09 AM ---------- Previous update was at 04:05 AM ----------

Sorry, I've seen the problem (I think)

I had

=! for !=

---------- Post updated at 04:33 AM ---------- Previous update was at 04:09 AM ----------

It works but in the other way round

grep "somethingelse" $file | awk '{print $6}'

is NOT

so when I do

if [NOT != NOT] then
echo ERROR

I expect to do not see the error message, but I have it.

For some reason the word "NOT" extracted with grep is different from the second "NOT"?

I tried anyway to type manually also the first "NOT" (skipping the grep part, and I still have the error message: what is different between the 2 "NOT"?

if [NOT != NOT] then
echo ERROR

What Shell are you using?

echo $SHELL
1 Like

/bin/tcsh

No replies so far are applicable to "/bin/tcsh" . Some people on this board assume that you have a Posix Shell unless stated otherwise and the rest seem to assume that all Shells are interchangeable (which they are not). Always best to say what you have at the outset.

People using "csh" and its variants are rare because it is virtually never used for Systems Administration scripts.

Probably best to start a new thread but mention "/bin/tcsh" in the thread title and on the first line of the post.