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:
| |
| 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:
# if...then...else example # written by Elliott Technologies NUM=0 if [ $NUM -eq 0 ]; then
| |
| 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
read userval case $userval in
echo -n "you answered yes " ;;
echo Stopping since you answered no val=0 ;;
| |










0 blogger-disqus:
Post a Comment