CST8177 - Linux II: Shell Scripting Todd Kelley
CST8177 - Linux II: Shell Scripting Todd Kelley
Shell Scripting
Todd Kelley
[email protected]
#!/bin/sh -u
PATH=/bin:/usr/bin ; export PATH # add /sbin and /usr/sbin if needed
umask 022 # use 077 for secure scripts
in PATH
./command.sh arg1 arg2 arg3
interpreter arguments
The shebang line mechanism is not being
was OK
0 means "true"
grep returns 0 if a match occurred, 1 if not,
# Body of script
myvar="howdy doody"
echo "The value of \$myvar is: $myvar" #notice backslash
echo "The number of arguments is: $#"
echo "The command name is $0"
echo "The arguments are: $*"
echo "The first argument is: $1"
echo "The second argument is: $2"
echo "The third argument is: $3"
#!/bin/sh -u
read aline #script will stop, wait for user
echo "you entered: $aline"
#!/bin/sh u
read p "enter your string:" aline
echo "You entered: $aline"
if [ -e /etc/passwd ]; then
echo "/etc/passwd exists"
fi
is the same as
if test e /etc/passwd; then
echo "/etc/passwd exists"
fi
if [ 0 eq 1 ]; then
echo that test is true
else
echo that test is false
fi
[ 004 eq 4 ]
Notice double quotes don't change anything
Is 00 equal to 0 as strings? no
[ 00 = 0 ]
Is 0004 equal to 4 as strings? no
[ 0004 = 4 ]
[ -e /etc ]
Does the value of myvar exist as a file or
directory?
[ -e "$myvar" ]
[ -r /etc ]
Is the value of myvar readable as a file or
directory?
[ -r "$myvar" ]
Not readable?
[ ! r "$myvar" ]
[ ! e /etc/passwd ]