Download presentation
Presentation is loading. Please wait.
Published byAnnabella Chandler Modified over 8 years ago
1
2 nd Intro to Shell Scripting
2
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
3
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
4
Shell Script 101 List of commands interpreted by a shell cd /var/tmp mkdir boks cd boks gzip -dc /var/tmp/boks_install.tar.gz | tar xf -./install.jvss0001 cd /var/tmp rm -r boks* cd /etc/rc2.d mv S99boksm _S99boksm
5
Why need? Add users List bad perms Parsing log files Startup of system (most of startup of os uses scripts) etc
6
Variables Assignments (no spaces) I=0 E=$((C+D)) Comparison ( needs spaces) I = 0 X = A
7
Input/Output/Assignment Input –read Output –Echo –printf Assignment X=$I Not X=I
8
Comparisions Numeric –-eq, -ne, -lt, -le, -gt, -ge Strings –=, != And is && Or is ||
9
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?
10
Sample Comparisons [ -f my_file ] (don’t forget spaces) [ $x –ge 0 ] [ $1 = start ] [ “$1” = “” ] – empty string test!
11
if then else if [ $count –ne 0 ] then avg=$((sum/count)) else avg=0 fi
12
for for var in $@ do echo $var done
13
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!
14
Accessing Parameters #!/bin/bash small=99999999999 #@ is all cmd line params for num in $@ do if [ $num -lt $small ] then small=$num fi eone echo the smallest is $small
15
Advanced Shell Scripting
16
while secretcode=agent007 echo Guess the code! echo -e 'Enter your guess: \c' read yourguess while [ $secretcode != $yourguess ] do echo Good guess but wrong. Try again! echo -e 'Enter your guess: \c' read yourguess done echo Wow! You are a genius!! exit 0
17
I/O Redirection > = overwrite >> = append < = read from a file << = read stdin until keyword entered
18
while read cat web_servers.txt | while read REMOTE_MACHINE USER_PASSWORD do echo $REMOTE_MACHINE == $USER_PASSWORD scp update_solaris_www.sh root@$REMOTE_MACHINE:/var/tmp/jdk14213postinstall- bld01 done web_servers.txt web1password1 web2password2
19
while read issues? Watch for spaces in input IFS defaults to white space
20
case case $FOO in 1) FOO=$((FOO+1)) echo FOO is now $FOO ;; *) FOO=0 echo FOO is now $FOO ;; esac
21
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 -l done
22
Scripts and Awk #!/bin/bash sum=0 for i in * do if [[ -f $i && -r $i ]] then temp=`ls -l $i | awk '{print $5}'` sum=$((sum+temp)) fi done echo The readable files in this directory are $sum in size
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.