Operating System - HP-UX
1752777 Members
6098 Online
108789 Solutions
New Discussion юеВ

help shell script if..else fi

 
Jairo Campana
Trusted Contributor

help shell script if..else fi

hello I have a problem
when make set -x obtaing a error line 20
'[' ']'
./filtra.ori: : No such file or directory
echo "$TEST is null"
.....
.....
if [ $? -ne 0 ]
then
echo "Hello"
TEST=`grep ou file`
if [ $TEST <> "" ] #line 20
echo "$TEST is null"
else
echo "action.."
fi
else
echo "hello bad"
fi


legionx
6 REPLIES 6
Patrick Wallek
Honored Contributor

Re: help shell script if..else fi

What is:

if [ $TEST <> "" ]

supposed to do? That is not a valid comparison. Do want the 'then' action to when TEST is null or when TEST is NOT NULL?

You are also missing a 'then' after that if statement.
Rick Garland
Honored Contributor

Re: help shell script if..else fi

if [ $TEST != "" ]
then

else

fi

BTW, what is the <> you have? Is this "not equal"? I am familar with the '!=' and the '-ne'. Haven't seen the '<>' but this could be lack of exposure on my part...
Kent Ostby
Honored Contributor

Re: help shell script if..else fi

Suspect that you need != instead of <> since its specifically complaining about the TEST line . See 'man sh' for what you can use here.

Also make sure that the you have spaces next to the [ and ] .

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Stuart Browne
Honored Contributor

Re: help shell script if..else fi

As you are using [ ] evaluation, you can also try using the '-z' switch.

if [ ! -z "$TEST" ]
then

See 'man test' for different options to single-[]'s, as against the possible shell internal ([[ ]], as in posix and korn shells) evaluation/testing.
One long-haired git at your service...
Bill Hassell
Honored Contributor

Re: help shell script if..else fi

You need to flag variables that contain strings by using double quotes. The reason is that if $TEST is null, the shell replaces $TEST with nothing and your test becomes:

if [ <> "" ]

which makes no sense. Enclose $TEST with quotes and the statement will look like this:

if [ "" <> "" ]

But as mentioned, <> is not a valid shell expression. The 3 comparison operators are = < . so to get "not equal", use the ! as negation. So not-equal is: != and your test should be:

if [ "$TEST" != "" ]
or
if [ -z "$TEST" ]


Bill Hassell, sysadmin
Jairo Campana
Trusted Contributor

Re: help shell script if..else fi

in solaris <> is : if $TEST is diferent ""

Thank, I solved my problem:
...
if [ x$TEST == "" ]
then
echo "$TEST is null"
...

********if is true*************
ouput , set -x
TEST=`grep ou file`
++ TEST=
++ '[' x == x ']'
++ echo ' is null'
is null

*********if is FALSE************

++ TEST=*oufileover.ou
++ '[' 'x*oufileover.ou' == x ']'
++ echo 'action'
action
legionx