2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by...
-
Upload
annabella-chandler -
Category
Documents
-
view
213 -
download
0
description
Transcript of 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by...
![Page 1: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/1.jpg)
2nd Intro to Shell Scripting
![Page 2: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/2.jpg)
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
![Page 3: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/3.jpg)
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
![Page 4: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/4.jpg)
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
![Page 5: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/5.jpg)
Why need?
• Add users• List bad perms• Parsing log files• Startup of system (most of startup of os
uses scripts)• etc• etc• etc
![Page 6: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/6.jpg)
Variables
• Assignments (no spaces)I=0E=$((C+D))• Comparison ( needs spaces)I = 0X = A
![Page 7: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/7.jpg)
Input/Output/Assignment
• Input– read
• Output– Echo– printf
• AssignmentX=$INot X=I
![Page 8: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/8.jpg)
Comparisions
• Numeric– -eq, -ne, -lt, -le, -gt, -ge
• Strings– =, !=
• And is &&• Or is ||
![Page 9: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/9.jpg)
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?
![Page 10: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/10.jpg)
Sample Comparisons
• [ -f my_file ] (don’t forget spaces)• [ $x –ge 0 ]• [ $1 = start ]• [ “$1” = “” ] – empty string test!
![Page 11: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/11.jpg)
if then else
if [ $count –ne 0 ]thenavg=$((sum/count))
elseavg=0
fi
![Page 12: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/12.jpg)
for
for var in $@doecho $var
done
![Page 13: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/13.jpg)
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!
![Page 14: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/14.jpg)
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
![Page 15: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/15.jpg)
Advanced Shell Scripting
![Page 16: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/16.jpg)
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
![Page 17: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/17.jpg)
I/O Redirection• > = overwrite• >> = append
• < = read from a file• << = read stdin until keyword entered
![Page 18: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/18.jpg)
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
![Page 19: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/19.jpg)
while read issues?• Watch for spaces in input• IFS defaults to white space
![Page 20: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/20.jpg)
casecase $FOO in1)
FOO=$((FOO+1))echo FOO is now $FOO;;
*)FOO=0echo FOO is now $FOO;;
esac
![Page 21: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/21.jpg)
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
![Page 22: 2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight…](https://reader035.fdocuments.us/reader035/viewer/2022070616/5a4d1c0f7f8b9ab0599f622b/html5/thumbnails/22.jpg)
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