- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: "if" statement usage in unix
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 02:35 PM
тАО02-20-2003 02:35 PM
.
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 02:41 PM
тАО02-20-2003 02:41 PM
Re: "if" statement usage in unix
# 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 02:44 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 02:45 PM
тАО02-20-2003 02:45 PM
Re: "if" statement usage in unix
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 02:50 PM
тАО02-20-2003 02:50 PM
Re: "if" statement usage in unix
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 02:54 PM
тАО02-20-2003 02:54 PM
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 03:09 PM
тАО02-20-2003 03:09 PM
Re: "if" statement usage in unix
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 03:15 PM
тАО02-20-2003 03:15 PM
Re: "if" statement usage in unix
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 03:22 PM
тАО02-20-2003 03:22 PM
Re: "if" statement usage in unix
http://www.bolthole.com/solaris/ksh.html
Good luck,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 03:31 PM
тАО02-20-2003 03:31 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 04:13 PM
тАО02-20-2003 04:13 PM
Re: "if" statement usage in unix
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 04:25 PM
тАО02-20-2003 04:25 PM
Re: "if" statement usage in unix
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2003 04:32 PM
тАО02-20-2003 04:32 PM
Re: "if" statement usage in unix
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-21-2003 05:41 AM
тАО02-21-2003 05:41 AM
Re: "if" statement usage in unix
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-21-2003 06:48 AM
тАО02-21-2003 06:48 AM
Re: "if" statement usage in unix
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-21-2003 11:14 AM
тАО02-21-2003 11:14 AM
Re: "if" statement usage in unix
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