SHELL SCRIPTS TIPS-N-TRICKS
The syntax and examples are written for use with bash (Bourne Again Shell) other shells may vary.
|
All script files must declare a shell on the first line.
All the examples provided herein assume the first line is the following:
Also, you must have execute permissions on the script file you create.
You can give yourself execute permissions by typing the following:
|
|
|
Special Characters
|
| # |
comments |
| * |
wildcard |
| ? |
single character wildcard |
|
redirect input - stdin |
|
redirect output - stdout |
|
|
string comparisons
|
| = |
equal |
| != |
not equal |
| < |
is less than |
| > |
is greater than |
| -n |
is not null |
| -z |
is null |
|
|
numeric comparisons
|
|
-eq
|
equal
|
|
-ge
|
greater than or equal to
|
|
-gt
|
greater than
|
|
-le
|
less than or equal to
|
|
-lt
|
less than
|
|
-ne
|
not equal
|
|
|
file operators
|
| -d file |
file exists and is directory |
| -e file |
file exists |
| -f file |
file exists and is regular |
| -s file |
file exists and is not empty |
|
file permission tests
|
| -r file |
you have read permission |
| -w file |
you have write permission |
| -x file |
you have execute permission |
| -O file |
you have ownership |
| -G file |
you have group ownership |
|
file comparisons
|
| file1 -nt file2 |
file1 newer than file2 |
| file1 -ot file2 |
file1 older than file2 |
|
|
if then else statement
|
|
operators
|
|
-a
|
and
|
|
-o
|
or
|
Example:
#!/bin/bash
# if...then...else example
# written by Elliott Technologies
NUM=0
if [ $NUM -eq 0 ]; then
else
fi
|
|
|
while and case example:
|
#!/bin/bash
# written by Elliott Technologies to demonstrate
# a shell script that implements while and case # statements
echo example of case
val=1
while [ $val = 1 ]
do
echo -n "continue:"
read userval
case $userval in
Yes|[Yy] )
# the above matches on Yes, Y, or y
echo -n "you answered yes " ;;
No|[Nn] )
# the above matches on No, N, or n
echo Stopping since you answered no
val=0 ;;
* )
echo -n "please answer (Yes/No) " ;;
esac
done
|
For more information, please send e-mail to:
Info@Elliott-Technologies.com
|