0% found this document useful (0 votes)
114 views

Shell Scripting

Shell scripting

Uploaded by

sharmaisha0902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

Shell Scripting

Shell scripting

Uploaded by

sharmaisha0902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

UNIX Shell-Scripting UNIX Shell-Scripting

With focus on bash


BINP14 Bjrn Canbck
Outline Outline

What is a shell? A shell script?

Introduction to bash

Running Commands
BINP14 Bjrn Canbck
What is a shell? What is a shell?
BINP14 Bjrn Canbck
A Unix shell is a command-line interpreter or shell that proides a
traditional !ser inter"ace "or the Unix operatin# s$stem and "or
Unix-like s$stems% Users direct the operation o" the comp!ter b$
enterin# commands as text "or a command line interpreter to
exec!te or b$ creatin# text scripts o" one or more s!ch commands%
&o!rce' http'((en%)ikipedia%or#()iki(Unix*shell
What is a shell? What is a shell?
Inp!t +&,-IN.
shell
o!tp!t +&,-/U,. error +&,-011.
Common Shells Common Shells

Bash (/bin/bash) Bourne again shell

C Shell (/bin/csh)

Turbo C Shell (/bin/tcsh)

orn Shell (/bin/!sh)


BINP14 Bjrn Canbck
What is bin ? What is bin ?

/bin

/usr/bin

/usr/local/bin

/home/bjorn/bin
BINP14 Bjrn Canbck
What is a shell script? What is a shell script?

A te"t file

With instructions

#"ecutable
BINP14 Bjrn Canbck
What is a Shell Script? What is a Shell Script?
% cat > hello.sh <<HERE
#!/bin/sh
echo 'Hello world!'
HERE
% chmod +x hello.sh
% ./hello.sh
Hello world!
What is a Shell Script? What is a Shell Script? A Text File A Text File
$ cat % hello&sh ''(#R#
)*/bin/sh
echo +(ello ,orld*+
(#R#
$ chmod -" hello&sh
$ &/hello&sh
(ello ,orld*
BINP14 Bjrn Canbck
What is a Shell Script? What is a Shell Script? o! To "un o! To "un
% cat > hello.sh <<HERE
#!/bin/sh
echo 'Hello world!'
HERE
$ chmod -" hello&sh
$ &/hello&sh
Hello world!
BINP14 Bjrn Canbck
What is a Shell Script? What is a Shell Script? What To #o What To #o
% cat > hello.sh <<HERE
#!/bin/sh
echo 'Hello world!'
HERE
% chmod +x hello.sh
% ./hello.sh
Hello world!
BINP14 Bjrn Canbck
What is a Shell Script? What is a Shell Script? $xecutable $xecutable
% cat > hello.sh <<HERE
#!/bin/sh
echo 'Hello world!'
HERE
% chmod +x hello.sh
% ./hello.sh
Hello world!
BINP14 Bjrn Canbck
What is a Shell Script? What is a Shell Script? "unning it "unning it
% cat > hello.sh <<HERE
#!/bin/sh
echo 'Hello world'
HERE
% chmod +x hello.sh
% ./hello.sh
(ello ,orld*
BINP14 Bjrn Canbck
Fin%ing the program& 'AT Fin%ing the program& 'AT

% ./hello.sh

% echo !"#H
/bin$/usr/bin$/usr/local/bin$
/home/bjorn/bin

% which echo
/usr/bin/echo
BINP14 Bjrn Canbck
(ariables an% the en)ironment (ariables an% the en)ironment
% hello.sh
bash$ hello.sh$ %ommand not &ound
% !"#H'(!"#H$.)
% hello.sh
Hello* world
BINP14 Bjrn Canbck
"e%irection "e%irection
echo he. % test&t"t
echo / he.0 %% test&t"t
#"pert users onl12
cat ' test&t"t
cat ''I345T
Some input
I345T
test&sh 6% m1#rror
te"t&sh% m1#rrorAnd7ut 6%89
BINP14 Bjrn Canbck
#"pert users onl12
error 2
inp!t 3
pro#ram
o!tp!t 1
*uoting *uoting
% echo '+,ER'
+,ER
% echo (+,ER)
bjorn
% echo +,ER
bjorn
% echo -)
)
% echo ->
>
BINP14 Bjrn Canbck
o! to learn o! to learn

man

man bash

man cat

man man

Learning the Bash Shell: 6


nd
#d&

/Bash Reference0 Cards


Continuing lines& + Continuing lines& +
% echo #his -
.s -
" -
/er0 -
1on2 -
%ommand 1ine
#his .s " /er0 1on2 %ommand 1ine
%
BINP14 Bjrn Canbck
,a-e .our /i0e $asier ,a-e .our /i0e $asier

TAB completion

Control-R

Control-S
'ipes 'ipes

;ots of ;ittle Tools


echo (Hello) 3 -
wc 4c
INPUT
echo
OUTPUT ERROR
0
1 2
INPUT
wc
OUTPUT ERROR
0
1 2
A Pipe!

4ollo)in# is onl$ i" $o! )ant to learn more
$xit status 1expert users2 $xit status 1expert users2

< is True
% ls /does/not/exist
% echo 5
6
% echo 5
7
BINP14 Bjrn Canbck
$xit status& 1expert users2 $xit status& 1expert users2
% cat > test.sh <<8#E,#8
exit 9
8#E,#8
% chmod +x test.sh
% ./test.sh
% echo 5
9
/ogic& test 1expert users2 /ogic& test 1expert users2
% test 6 4lt 67
% echo 5
7
% test 6 '' 67
% echo 5
6
/ogic& test 1expert users2 /ogic& test 1expert users2

test

: ;

: 6 <lt 67 ;

:: ;;

:: (this strin2) '= (this) ;;

>> ??

>> 6 < 67 ??
/ogic& test 1expert users2 /ogic& test 1expert users2

: 4& /etc/@asswd ;

: ! <& /etc/@asswd ;

: 4& /etc/@asswd <a <& /etc/shadow ;

: 4& /etc/@asswd <o <& /etc/shadow ;


An asi%e& An asi%e& >> ?? >> ?? 0or ,ath 1expert users2 0or ,ath 1expert users2
% echo >> 6 + A ??
9
% echo >> A B 9 ??
C
% echo >> 6 / 9 ??
7
/ogic& i0 1expert users2 /ogic& i0 1expert users2
i& something
then
$
# (eli&) a contraction o& (else i&)$
eli& something-else
then
$
else
then
$
&i
/ogic& i0 1expert users2 /ogic& i0 1expert users2
i& : +,ER <eD (borwicjh) ;
then
$
# (eli&) a contraction o& (else i&)$
eli& ls /etc/oratab
then
$
else
then
$
&i
/ogic& i0 1expert users2 /ogic& i0 1expert users2
# see i& a &ile exists
i& : 4e /etc/@asswd ;
then
echo (/etc/@asswd exists)
else
echo (/etc/@asswd not &ound!)
&i
/ogic& 0or 1expert users2 /ogic& 0or 1expert users2
&or i in 6 A 9
do
echo i
done
/ogic& 0or 1expert users2 /ogic& 0or 1expert users2
&or i in /B
do
echo (1istin2 i$)
ls 4l i
read
done
/ogic& 0or 1expert users2 /ogic& 0or 1expert users2
&or i in /B
do
echo (1istin2 i$)
ls 4l i
read
done
/ogic& 0or 1expert users2 /ogic& 0or 1expert users2
&or i in /B
do
echo (1istin2 i$)
ls 4l i
read
done
/ogic& C-st3le 0or 1expert users2 /ogic& C-st3le 0or 1expert users2
&or >> ex@r6 E
ex@rA E
ex@r9 ??
do
list
done
/ogic& C-st3le 0or 1expert users2 /ogic& C-st3le 0or 1expert users2
1.F.#'67
&or >> a'6 E
a<'1.F.# E
a++ ??
do
echo <n (a )
done
/ogic& !hile /ogic& !hile
while something
do
$
done
/ogic& !hile /ogic& !hile
a'7E 1.F.#'67
while : GaG 4lt G1.F.#G ;
do
echo 4n Ga )
a'>> a + 6 ??
done
Counters Counters
%H+I#ER'7
while : 4e (J.1E.%H+I#ER) ;
do
%H+I#ER'>> %H+I#ER + 6??
done

3ote2 race condition


"eusing Co%e& 4Sourcing5 "eusing Co%e& 4Sourcing5
% cat > /@ath/to/m0/@asswords <<8!K8
J#!8+,ER'(sct)
8!K8
% echo J#!8+,ER
% . /@ath/to/m0/@asswords
% echo J#!8+,ER
sct
%
(ariable ,anipulation (ariable ,anipulation
% J.1E!"#H'/@ath/to/m0/[email protected]
% echo J.1E!"#H
/@ath/to/m0/[email protected]
% echo LJ.1E!"#H%.lisM
/@ath/to/m0/out@ut
% echo LJ.1E!"#H#B/M
@ath/to/m0/[email protected]
% echo LJ.1E!"#H##B/M
[email protected]
"unning 'rograms "unning 'rograms
"easons 0or "unning 'rograms "easons 0or "unning 'rograms

Chec! Return Code

=et >ob 7utput

H+#!+#'Necho (Hello)N

H+#!+#'>echo (Hello)?

Send 7utput Some,here

Redirection2 <: >

4ipes
$mail Noti0ication $mail Noti0ication
% echo (Fessa2e) 3 -
mail <s (HereOs 0our messa2e) -
borwicjhPw&u.edu
#ates #ates
% Q"#E,#R.IR'Ndate +%S%m%dN
% echo Q"#E,#R.IR
A77C76AT
% man date
FT' the ar% Wa3 FT' the ar% Wa3
&t@ <n <u serUer.w&u.edu <<8J#!8
user username password
@ut J.1E
8J#!8
FT' !ith FT' !ith w2et w2et

w2et -
&t@$//[email protected]&u.edu/&ile

w2et <r -
&t@$//[email protected]&u.edu/dir/
FT' !ith FT' !ith curl curl
curl <# u@load4&ile -
4u username$@assword -
&t@$//serUer.w&u.edu/dir/&ile
Searching& Searching& &ind &ind
$ &ind /home/borwicjh -
4name VB.lisO
[all files matching *.lis]
% &ind /home/borwicjh -
4mtime 46 <name VB.lisO
[*.lis, if modified within 24h]
% man &ind

You might also like