2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by...

22
2 nd Intro to Shell Scripting

description

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.

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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