Date post: | 27-Feb-2018 |
Category: |
Documents |
Upload: | dung-nguyen-phu |
View: | 234 times |
Download: | 0 times |
of 29
7/25/2019 16 Shell Programming
1/29
!"# %&'() *)+,,
-&./(0 1234 !2()
7/25/2019 16 Shell Programming
2/29
!"# %&'() 5)+,, ,6 0'
*)+,, ,6 %&'() %)7(0 89:) ,3() :;< !2(4=
-).>(0 %./(0 %?: @A2 (0.>2 8B(0 %)+C %D(0 :E4,3()F
*)+,, GH: ,3() %D I6( #)JK )CL: M,+ N)> )O% ()E( !2(4= %)P: )23( ,3()
*)+,, 5:&2#%
Q?: :)./(0 %&'() 5)+,,R I
7/25/2019 16 Shell Programming
3/29
*CO( @6 %)P: %)2 :)./(0 %&'() 5)+,,
*U 8V(0 KH2 %&'() 5CO( %)WC 8O(0 %+=%X @2R +K
7/25/2019 16 Shell Programming
4/29
hJ 8V 5)+,, G/( 02W(
!"# %&'(# My first shell scriptclear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc l
echo "Calendar"
! )*+,- .// %&'(
!01%&'(
7/25/2019 16 Shell Programming
5/29
i2a( %&C(0 5)+,,
-&C(0 !2(4= 5)+,, :j k ,CO2 I2a(X i2a( )3 %)l(0X
-OC &< @6 b4W( ,m In2 !2(4=F -[( I2a( ,6 Qop oqr
i2a( 8C (0.>2 8B(0 G9() (0)s< -OC &< @6 b4W( ,m In2 (0.>2 8B(0 -[( I2a( ,6 :)t %).>(0
u+K )CL: %&4c ()"# 02? %&9 :?: I2a(X v%[(fI2a( +:)C voqwx
+:)C vy*xzNrwx{)W2 :j 8_4 v %&.A: %[( I2a(
7/25/2019 16 Shell Programming
6/29
wY% 5l I2a( )3 %)l(0
System Variable Meaning
BASH=/bin/bash Our shell name
BASH_VERSION=1.14.7(1) Our shell version name
COLUMNS=80 No. of columns for our screen
HOME=/home/vivek Our home directoryLINES=25 No. of columns for our screen
LOGNAME=students students Our logging name
OSTYPE=Linux Our Os type
PATH=/usr/bin:/sbin:/bin:/usr/sbin Our path settings
PS1=[\u@\h \W]\$ Our prompt settings
PWD=/home/students/Common Our current working directory
SHELL=/bin/bash Our shell nameUSERNAME=vivek User name who is currently login to this PC
7/25/2019 16 Shell Programming
7/29
|9() (0)s< :?: I2a( :;< (0.>2 8B(0
Q} #)?#X
%[(fI2a(~02?f%&9
( 02? %&9 :;< I2a(
+:)C v%[(fI2a(
hJ 8VX
(C~]
+:)C v(C
7/25/2019 16 Shell Programming
8/29
4c %: GL% %[( I2a(
-[( I2a( #)W2 I% G4 I^(0 m %P
oqwx
**-xwfhxz*qN
(C
@+:)
)7(0 G.Z: G 8_4 :?:) )
7/25/2019 16 Shell Programming
9/29
4c %: GL% %[( I2a(
-[( I2a( :j #)E( I23% :)t )C(0 Q?: I2a( 5
7/25/2019 16 Shell Programming
10/29
hJ 8V
$ vi variscript
#
#
# Script to test MY knowledge about variables!
#
myname=Vivek
myos = TroubleOS
myno=5
echo "My name is $myname"
echo "My os is $myos"
echo "My number is myno, can you see this number"
7/25/2019 16 Shell Programming
11/29
!3() +:)C
Q} #)?#X+:)C C#C( 5%&2(0R @
7/25/2019 16 Shell Programming
12/29
Q?: #)# %C?( 5l )H:
| %)P: )23( :?: #)# () %C?( 5l )H: :( 8B(0 :E4,3()X
+=#& I24f%):f5lf)H:Q?: %C?( %UX eR R R gR
hJ 8VX+=#& ] e +=#& k ]+=#& ] g k+=#& k
+=#& ] #)# ()E( ,6 F+:)C +=#& e G?() 02? 02? %&9 I24 %): e @6 2( &
7/25/2019 16 Shell Programming
13/29
Q?: 8_4 (0CL:
1_4 (0CL: #
-_% :W :?: m %P %&C(0 8_4 (0CL: # Gd4 )7(0 :jm (0)s< () %C?(R %&D ()t(0 m %P 5
7/25/2019 16 Shell Programming
14/29
-&O(0 %)?2 a% %)}: :E4 ,3()
!2(4= KL: G9() %&W @dX -&O(0 %)?2 (a4 :E4 ,3() a% %)}: %)6() :7(0F )?: (a4 a% %)}: :j ,T2
2K %&< %&O(0 %)?2 a% %)}: KY% :E4 ,3() v X :)C I2a% %&O(0 %)?2 a% %)}: :E4 ,3() %&.A: Gj
hJ 8V&K 4((C]M,+Na4 )7(0 :j M,+ (6cR )3 %)l(0 %)7(0 I?C&KX :
7/25/2019 16 Shell Programming
15/29
QE4 ,3() GH: 8t ,234 G4 @6C
|H: 8t ,234 %D I6( #)JK @6 0)2 @6 I2a(
Q} #)?#X
z+
7/25/2019 16 Shell Programming
16/29
Q?: %)
7/25/2019 16 Shell Programming
17/29
Q_4 %&}: & ()?() 2
Q} #)?#Xif !i"u_ki#nthen
cu l#nh 1
fi
QE4 ,3() ] G.Z: %)P: )23( )2 G2d4f23( ,6 G}(0
)CL: %&O(0 %)?2 a% %)}: :;< G2d4f23( ,6 a%%)}: %)6() :7(0F
7/25/2019 16 Shell Programming
18/29
Q_4 %&}: & ()?() 2
hJ 8VR %3# 5)CM,+ :j (Y2 84(0X
-)P: %)2 %3#X
vFg5)CM,+ CC v] :)C 02? %&9 CC
#!/bin/sh
#
#Script to print file
#
if cat $1then
echo -e "\n\nFile $1, found and successfully echoed"
fi
7/25/2019 16 Shell Programming
19/29
Q_4 %&}: & ()?() 2 +,5+ M
Q} #)?#
2G2d4f23( %)+(
:E4f,3()f]
F
+,5+
:E4f,3()fk
M
7/25/2019 16 Shell Programming
20/29
!3() %+5%
!3() %+5% G.Z: 8B(0 G 2K %&< KY% I24 %): ,6G}(0 )(0 )Z# :\( ,O2
Q} #)?#X-+5% I24f%):I24 %):
i24 %): :j %) I
7/25/2019 16 Shell Programming
21/29
7/25/2019 16 Shell Programming
22/29
7/25/2019 16 Shell Programming
23/29
7/25/2019 16 Shell Programming
24/29
!3() %+5%
Q?: #)# %C?( ,C02:X
Nq-X
i24f%):
rN1X < i24f%):f] < I24f%):fk
qzX &
i24 %):f] & I24f%):fk
7/25/2019 16 Shell Programming
25/29
!3() %+5%
hJ 8V %3# 25#C52@+X
v Fg25#C52@+
! #$%&'( )* +,*)-.'
!/bin/sh
Script to see whether argument is positive
if test $1 -gt 0
then
echo "$1 number is positive"
fi
7/25/2019 16 Shell Programming
26/29
Q_4 %&}: ,L# C&
Q} #)?#for { variable name } in { list }
do
Cc cu l#nhdone
Ho$c:
C& +=#&]` +=#&k` +=#& 8CQ?: :E4 ,3()
8C(+ V d%t#p testforC& 2 2( ] k 8C
+:)C +,:CK+ v2 K+58C(+
7/25/2019 16 Shell Programming
27/29
Q_4 %&}: ,L# )2,+
Q} #)?#)2,+ :C(82C( 8C
:CKK
7/25/2019 16 Shell Programming
28/29
Q_4 %&}: :0$1?
888 88
6,%%0#799'*06
7/25/2019 16 Shell Programming
29/29
Q_4 %&}: :