ch5 condition execution
spaces around the [ and ] are required
echo $? return non-zero for failure and zero for success
---------
#!/bin/bash
# Test for failure
cat “$1”
if [ “$?” -ne “0” ]; then
echo “Error: Reading $1 failed.”
fi
---------
-r test to see if a file exists and is readable
#!/bin/bash
# Test for likely causes of failure
if [ ! -r “$1” ]; then
echo “Error: $1 is not a readable file.”
echo “Quitting.”
exit 1
fi
cat "$1"
----------
if [ -r “$1” ]; then cat “$1”; fi
if [ ! -r “$1” ]; then echo “File $1 is not readable – skipping. “; fi
----------
In the shell, [ is a program, and ] is simply an argument to it, which is required, but discarded. It has no significance to the shell whatsoever.
----------
The ability to execute a directory is treated as the right to change into that directory
----------
This adds ~/bin to the PATH environment, but only if that directory exists:
if [ -d ~/bin ]; then
PATH=$PATH:~/bin
fi
----------
cat rwx
#!/bin/bash
while read -p "What file do you want to test? " filename
do
if [ ! -e "$filename" ]; then
echo “The file does not exist.”
continue
fi
# Okay, the file exists.
ls -ld "$filename"
if [ -r "$filename" ]; then
echo “$filename is readable.”
fi
if [ -w "$filename" ]; then
echo “$filename is writeable”
fi
if [ -x "$filename" ]; then
echo “$filename is executable”
fi
done
ubuntu2@ubuntu2:~/Documents/shellscripting/ch5$ bash rwx
What file do you want to test? rcheck
-rw-rw-r-- 1 ubuntu2 ubuntu2 82 Sep 13 05:26 rcheck
“rcheck is readable.”
“rcheck is writeable”
echo $? return non-zero for failure and zero for success
---------
#!/bin/bash
# Test for failure
cat “$1”
if [ “$?” -ne “0” ]; then
echo “Error: Reading $1 failed.”
fi
---------
-r test to see if a file exists and is readable
#!/bin/bash
# Test for likely causes of failure
if [ ! -r “$1” ]; then
echo “Error: $1 is not a readable file.”
echo “Quitting.”
exit 1
fi
cat "$1"
----------
if [ -r “$1” ]; then cat “$1”; fi
if [ ! -r “$1” ]; then echo “File $1 is not readable – skipping. “; fi
----------
In the shell, [ is a program, and ] is simply an argument to it, which is required, but discarded. It has no significance to the shell whatsoever.
----------
The ability to execute a directory is treated as the right to change into that directory
----------
This adds ~/bin to the PATH environment, but only if that directory exists:
if [ -d ~/bin ]; then
PATH=$PATH:~/bin
fi
----------
cat rwx
#!/bin/bash
while read -p "What file do you want to test? " filename
do
if [ ! -e "$filename" ]; then
echo “The file does not exist.”
continue
fi
# Okay, the file exists.
ls -ld "$filename"
if [ -r "$filename" ]; then
echo “$filename is readable.”
fi
if [ -w "$filename" ]; then
echo “$filename is writeable”
fi
if [ -x "$filename" ]; then
echo “$filename is executable”
fi
done
ubuntu2@ubuntu2:~/Documents/shellscripting/ch5$ bash rwx
What file do you want to test? rcheck
-rw-rw-r-- 1 ubuntu2 ubuntu2 82 Sep 13 05:26 rcheck
“rcheck is readable.”
“rcheck is writeable”
------------------
You can also find out whether a file is owned by the current user and/or group ID, with the -O and
-G flags respectively
-----------------
Another feature of Unix-style file permissions are the suid (Set UserID) and sgid (Set GroupID)bits.
These allow the program to be run as the user (or group), which owns the file, not necessarily the
user (or group) running the program. This is shown as an s instead of an x in the rwx style of displaying file permissions. You can test for these using the -g and -u flags respectively.
-----------------
$stat --printf=%G f; echo ;
ubuntu2
$stat --printf=%G f; echo ;
ubuntu2
stat f
File: ‘f’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 158577 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ ubuntu2) Gid: ( 1000/ ubuntu2)
Access: 2015-09-13 05:25:14.637260130 -0700
Modify: 2015-09-13 05:25:11.161260170 -0700
Change: 2015-09-13 05:25:11.161260170 -0700
Birth: -
-------------------
if [ -s /dir/filename ]
the -s flag tells you if the file exists and has a size greater than zero
-------------------
inode information
$stat --format=%i f
158577
$ ls -i
158577 f 158579 rcheck 158580 rwx
------------------
String comparison
The
string comparisons < and > only work within the [[ ... ]] compound command. You can use the ==
and != tests within either a single or double bracket test, but the single bracket test is the only one that
works with a single equal sign (=).
The bash shell, and others, will also accept a double ==, which is more in line with
other languages (notably C). However, this is not compliant with the POSIX
standard, and the traditional Bourne shell does not recognize this syntax.
------------------
cat whilez
#!/bin/bash
input=""
while [ -z "$input" ]; do
read -p "Please enter the text: " input
done
echo Thank you for entering $input
String comparison
The
string comparisons < and > only work within the [[ ... ]] compound command. You can use the ==
and != tests within either a single or double bracket test, but the single bracket test is the only one that
works with a single equal sign (=).
The bash shell, and others, will also accept a double ==, which is more in line with
other languages (notably C). However, this is not compliant with the POSIX
standard, and the traditional Bourne shell does not recognize this syntax.
------------------
cat whilez
#!/bin/bash
input=""
while [ -z "$input" ]; do
read -p "Please enter the text: " input
done
echo Thank you for entering $input
bash whilez
Please enter the text: test
Thank you for entering test
------------------
Regular expression tests
A feature new since bash 3 is the =~ operator, which acts much like its perl equivalent. It treats the
right side as an extended regular expression, so bash can now do some of the things that previously
one would need to go to perl, sed, or grep to achieve
#!/bin/bash
for deb in pkgs/*
do
pkgname=`basename $deb`
if [[ $pkgname =~ .*\.deb ]]; then
echo “$pkgname is a .deb package”
else
echo “File \”$pkgname\” is not a .deb package.”
fi
done
if [[ $pkgname =~ (.+)_(.*)_(.*)\.deb ]]; then
----------------
Numerical comparisons
Six numerical comparisons are available. The -eq test returns true if the two numbers are equal,
while -ne returns true if they are not equal. -lt and -gt respectively are used for comparing if one
number is less than or greater than the other. If you need to test if the values are less than or equal
to, -le does that test, and -ge does the complementary test to see if one number is greater or equal
to the other.
-----------------
Combining tests
It is possible to combine tests by using the && and || operators. These perform a Logical AND and
Logical OR, respectively
----------------
file is readable and is of nonzero length
if [ -r “$filename” ] && [ -s “$filename” ]; then
----------------
CASE
#!/bin/bash
read -p “Which city are you closest to?: “ city
case $city in
“New York”|London|Paris|Tokyo)
# You can identify the capital cities and still fall through to
# match the specific country below.
echo “That is a capital city” ;;&
Chicago|Detroit|”New York”|Washington)
echo “You are in the USA” ;;
London|Edinburgh|Cardiff|Dublin)
echo “You are in the United Kingdom” ;;
“Ramsey Street”)
# This is a famous street in an unspecified location in Australia.
# You can still fall through and run the generic Australian code
# by using the ;& ending.
echo “G’Day Neighbour!” ;&
Melbourne|Canberra|Sydney)
echo “You are in Australia” ;;
Paris)
echo “You are in France” ;;
Tokyo)
echo “You are in Japan” ;;
N*)
# We have already matched “New York” and ended it with a ;;
# so New York will not fall through to this test. Other places
# beginning with N will fall through to here.
echo “Your word begins with N but is not New York” ;;
*)
echo “I’m sorry, I don’t know anything about $city” ;;
esac
#!/bin/bash
read -p “Which city are you closest to?: “ city
case $city in
“New York”|London|Paris|Tokyo)
# You can identify the capital cities and still fall through to
# match the specific country below.
echo “That is a capital city” ;;&
Chicago|Detroit|”New York”|Washington)
echo “You are in the USA” ;;
London|Edinburgh|Cardiff|Dublin)
echo “You are in the United Kingdom” ;;
“Ramsey Street”)
# This is a famous street in an unspecified location in Australia.
# You can still fall through and run the generic Australian code
# by using the ;& ending.
echo “G’Day Neighbour!” ;&
Melbourne|Canberra|Sydney)
echo “You are in Australia” ;;
Paris)
echo “You are in France” ;;
Tokyo)
echo “You are in Japan” ;;
N*)
# We have already matched “New York” and ended it with a ;;
# so New York will not fall through to this test. Other places
# beginning with N will fall through to here.
echo “Your word begins with N but is not New York” ;;
*)
echo “I’m sorry, I don’t know anything about $city” ;;
esac
$ ./case2.sh
Which city are you closest to?: London
That is a capital city
You are in the United Kingdom
$ ./case2.sh
Which city are you closest to?: Paris
That is a capital city
You are in France
Which city are you closest to?: London
That is a capital city
You are in the United Kingdom
$ ./case2.sh
Which city are you closest to?: Paris
That is a capital city
You are in France
Comments
Post a Comment