IF is not working in csh script

cat tmp0.txt
700000

#!/bin/csh -fx
set id=`cat tmp0.txt`
echo $id
if ("$id" == "700000") then
   echo "Good Morning"
endif
if ("$id" == "700002") then
         echo "Good evening"
 endif

My output from terminal

set id=`cat tmp0.txt`
cat tmp0.txt
echo 700000 
700000 
 == 700000 ) then
 == 700002 ) then
 

Seems like the IF is not working. Is there anything wrong with my variable?

The parentheses need spaces

if ( "$id" == "700000" ) then
   echo "Good Morning"
endif
if ( "$id" == "700002" )  then
         echo "Good evening"
 endif

each ( needs a space before and after, so does each ) character.

Hi jim, the code still won't work.

Try:

#!/bin/csh -fx
set id=`dos2unix tmp0.txt|head -1`
echo $id
if ("$id" == "700000") then
   echo "Good Morning"
endif
if ("$id" == "700002") then
         echo "Good evening"
endif
1 Like

Following up on rdrtx1's post:
when you do this:

vi tmp0.txt

Do you see ^M at the end of each line?

I opened tmp0.txt with different kind of editor, i.e vi and nedit, even with notepad and I don't see ^M . Just a number 700000

---------- Post updated at 11:41 AM ---------- Previous update was at 11:40 AM ----------

Same result, it doens't do what it suppose to do.

can you post the output of the below command

 
od -c tmp0.txt

The output from terminal:

0000000   7   0   0   0   0   0      \r  \n
0000011

It's a dos formatted file, remove the cr with:

tr -d '\r' < dosfile > unixfile
1 Like

Reason your code did not work:

700000\r is not equal to 700000. DOS files mess up a lot of things in UNIX.
Learn about the dos2unix command. All windows based editors "like" \r and do not display it.

1 Like