|
|
| Portable Shell Programming (ISBN: 0134514947) |
 |
List Price: $49.93
Our Price: $49.93
Used Price: $29.89
Release Date: 19 October, 1995
Manufacturer: Prentice Hall PTR (Paperback)
Sales Rank: 19,475
Author: Bruce Blinn
|
More Info
|
|
|
If Statement
|
2002-09-03 23:15:56
|
| |
sh
|
|
|
|
|
Category: source:sh:general
|
|
Description: Examples using the if statement in shell scripts.
|
|
Platform: unix
|
|
Author: mind
|
|
Viewed: 12581
|
|
Rating: 4.6/5 (33 votes)
|
|
|
| If you have any questions about
this piece of code or still need help, try posting your question on the forum. |
#!/bin/sh
#
# if_sh - mind (mind@metalshell.com)
#
# Basic example on the if structure
#
# [ http://www.metalshell.com ]
# if list then list [ elif list then list ] ... [ else list ] fi
#
# list: this would be our true/false statement
# then: open's your if structure and every statement within it and 'fi'
# will be executed if the statement is true.
# list: this is where your commands or statements are placed. This could
# also hold other if structures, for, or while structures,
# anything; possibilities are endless..
# elif: "else if", if our statement is false elif would be used to test
# a different statement
# list: this would be our true/false statement
# then: open's your elif structure and pass's all statement's within
# it and 'fi'
# list: would be commands, statement's and other structures to pass
# within our elif structure
# else: this would be read in if our if structure was false.
# list: would be commands, etc..
# fi: would finish our if structure
# This half should be used in a if structure.
#
# The below are conditional expressions used to test file attributes
# and perform string and arithmetic comparisons. All expressions
# below are true if..
#
# -a file (file exists)
# -b file (file exists and is a block special)
# -c file (file exists and is a character special)
# -d file (file exists and is a directory)
# -e file (file exists)
# -f file (file exists and is a regular file)
# -g file (file exists and is set-group-id)
# -h file (file exists and is a symbolic link)
# -k file (file exists and is sticky)
# -p file (file exists and is a named pipe)
# -r file (file exists and is readable)
# -s file (file exists and has a size greater than zero)
# -t fd (file descriptor is open and refers to a terminal)
# -u file (file exists and is set-user-id)
# -w file (file exists and is writable)
# -x file (file exists and is executable)
# -O file (file exists and is owned by the current user)
# -G file (file exists and is owned by the current user group)
# -L file (file exists and is a symbolic link)
# -S file (file exists and is a socket)
# -N file (file exists and has been modified since last read)
#
# file1 -nt file2 (file1 is newer by modification date than file2)
# file1 -ot file2 (file1 is older by modification date than file2)
# file1 -ef file2 (file1 and file2 have the same device and inode)
#
# -o optname (shell option optname is enabled)
#
# -z string (length of string is zero)
# -n string (length of string is non-zero)
#
# string1 == string2 (strings are equal)
# string1 != string2 (string are not equal)
# string1 < string2 (string1 sorts before string2)
# string1 > string2 (string1 sorts after string2)
#
# The below are arithmetic binary operators
#
# arg1 OP arg2
# OP:
# -eq (arg1 is equal to arg2)
# -ne (arg1 is not equal to arg2)
# -lt (arg1 is less than arg2)
# -le (arg1 is less than or equal to arg2)
# -gt (arg1 is greater than arg2)
# -ge (arg1 is greater than or equal to arg2)
# in our first example we are going to compare two strings
# define str1 and str2 as a string with: test
str1="test"
str2="test"
# start our first if structure
echo "--- \"if [ \"\$str1\" = \"\$str2\" ]\" example ---"
if [ "$str1" = "$str2" ]
then
# print success
echo "$str1 and $str2 match"
fi
# Now we are going to append 'a' to str2 and compare them again
# this time using elif and the else structure's..
# define str2 as a string with: $str2+a (testa)
str2="${str2}a"
# print blank line for spacing
echo
# start our second if structure
echo "--- \"if [ \"\$str1\" = \"\$str2\" ]\" example #2 ---"
if [ "$str1" = "$str2" ]
then
# print success
echo "$str1 and $str2 match"
elif [ "${str1}a" = "$str2" ]
then
# print success
echo "${str1}a and $str2 match"
else
# print failure
echo "Neither $str1 nor $str1a match $str2"
fi
# In our third example we are going to use the if structure in
# a while structure to match a two variables
# define i as a integer of 0
i=0
# define y as a integer of 10
y=10
# print blank line for spacing
echo
# start our third if structure
echo "--- \"if [ \$i -ne \$y ]\" example ---"
while [ 1 -eq 1 ]
do
# The expr utility evaluates <expression> and writes the
# result on standard output.
# expr <expression>
# the below would increase $i by 1
i=`expr $i + 1`
echo "Number $i of $y"
if [ $i -eq $y ]
then
# print success
echo "Finished.."
# break our never-ending while loop
break
fi
done
|
|
|