#!/bin/bash
# How to do stuff in bash shell -- compare with tcsh_syntax for tcsh equivalents
# 1: Using Variables
# 2: Command Line Arguments aka Positional Parameters
# 3: Command Error Codes
# 4: Quoting
# 5: Conditional Statements - If, case
# 6: String Comparisons
# 7: Number Comparisons
# 8: Logical Operators
# 9: File Comparisons
# 10: Looping
# 11: Functions
echo
echo bash Syntax Guide
echo 1: Using variables ---------------
# No space allowed around the = sign
var1=0
var2=text
var3='text with spaces'
var4="text with spaces"
var5=text\ with\ spaces
echo var1=$var1
echo var2=$var2
echo var3="'$var3'"
echo var4=\"$var4\"
echo var5=text\\ with\\ spaces -- $var5
echo
echo 2: Command Line Arguments aka Positional Parameters ---------------
numargs=$#
progname=$0
arg1=$1
arg2=$2
allargs=$*
allargs2=$@
# use shift to discard $1 and shift all parameters down one
# thus $1 will now be $2 etc. Specify a number with shift
# to shift by that many parameters
shift
newarg1=$1
echo numargs=$numargs
echo progname=$progname
echo arg1=$arg1
echo arg2=$arg2
echo allargs=$allargs
echo allargs2=$allargs2
echo newarg1=$newarg1
echo
echo 3: Command Error Codes ---------------
# command return value of zero is success - non-zero indicates an error code
cmd_error=$?
echo cmd_error=$cmd_error
echo
echo 4: Quoting ---------------
# double quotes interpolate variables, single quotes do not
# backslash takes the next character literally
# backticks ` execute a shell command and evaluate to standard output
var2="this is $var1"
var3='this is $var1'
var4="this is \$var1"
var5=`wc -l $0`
echo var2=$var2
echo var3=$var3
echo var4=$var4
echo var5=$var5
echo
echo 5: Conditional Statements - If ---------------
if [ "$var1" = "Yes" ]; then
echo If Yes
elif [ "$var1" = "No" ]; then
echo Elif No
else
echo Else $var1
fi
# If you leave off the second semicolon in a case it will fall through
# to the next case. You can use wildcards ?*[] in the case selections for matching strings
case $var1 in
yes | YES) echo Case: Yes;;
[nN][oO]) echo Case: No;;
*) echo Case: default;;
esac
echo
echo 6: String Comparisons ---------------
# $s not-empty?
# $s = $s equal?
# $s != $s not equal?
# -z $s zero length?
# -n $s non-zero length?
# -l $s length of string
string1="abc"
string2="abd"
string3=""
# note string4 is undefined
string5=0
string6=1
string7=
# bash doesn't care about echoing or using undefined variables
echo string1=$string1
echo string2=$string2
echo string3=$string3
echo string4=$string4
echo string5=$string5
echo string6=$string6
echo string7=$string7
# you can use test or [ interchangeably
if test $string1 = $string2; then
echo test eq1: string1 equal to string2
else
echo test eq1: string1 not equal to string2
fi
if test $string3 = $string4; then
echo test eq2: string3 equal to string4
else
echo test eq2: string3 not equal to string4
fi
# This generates a syntax error - string 4 not defined - be careful
# bash will continue on though
if test $string5 = $string4; then
echo test eq3: string5 equal to string4
else
echo test eq3: string5 not equal to string4
fi
# here's how to be safe with string comparisons
# always use quotes and
if test "$string5" = "$string4"; then
echo test eq4: string5 equal to string4
else
echo test eq4: string5 not equal to string4
fi
if test "-$string5" = "-$string4"; then
echo test eq5: string5 equal to string4
else
echo test eq5: string5 not equal to string4
fi
# Note: the space after the [ is important because [ is an external program
# not an internal shell syntax
# test for equality
if [ $string1 = $string2 ]; then
echo test eq6: string1 equal to string2
else
echo test eq6: string1 not equal to string2
fi
# test for inequolity
if [ $string2 != $string1 ]; then
echo test ne1: string2 not equal to string1
else
echo test ne1: string2 equal to string1
fi
# test for emptiness
if [ $string1 ]; then
echo test empty1: string1 is not empty
else
echo test empty1: string1 is empty
fi
if [ $string3 ]; then
echo test empty2: string3 is not empty
else
echo test empty2: string3 is empty
fi
# works if variable is not defined
if [ $string4 ]; then
echo test empty3: string4 is not empty
else
echo test empty3: string4 is empty
fi
if test $string4; then
echo test empty4: string4 is not empty
else
echo test empty4: string4 is empty
fi
if [ -z $string1 ]; then
echo test zlen1: string1 has length equal to zero
else
echo test zlen1: string1 has a length greater than zero
fi
if [ -z $string3 ]; then
echo test zlen2: string3 has length equal to zero
else
echo test zlen2: string3 has a length greater than zero
fi
if [ -z $string4 ]; then
echo test zlen3: string4 has length equal to zero
else
echo test zlen3: string4 has a length greater than zero
fi
if [ -z "$string1" ]; then
echo test zlen4: string1 has length equal to zero
else
echo test zlen4: string1 has a length greater than zero
fi
if [ -z "$string3" ]; then
echo test zlen5: string3 has length equal to zero
else
echo test zlen5: string3 has a length greater than zero
fi
if [ -z "$string4" ]; then
echo test zlen6: string4 has length equal to zero
else
echo test zlen6: string4 has a length greater than zero
fi
# NOTE: These tests don't work unless quotes are used!
if [ -n $string1 ]; then
echo test nzlen1: string1 has a length greater than zero
else
echo test nzlen1: string1 has length equal to zero
fi
if [ -n $string3 ]; then
echo test nzlen2: string3 has a length greater than zero
else
echo test nzlen2: string3 has length equal to zero
fi
if [ -n $string4 ]; then
echo test nzlen3: string4 has a length greater than zero
else
echo test nzlen3: string4 has length equal to zero
fi
if [ -n "$string1" ]; then
echo test nzlen4: string1 has a length greater than zero
else
echo test nzlen4: string1 has length equal to zero
fi
if [ -n "$string3" ]; then
echo test nzlen5: string3 has a length greater than zero
else
echo test nzlen5: string3 has length equal to zero
fi
if [ -n "$string4" ]; then
echo test nzlen6: string4 has a length greater than zero
else
echo test nzlen6: string4 has length equal to zero
fi
if [ -l $string1 -eq 3 ]; then
echo test len1: string1 has a length of three
else
echo test len1: string1 has length of not three
fi
# NOTE This is NOT the way to do it!
if [ -l $string1 = 3 ]; then
echo test len2: string1 has a length of three
else
echo test len2: string1 has length of not three
fi
# Quotes needed here also
if [ -l "$string3" -eq 3 ]; then
echo test len3: string3 has a length of three
else
echo test len3: string3 has length of not three
fi
if [ -l "$string4" -eq 3 ]; then
echo test len4: string4 has a length of three
else
echo test len4: string4 has length of not three
fi
echo
echo 7: Number Comparisons ---------------
# $n -eq $n
# $n -ge $n
# $n -le $n
# $n -ne $n
# $n -gt $n
# $n -lt $n
number1=5
number2=10
number3=05
number4=""
# number5 not defined
echo number1=$number1
echo number2=$number2
echo number3=$number3
echo number4=$number4
echo number5=$number5
if [ $number1 -eq $number3 ]; then
echo test eq1: number1 is equal to number3
else
echo test eq1: number1 is not equal to number3
fi
if [ $number1 -eq $number2 ]; then
echo test eq2: number1 is equal to number2
else
echo test eq2: number1 is not equal to number2
fi
# This generates an error number4 is not defined
if [ $number1 -eq $number4 ]; then
echo test eq3: number1 is equal to number4
else
echo test eq3: number1 is not equal to number4
fi
# once again, use a trick to fix this
if [ "0$number1" -eq "0$number4" ]; then
echo test eq4: number1 is equal to number4
else
echo test eq4: number1 is not equal to number4
fi
# This is also an error
if [ $number1 -eq $string1 ]; then
echo test eq5: number1 is equal to string1
else
echo test eq5: number1 is not equal to string1
fi
if [ "0$number1" -eq "0$string1" ]; then
echo test eq6: number1 is equal to string1
else
echo test eq6: number1 is not equal to string1
fi
if [ $number1 -ne $number3 ]; then
echo test ne1: number1 is not equal to number3
else
echo test ne1: number1 is equal to number3
fi
if [ $number1 -ne $number2 ]; then
echo test ne2: number1 is not equal to number2
else
echo test ne2: number1 is equal to number2
fi
if [ $number1 -gt $number3 ]; then
echo test gt1: number1 is greater than number3
else
echo test gt1: number1 is not greater than number3
fi
if [ $number2 -gt $number1 ]; then
echo test gt2: number2 is greater than number1
else
echo test gt2: number2 is not greater than number1
fi
if [ $number1 -ge $number3 ]; then
echo test ge1: number1 is greater than or equal to number3
else
echo test ge1: number1 is not greater than or equal to number3
fi
if [ $number2 -ge $number1 ]; then
echo test ge2: number2 is greater than or equal to number1
else
echo test ge2: number2 is not greater than or equal to number1
fi
if [ $number1 -ge $number2 ]; then
echo test ge3: number1 is greater than or equal to number2
else
echo test ge3: number1 is not greater than or equal to number2
fi
if [ $number1 -lt $number3 ]; then
echo test lt1: number1 is less than number3
else
echo test lt1: number1 is not less than number3
fi
if [ $number1 -lt $number2 ]; then
echo test lt2: number1 is less than number2
else
echo test lt2: number1 is not less than number2
fi
if [ $number1 -le $number3 ]; then
echo test le1: number1 is less than or equal to number3
else
echo test le1: number1 is not less than or equal to number3
fi
if [ $number2 -le $number1 ]; then
echo test le2: number2 is less than or equal to number1
else
echo test le2: number2 is not less than or equal to number1
fi
if [ $number1 -le $number2 ]; then
echo test le3: number1 is less than or equal to number2
else
echo test le3: number1 is not less than or equal to number2
fi
echo
echo 8: Logical Operators ---------------
# ! ex negation
# ex -a ex logical AND
# ex -o ex logical OR
# Note the space around the !
if [ ! $number1 -eq 5 ]; then
echo test not1: number1 is not \(equal to 5\)
else
echo test not1: number1 is equal to 5
fi
if [ ! $number1 -ne 5 ]; then
echo test not2: number1 is not \(not equal to 5\)
else
echo test not2: number1 is not equal to 5
fi
# NOTE the ! negates just the next logical expression, not the whole expression
if [ ! 0 -eq 1 -a 0 -eq 1 ]; then
echo test not3: \! 0 -eq 1 -a 0 -eq 1 is true
else
echo test not3: \! 0 -eq 1 -a 0 -eq 1 is false
fi
# Cannot use ( ) to specify evaluation order
#if [ ! ( 0 -eq 1 -a 0 -eq 1 ) ]; then
# echo test not3: \! 0 -eq 1 -a 0 -eq 1 is true
#else
# echo test not3: \! 0 -eq 1 -a 0 -eq 1 is false
#fi
if [ ! 0 -eq 1 -a ! 0 -eq 1 ]; then
echo test not4: \! 0 -eq 1 -a \! 0 -eq 1 is true
else
echo test not4: \! 0 -eq 1 -a \! 0 -eq 1 is false
fi
if [ $number1 -a $number2 ]; then
echo test and1: number1 and number2 is true
else
echo test and1: number1 and number2 is false
fi
if [ $number1 -a "$number4" ]; then
echo test and2: number1 and number4 is true
else
echo test and2: number1 and number4 is false
fi
if [ $number1 -o "$number4" ]; then
echo test or1: number1 or number4 is true
else
echo test or1: number1 or number4 is false
fi
if [ "$number5" -o "$number4" ]; then
echo test or2: number5 or number4 is true
else
echo test or2: number5 or number4 is false
fi
echo
echo 9: File Comparisons ---------------
# see man test for even more!
# -d dir is directory?
# -e file exists?
# -f file is a regular file?
# -L file is a symbolic link??
# -O file am I the owner?
# -r file can I read it?
# -w file can I write to it?
# -x file can I execute it?
# -s file is it non-zero length?
# first set up a subdirectory and file for the test
if (-d _tmp) then
echo _tmp already exists, I dare not muck with it!
echo skipping the file and directory testing
else
mkdir _tmp
touch _tmp/file1
echo hello > _tmp/file2
touch _tmp/file3
chmod 500 _tmp/file1
chmod 200 _tmp/file3
mkdir _tmp/dir1
chmod 700 _tmp/dir
ln -s file1 _tmp/link1
ln -s dir1 _tmp/linkdir
ls -al _tmp
# Does it exist?
if [ -e _tmp/null ]; then
echo test -e1: null exists
else
echo test -e1: null does not exist
fi
if [ -e _tmp/dir1 ]; then
echo test -e1: dir1 exists
else
echo test -e1: dir1 does not exist
fi
if [ -e _tmp/file1 ]; then
echo test -e2: file1 exists
else
echo test -e2: file1 does not exist
fi
if [ -e _tmp/link1 ]; then
echo test -e3: link1 exists
else
echo test -e3: link1 does not exist
fi
if [ -e _tmp/linkdir ]; then
echo test -e4: linkdir exists
else
echo test -e4: linkdir does not exist
fi
# is it a directory?
if [ -d _tmp/null ]; then
echo test -d1: null is a directory
else
echo test -d1: null is not a directory
fi
if [ -d _tmp/dir1 ]; then
echo test -d1: dir1 is a directory
else
echo test -d1: dir1 is not a directory
fi
if [ -d _tmp/file1 ]; then
echo test -d2: file1 is a directory
else
echo test -d2: file1 is not a directory
fi
if [ -d _tmp/link1 ]; then
echo test -d3: link1 is a directory
else
echo test -d3: link1 is not a directory
fi
if [ -d _tmp/linkdir ]; then
echo test -d4: linkdir is a directory
else
echo test -d4: linkdir is not a directory
fi
# is it a file?
if [ -f _tmp/null ]; then
echo test -f1: null is a file
else
echo test -f1: null is not a file
fi
if [ -f _tmp/dir1 ]; then
echo test -f1: dir1 is a file
else
echo test -f1: dir1 is not a file
fi
if [ -f _tmp/file1 ]; then
echo test -f2: file1 is a file
else
echo test -f2: file1 is not a file
fi
if [ -f _tmp/link1 ]; then
echo test -f3: link1 is a file
else
echo test -f3: link1 is not a file
fi
if [ -f _tmp/linkdir ]; then
echo test -f4: linkdir is a file
else
echo test -f4: linkdir is not a file
fi
# is it a symbolic link?
if [ -L _tmp/null ]; then
echo test -L1: null is a link
else
echo test -L1: null is not a link
fi
if [ -L _tmp/dir1 ]; then
echo test -L2: dir1 is a link
else
echo test -L2: dir1 is not a link
fi
if [ -L _tmp/file1 ]; then
echo test -L3: file1 is a link
else
echo test -L3: file1 is not a link
fi
if [ -L_tmp/link1 ]; then
echo test -L4: link1 is a link
else
echo test -L4: link1 is not a link
fi
if [ -L _tmp/linkdir ]; then
echo test -L5: linkdir is a link
else
echo test -L5: linkdir is not a link
fi
# do I own it?
if [ -O _tmp/file1 ]; then
echo test -O1: file1 is owned by me
else
echo test -O1: file2 is not owned by me
fi
if [ -O /etc/fstab ]; then
echo test -O2: /etc/fstab is owned by me
else
echo test -O2: /etc/fstab is not owned by me
fi
# can I read it?
if [ -r _tmp/file1 ]; then
echo test -r1: file1 is readable
else
echo test -r1: file1 is not readable
fi
if [ -r _tmp/file2 ]; then
echo test -r2: file2 is readable
else
echo test -r2: file2 is not readable
fi
if [ -r _tmp/file3 ]; then
echo test -r3: file3 is readable
else
echo test -r3: file3 is not readable
fi
# can I write to it?
if [ -w _tmp/file1 ]; then
echo test -w1: file1 is writable
else
echo test -w1: file1 is not writable
fi
if [ -w _tmp/file2 ]; then
echo test -w2: file2 is writable
else
echo test -w2: file2 is not writable
fi
if [ -w _tmp/file3 ]; then
echo test -w3: file3 is writable
else
echo test -w3: file3 is not writable
fi
# can I execute it?
if [ -x _tmp/file1 ]; then
echo test -x1: file1 is executable
else
echo test -x1: file1 is not executable
fi
if [ -x _tmp/file2 ]; then
echo test -x2: file2 is executable
else
echo test -x2: file2 is not executable
fi
if [ -x _tmp/file3 ]; then
echo test -x3: file3 is executable
else
echo test -x3: file3 is not executable
fi
# Is it zero size?
if [ -s _tmp/file1 ]; then
echo test -s1: file1 is not zero size
else
echo test -s1: file1 is zero size
fi
if [ -s _tmp/file2 ]; then
echo test -s2: file2 is not zero size
else
echo test -s2: file2 is zero size
fi
if [ -s _tmp/file3 ]; then
echo test -s3: file3 is not zero size
else
echo test -s3: file3 is zero size
fi
if [ -s _tmp/dir1 ]; then
echo test -s3: dir1 is not zero size
else
echo test -s3: dir1 is zero size
fi
# clean up the test subdirectory
rm -rf _tmp
fi
echo
echo 10: Looping ---------------
# use 'break' to break out of a loop prematurely
# use 'exit' with an optional number to terminate the script with the number
# as the command return code in $?
# Loop on matching file names
for var in *.txt
do
echo for1: $var
done
# Loop on a list of items
for var in a b c
do
echo for2: $var
done
# Loop on all command line arguments
# same as for var in "$@"
for var
do
echo for3: $var
done
idx=0
while [ $idx -lt 5 ]
do
echo while1: $idx
idx=`expr $idx + 1`
done
idx=0
until [ $idx -ge 5 ]
do
echo until1: $idx
idx=`expr $idx + 1`
done
# bash has no 'repeat' command like tcsh - use a for loop
# this is much slower than tcsh
echo repeat1:
idx=1
while [ $idx -le 76 ]
do
echo -n '-'
idx=`expr $idx + 1`
done
echo " "
# select - repeatedly display a menu asking user for a selection
select item in File Edit Tools Syntax Buffers Window Help Exit
do
case $item in
File) echo File menu selected;;
Edit) echo Edit menu selected;;
Tools) echo Tools menu selected;;
Syntax) echo Syntax menu selected;;
Buffers) echo Buffers menu selected;;
Window) echo Window menu selected;;
Help) echo Help menu selected;;
Exit) break;;
*) echo Incorrect Menu Selection;;
esac
done
echo
echo 11: Functions ---------------
# Define a function to display a month name
# the word function is optional
function DisplayMonth() {
case $1 in
01 | 1) echo "January";;
02 | 2) echo "February";;
03 | 3) echo "March";;
04 | 4) echo "April";;
05 | 5) echo "May";;
06 | 6) echo "June";;
07 | 7) echo "July";;
08 | 8) echo "August";;
09 | 9) echo "September";;
10) echo "October";;
11) echo "November";;
12) echo "December";;
*) echo "Invalid";;
esac
}
DisplayMonth 8
               (
geocities.com/gurucoder/Tools)                   (
geocities.com/gurucoder)