1834926 Members
2457 Online
110071 Solutions
New Discussion

Re: If statement

 
SOLVED
Go to solution
presage112
Advisor

If statement

Where can find a manual for if statement?.
for example .

if [-s ...]

a manual for the option after if (-s ,-f ,-d).


THANKS
Rgds.
8 REPLIES 8
Olivier Decorse
Respected Contributor
Solution

Re: If statement

See the Brian Brown online "Learning Unix" courses :
http://goforit.unk.edu/unix/ostart.htm
Module 3 : shell scripts, and you will see on the "Shell conditionnal tests" :
Test Meaning:
!= not equal
= equal
-eq equal
-gt greater than
-ge greater than or equal
-lt less than
-le less than or equal
! logic negation
-a logical and
-o logical or
-r file true if the file exists and is readable
-w file true if the file exists and is writable
-x file true if the file exists and is executable
-s file true if the file exists and its size > 0
-d file true if the file is a directory
-f file true if the file is an ordinary file
-t filed true if the file descriptor is associated with a terminal
-n str true if the length of str is > 0
-z str true if the length of str is zero

Olivier.
They say "install windows 2k, xp or better", so i install unix !
T G Manikandan
Honored Contributor

Re: If statement

do a

$man 1 sh-posix

search for 'Conditional Expressions'

you will find them there
Olivier Decorse
Respected Contributor

Re: If statement

A very good doc for any advanced Shell enhancement (I/O Redirection, Performing Arithmetic, Translating Characters, Command Exit Status, etc) :
http://users.sdsc.edu/~steube/Bshell/

Olivier.
They say "install windows 2k, xp or better", so i install unix !
Jim Mallett
Honored Contributor

Re: If statement

On top of the suggestions above, you can do:
# man test

That will give you some examples of the test conditions that you can use with the if/fi statement.

Jim
Hindsight is 20/20
Muthukumar_5
Honored Contributor

Re: If statement

we can use if on shell script as,

if [[ ]]
then
< operation's >
fi

or

if [ ]
then
< operation's >
fi

where we have need a format as,
if[]
then
< operation's >
fi

if[[]]
then

fi

we have to use if / elif / else as,
if [[ ]]
then
action1
elif [[ ]]
then
action2
elif [[ ]]
then
action3
else
action4
fi

-f file True if file exists and is a regular file.
-d file True if file exists and is a directory.
-s file True if file exists and has a size greater than zero.

We can encorporate on script as,

#!/usr/bin/ksh
# Debugging mode
set -x

########################
echo "Enter input"
read input

if [[ -f $input ]]
then
echo "$input is a regular file"
elif [[ -d $input ]]
then
echo "$input is a directory"
elif [[ -s $input ]]
then
echo "$input is a file and exists with size greater than zero `ll $PWD/$input`"
else
echo "$input is not needed to check for -d / -f / -s option"
exit 1
fi

# Normal exit
exit 0

We can use another method simply as,

to check is there a file then,

[[ -f ]] && operation.

Example:

[[ -f /stand/vmunix ]] && echo "System file is there"

Regards
-Muthu
Easy to suggest when don't know about the problem!
Sยภเl Kย๓คг
Respected Contributor

Re: If statement

man page for sh-posix will help u
regrads
SK
Your imagination is the preview of your life's coming attractions
Jeroen Peereboom
Honored Contributor

Re: If statement

The [] are a shortcut for the 'test' command.
So indeed 'man test' will give you info.
But 'test' is also a 'shell built-in', so you may also have a look at the shell's manpage.

JP.
presage112
Advisor

Re: If statement

All comments are helpful ,but I like "man test" more.

Thanks all.