Operating System - HP-UX
1819899 Members
2560 Online
109607 Solutions
New Discussion юеВ

Re: "if" statement usage in unix

 
SOLVED
Go to solution
Paul Middleton
Frequent Advisor

"if" statement usage in unix

This is the most embarassing question I've ever asked. Is there a link to how to use the "if" statement in a script, or what the options like -en or -eq mean?
.
Is it used the same as in Fortran? (The last Fortran class I took was in 1972.)
.
Now that I've figured out how I give maximum points for the good suggestions. (I've been "prompted enough.)
.
Regards,
Paul Middleton
Lost, Lonely and Confused in Cleveland.
Think I'll visit my grandchildren tonight.
Dilligad - Do I Look Like I Give A Damn
15 REPLIES 15
Patrick Wallek
Honored Contributor

Re: "if" statement usage in unix

Do a

# man sh

or 'man whatever_shell_you_desire' and there is lots of information there. I would also advise you get a book on shell programming. I've got "UNIX shells by example" and I use it quite a bit.
Tony Contratto
Respected Contributor
Solution

Re: "if" statement usage in unix

Hi Paul,

The information you seek is in the man page for the shell you are using.

One of these will probably get the info you want:
man sh-posix
man ksh

--
Tony
got root?
Patrick Wallek
Honored Contributor

Re: "if" statement usage in unix

Oops...I just checked the 'man sh' page. That ain't what you want.

What you really want for the posix shell is:

# man sh-posix

For the ksh:

# man ksh

For the bourne shell

# man sh-bourne

For the csh:

# man csh
Dave La Mar
Honored Contributor

Re: "if" statement usage in unix

Paul -
A very good book for shell programming basics can be found at Barnes and Noble book stores or a subsidiary.
Look for UNIX Shell Programming
by Stephen G. Kochran and Patrick H. Wood.

The if statement is same or similar across many platforms.

ie.
if [ some condition ]
then
do something
else
do something else
fi

There are many variations and ways of compounding and nesting.

Fortran in '72, yeh, I remember doing that .... thanks for the reminder ....

Regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."
Carlos Fernandez Riera
Honored Contributor

Re: "if" statement usage in unix


A simple response:

VAR1="text1"
VAR2=44

if [ ${VAR1} = "text" -o ${VAR2} -ge 66 ]
then

echo IN

else
echo OUT
fi





Maybe man test is better reference.

Lots of examples in /sbin/rc0.d


I will be here for a while...

unsupported
James A. Donovan
Honored Contributor

Re: "if" statement usage in unix

"man test" will explain the differences between -eq and =, etc., etc.....
Remember, wherever you go, there you are...
Michael Tully
Honored Contributor

Re: "if" statement usage in unix

Here is the list of conditions from the ksh man page:

