Upload
lee-weaver
View
283
Download
1
Embed Size (px)
Citation preview
Shells
Variables
• MESSAGE="HELLO WORLD"• echo $MESSAGE• MESSAGE="HELLO WORLD $LOGNAME"• echo $MESSAGE• MESSAGE="HELLO WORLD $USER"• echo $MESSAGE
comment
• #this is a comment• #this just prints to screen• echo hello mum• save this as hello.sh• change permissions• run as• sh hello.sh
utilities and commands
• a utility is a program, • a command is an instruction• uname -a > outputfile.txt• (will display vital statistics on the system.)
Five different types of command
• simple command• pipeline• list command• function definition• compound command
Grouping Commands
• (echo start ; cat hello.sh ; echo end) > new.txt• adds start and end to hello.sh• command1 || (command2 && command3)
Exit Status
• 0 if previous command successful• non zero if previous command not successful• chmod u-w testfile• cat > testfile• echo $?• 1
basic calculator
• bc• 1+2• 100/3• scale=5 (5 decimal places)• sqrt(2)
operations
• + addition• - subtraction• * multiplication• % integer remainder• ^ power
Functions
• sqrt • length• scale• (need option -l for library)• s, sine • c, cosine• a, inverse tanget• e, exponetial• l, natural logarithm. • echo "1+2" | bc• echo 2/3 | bc –l (-l is better!!!!)• .66666666666666666666
The Test Statement
• two ways• test arguments• [arguments]• [ -e ouput.txt ] && echo file exists• if the file exists, echo the message• [ -e ouput.txt ] || echo file not exist• if the file does not exist, echo the message
If…file exists
• test -e ouput.txt && echo file exists• if the file exists, echo the message• test -e ouput.txt || echo file not exist• if the file does not exist, echo the message
File operators used by test
• -d, if exists and is directory• -e, if exists• -f, if exists and is regualar file• -r, if exists and is readable• -s, if exists and has non-zero size• -w, if exists and is writable• -x, if exists and is executable
String operators used by test
• -n, if string has non-zero length• -x, if string has zero length• string, if string is not null• s1=s2, if strings are equal• s1!=s2, if strings are different.
Example – NAME not set
• $ [ "$NAME" ] || echo NAME not set• NAME not set• $ [ $NAME ] || echo NAME not set• NAME not set
Arithmetic Operators used by test
• -eq, equal • -ne, not equal• -gt, greater than• -ge, greater than or equal to• -lt, less than• -le, less than or equal to.
Example -eq and =
0 and 00 are numerically equal• $ [ 0 -eq 00 ] && echo equal• equal• 0 and 00 are different strings. • $ [ 0 = 00 ] && echo equal• $
White Space is important
• # [ "0" -eq "00" ] && echo equal• equal• # [ "0" -eq "00"] && echo equal• bash: [: missing `]'
IF statement
• if [ hell = hell ]• then echo equal• else echo not equal• fi• if [ hell1 = hell2 ]• then echo equal• else echo not equal• fi
For Loops
• for name in values• do• commands• done
For loop example
• for i in $( ls )• do • [ -r $i ] && echo $i is readable• done
While Loops
• while true• do• echo tick tock (this the sound of a clock)• date• sleep 1• done
Until Loops
• until command1• do• command2• done