2 nd intro to shell scripting. dates for last week of class homework 7 – due tuesday 5/1 by...
Post on 20-Jan-2018
214 Views
Preview:
DESCRIPTION
TRANSCRIPT
2nd Intro to Shell Scripting
Dates for Last Week of Class• Homework 7
– Due Tuesday 5/1 by midnight• Labs 7 & 8 or late ones
– 8 is extra credit– Due Thursday 5/3 by midnight– 5/4 time stamps mean no grade!
• Final – Thursday 5/3– 6 – 9:30 in classroom– open [book|notes|computer]– will need working VM
Student Evaluations• The online evaluation system will be open for evaluation
of full term courses starting Friday, April 27th. Evaluations will close at 11:59pm, Sunday May 6th.
• Don't do them, grade get held hostage until May 23.• http://eval.nku.edu
Shell Script 101• List of commands interpreted by a shell
cd /var/tmpmkdir bokscd boksgzip -dc /var/tmp/boks_install.tar.gz | tar xf -
./install.jvss0001cd /var/tmprm -r boks*cd /etc/rc2.dmv S99boksm _S99boksm
Why need?
• Add users• List bad perms• Parsing log files• Startup of system (most of startup of os
uses scripts)• etc• etc• etc
Variables
• Assignments (no spaces)I=0E=$((C+D))• Comparison ( needs spaces)I = 0X = A
Input/Output/Assignment
• Input– read
• Output– Echo– printf
• AssignmentX=$INot X=I
Comparisions
• Numeric– -eq, -ne, -lt, -le, -gt, -ge
• Strings– =, !=
• And is &&• Or is ||
Comparisions II
• File testing:– -d – is the item a directory?– -e – does the file already exist?– -f – does the file exist and is it a regular file?– -h (or –L) – is the item a symbolic link?
(current user)– -r – does the file exist and is it readable?– -w – does the file exist and is it writable?– -x – does the file exist and is it executable?
Sample Comparisons
• [ -f my_file ] (don’t forget spaces)• [ $x –ge 0 ]• [ $1 = start ]• [ “$1” = “” ] – empty string test!
if then else
if [ $count –ne 0 ]thenavg=$((sum/count))
elseavg=0
fi
for
for var in $@doecho $var
done
Command Line Parameters
• Accessed via $# parameters– $0 is script name– $1, $2 … $9 = parameters on the command
line– $@ are all parameters– $# or $* is total number passed
• Assignments say receive as parameter, or list of parameters assume it’s this one!
Accessing Parameters#!/bin/bashsmall=99999999999#@ is all cmd line paramsfor num in $@do
if [ $num -lt $small ]then
small=$numfi
eoneecho the smallest is $small
Advanced Shell Scripting
whilesecretcode=agent007echo Guess the code!echo -e 'Enter your guess: \c'read yourguesswhile [ $secretcode != $yourguess ]doecho Good guess but wrong. Try again!echo -e 'Enter your guess: \c'read yourguess
doneecho Wow! You are a genius!!exit 0
I/O Redirection• > = overwrite• >> = append
• < = read from a file• << = read stdin until keyword entered
while readcat web_servers.txt | while read REMOTE_MACHINE
USER_PASSWORDdo
echo $REMOTE_MACHINE == $USER_PASSWORD scp update_solaris_www.sh
root@$REMOTE_MACHINE:/var/tmp/jdk14213postinstall-bld01
done
web_servers.txtweb1 password1web2 password2
while read issues?• Watch for spaces in input• IFS defaults to white space
casecase $FOO in1)
FOO=$((FOO+1))echo FOO is now $FOO;;
*)FOO=0echo FOO is now $FOO;;
esac
Accessing Files?
for var in `ls -1 /etc/rc5.d/S*`do echo -e “$var:\c” for lines in `cat $var` do echo $lines | grep ^# done | wc -ldone
Scripts and Awk#!/bin/bashsum=0for i in *do if [[ -f $i && -r $i ]] then
temp=`ls -l $i | awk '{print $5}'`
sum=$((sum+temp)) fidoneecho The readable files in this directory are $sum in size
top related