Conditional Expressions.
A conditional expression is used with the [[ compound command to test
attributes of files and to compare strings. Word splitting and file
name generation are not performed on the words between [[ and ]].
Each expression can be constructed from one or more of the following
unary or binary expressions:

-a file True if file exists.

-b file True if file exists and is a block special
file.
-c file True if file exists and is a character
special file.
-d file True if file exists and is a directory.
-f file True if file exists and is an ordinary file.
-g file True if file exists and is has its setgid bit
set.
-h file True if file exists and is a a symbolic link.
-k file True if file exists and is has its sticky bit
set.
-n string True if length of string is non-zero.
-o option True if option named option is on.
-p file True if file exists and is a fifo special
file or a pipe.
-r file True if file exists and is readable by
current process.
-s file True if file exists and has size greater than
zero.
-t fildes True if file descriptor number fildes is open
and associated with a terminal device.
-u file True if file exists and is has its setuid bit
set.
-w file True if file exists and is writable by
current process.
-x file True if file exists and is executable by
current process. If file exists and is a
directory, the current process has permission
to search in the directory.
-z string True if length of string is zero. -L file
True if file exists and is a symbolic link.
-O file True if file exists and is owned by the
effective user ID of this process.
-G file True if file exists and its group matches the
effective group ID of this process.
-S file True if file exists and is a socket.
file1 -nt file2 True if file1 exists and is newer than file2.
file1 -ot file2 True if file1 exists and is older than file2.
file1 -ef file2 True if file1 and file2 exist and refer to
the same file.
string = pattern True if string matches pattern.
string != pattern True if string does not match pattern.
string1 < string2 True if string1 comes before string2 based on
ASCII value of their characters.
string1 > string2 True if string1 comes after string2 based on
ASCII value of their characters.
exp1 -eq exp2 True if exp1 is equal to exp2.
exp1 -ne exp2 True if exp1 is not equal to exp2.
exp1 -lt exp2 True if exp1 is less than exp2.
exp1 -gt exp2 True if exp1 is greater than exp2.



How bout an amusing example:

if [ -s $FRIDGE} ]
then /usr/local/bin/different_fridge
else
echo "Dear I'm off the nearest bar!"
done

Cheers
Michael
Anyone for a Mutiny ?
John Dvorchak
Honored Contributor

Re: "if" statement usage in unix

You might want to take a look here. Yeah I know is it for Solaris, but ksh is the same everywhere. Isn't it? Well close anyway.

http://www.bolthole.com/solaris/ksh.html

Good luck,
John
If it has wheels or a skirt, you can't afford it.
A. Clay Stephenson
Acclaimed Contributor

Re: "if" statement usage in unix


Bear in mind that you should really learn one if and stick with it. The '[ ]' style if calls the external command test while the '[[ ]]' style command is internal to the shell BUT the syntax is a little different.

if [ ${A} -le 5 -a ${B} -gt 10 ]
then
echo "Do something"
fi

becomes


if [ ${A} -le 5 && ${B} -gt 10 ]
then
echo "Do something"
fi

Note : AND -a becomes && and
OR -o becomes ||.

NOTE: The space between the comparand and the brackets is REQUIRED.

If it ain't broke, I can fix that.
Jeff Schussele
Honored Contributor

Re: "if" statement usage in unix

Hey Michael...

I know you Ozzers are a different lot mate, but that if statement should be

if
then
else
fi

Or is it...

for
do
fe
fi
fo
fum

Cheers buddy,
Jeff (Who sure wishes he was down under)
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Michael Tully
Honored Contributor

Re: "if" statement usage in unix

Looks like the old fat finger syndrome....

if [ -s $FRIDGE} ]
then /usr/local/bin/different_fridge
else
echo "Dear I'm off the nearest bar!"
fi

Perhaps it's either too much coffee and not enough beer .... 11:25AM .... bring on Friday lunch!

Anyone for a Mutiny ?
Chris Vail
Honored Contributor

Re: "if" statement usage in unix

I'll second the motion on the Kochan and Wood book. Mine already fell apart once from use, and its about to happen again.

Unlike some of the others, I come from a non technical background (sales). So I understand you confusion about the 'if' command. Here's a primer:
if
then
do something
else
do something else
fi

I almost always use 'if' with 'test'. The test command is interesting when used with the 'if' command. Do a man on test to find all the options, but I use the -f the -eq and the = options all the time. There are differences between -eq and =. The = compares strings, and the -eq compares numeric values.

It also really, really helps to know how, when and where to use quotes, and what kind to use. The Kochan & Wood book has a whole chapter on this.

I've attached a fully documented and commented script that uses Netbackup commands to create a report. Forget the Netbackup commands and look at the way the if/then statements are used. There is also a simple looping structure within the main if/then statement.

Feel free to write me with any questions: cvail "at" ercot dot com.

Chris
Paul Middleton
Frequent Advisor

Re: "if" statement usage in unix

Gentlemen,
Thanks for the information. Sometimes you do learn one form of an if statement, then you need to change the way if works, and you have a massive brain fart.
.
The list of conditions is very useful and I'm looking into the man pages for sh and ksh now.
.
Again, thanks for all the help.
.
Paul Middleton
Dilligad - Do I Look Like I Give A Damn
Mark Ellzey
Valued Contributor

Re: "if" statement usage in unix

Paul,

I scanned the messages pretty quickly, but did not see 'test' mentioned. Do a man on 'test' for more info on the different options available for the 'if' construct.

Regards,
Mark
Gregory Fruth
Esteemed Contributor

Re: "if" statement usage in unix

The '[ ...'] construct is an alias for the
command 'test', as someone else noted.
However, I generally avoid '[ ... ]' / 'test'
because it doesn't handle empty
arguments very well. The '[[ ... ]]'
operator is better behaved. See
the man page for ksh or sh-posix
for details on '[[ ... ]]'

For example:

foo=""
if [ -n $foo ]
then
echo foo is set
else
echo foo is not set
fi

Because "$foo" evaluates to nothing,
"-n $foo" evaluates to "-n", which causes
the warning:

test: Specify a parameter with this command.

You have to put double quotes around $foo in
this example. The '[[ ... ]]' operator does not
have this problem.

In old scripts, you'll sometimes see people
do this to get around the problem:

if [ X"$foo" = Xsomestring ]

HTH