Вы находитесь на странице: 1из 15

http://linuxnews.persianblog.com + mohammad@ostadhashemi.

com
Shell
Shell
! "# $ " % & '( Shell
# $& ) * # + , ( - . ) " / *0 1 + 0
2 & &3 , & & -! "
- ) $4 5 "( ) 6 ( &) * , 0
5 9 : 9 7 3 6 - 5 "! 8
*2 = <Shell Script; 5 > ? : ( 5 "! 8 =<Shell Programming;
' A - @ ) ! 8 9 - 9 > ?
DB -5 C
D& 0 E@ F8 " G -5 & *H. I
bash ( Bourne – Again Shell )
csh ( C Shell )
tcsh
ksh ( Korn Shell )

5 JK ? ) * = bash : & A G - 5 &


? $cat/etc/Shells D ! "# $ & 9, B9
& F* ) * !8 & bash 6. 9 L
) * ! "# $ $echo $BASH_VERSION

# $man bash " ! bash

bash shell $ %
M M- N Salam 3 E@ B I ) > ? ( FI
salam.sh P 9 gEdit Vi 5 O %C ( ( ,
) $4
bin/bash
salam.sh
echo "Salam
- R0 ! 8 Mode G 5 5 " G HQ
) * chmod & 0 7
$chmod +x salam.sh D T 0 Xtrem ) S '

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
) $4 5 " H+ ! - 0
./salam.sh D T 0 9 " B- 5 " ) 5 5 " 1Q
- $) N Salam 3 E@ ) 5 @ 2
B ) , B E B 2 U V@ 1Q
+W 0 K ( K 2 5 ) I # 1 #$
MG M !8 & G 2 !8 & G <Y; U S G0 X@ <Comment;
M0 M- bash MG M$ ! M "# M$ M9 * 5 + 1 #$ U S G0 X@ & G
) * S 5 I ,& 3 V B9
whereis bash D ! "# $ bash 5 + ! F"
M% M M 3 M@X[ M% .9 ) P +W 0 K ( Z P #$
G !
& G ^W echo "Salam"; P #$ \ B ) PS 0]
M$ P 2 XO _ , +*` 0 E@ - echo
_ ,P ! - 9 Salam

& '
(M E C' - R & F H P 0 ! &( ! @ -5 bash
G0 + 2 bash 7 "Q ? (variable) R
(user-defined variable) M # M 0) a G0 R ( I & !8
% 7 "Q & 9 !8 Z, , %B b0 )
M MF & U MHd M ! 0 $ P ) $4 R - $ c "
!8 V > R S ) (String) R
myname=mohammad D 9 XO - E9
mohammad ! 8 ) ) E9 V myname R P S

#!/bin/bash
#name.sh
myname="mohammad"
echo $myname

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
count B M- MS ; count=56 D M- MR @ V ( 0 - . 2
< R ) ) E9 V 56 R
M& M ) * M ! M0 R P ! @ @e " Q - & bash
M ) *M !8 & >M ) a G0 R 9 P&A -5 bash
) a G0 R I C++ Visual Basic 5 O 9 F & $ XO
9 P&A f - bash ) * !8 & >
B - M. M2 G ! E9 H ? ) R I D ?
DB 9 % B- E9 myname R mohammad P
myname="mohammad"
B ) * !8 V & R I ? B- ?
3 M` M ) * & R P & 5E R + "2 bash
M MR M+ MI ) 2 R V - $ 2 5
$myname D 9
M' M , ( & 5 " ! "( H -2 ) Q
D - $) - C 5 @ ) *
#!/bin/bash
#parameters.sh
echo "First para = $1"
echo "Second para = $2"

G _ , F8 + B $5 & 9- R 2 1
3 ME@ < parameters.sh hello man D M9 MG ; M ' ( 2
D - $ &
First para = hello
Second para = man

DB "G - R I B- 9 $8
a G0 H - R & D (User Variables) & '
R0 0 > ? -5 "
P&A 9- B 9 # + & C. - D (Environment Variables) ( ) & '
) * % R - 5$ 0 F8 a G0 F8 9
a G0 8 ! 0 & 3 ` PATH 5 O R0 0 F8 & $ 2
R0

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
$ B9 H - R D (Built-in Variables) *! & '
R0 0 g - 2

iG E h B9 # 0 K + - R H$ - R B *2 K -
9 - R - R R05 B- i G R0! 0 F8 &
F & , S 9- ) a G0 B 9 2 (System Variables)
D ) ) -1 O 9 - R

9 R < ; G
BASH = /bin/bash ) * 5 P
BASH_VERSION = 1.14.7(1) ) * 5 .9 )
COLUMNS = 80 N + *` ! )
HOME = /home/yourusername < H` ; $
LINES = 25 N + *` ! )
LOGNAME = student < loggin name ; P
OSTYPE = Linux 5 @B 9
PS1 = [\u@\h\w]\s prompt 3 7 0
PWD = /home/yourusername/temp
SHELL = /bin/bash ) * 5 P
USERNAME = yourusername < user name; ) pc 9 P

- 0 XO ) * 0 *H . -3 ` - R &
D % $B 9E A 1 3 @X[ & (
$echo $OSTYPE < _ , 5 @B 9 I ;
$echo $HOME < - ! C $ ;

0X?C ! ? ! , $ 9 - R ! R0& \ Q D +,
8N '

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
D & ' ./ , -
< ; X@ F 0 @ k Q5 0 I E* k Q R P j
) * Space & E R V R P l
3 FH "P 5 O 9Q( , n Z k Q - R m
D XO a G0p ' - R 0 o
$vech= < ) * Z & R a G0 ;
$vech=""
) * R P E) d *,r D5O hX@ & q

read 0 1 2 30 4 & /*5


D W - %H @
B ) * W - %H @ & B 0 - 9 % - bash
D5 ?
$expr 1 @ W %H @ P @
$expr 6+3
D& 0 E@ bash ) * 5 - %H @
+
- s *0
/ B 9 V0
% )
\* t W <* \* t W X@;
D 9 _ , echo -5 @ 5` Q - . 2
echo `expr 6+3`
A G <`; ) ] 2 echo & G k [ X@ 0
~ ! - tab
D 9 2 XO 3 U % - iG X@
echo "today is date"
D 9 2 - $_ , H - V
echo "today is `date`"
_ ,& ! -u 0! 8 H ) _ , today is 3 E@

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
"6 4 7
@ 2 ? Q $? D B 9 2 ( - ! '& G
! v '5 O ) PS V" G *` - B9
@( <$?; t ) ) N @ 2 ( U b 5 "(
) C PS 7 5 @ ) ! ' V" HE G *` d

" read 0
) * R ( ! 8 ) $4 & 3 @X[ "2 read
D ) * 3 `
read 1 R ,P R , , $8 R
D 0 &
$vi sayH
#script to read your name
echo "what is your first name?"
read fname
echo "Hello $fname, be lucky"
D !8 5 ?
$chmod 755 sayH
$./sayH
$P ! & ' ; 9, (, P ' &
PX > < Z enter H
D - u 'B 9 mohammad $P 2 XO
Hello mohammad, be lucky

bash shell !8 1 7
long_story 5 " B c " ; $ls long_story D ! "# $
D E 5O ' U < E
long_story : No such file or directory
B 1Q 5 G ( ls K -
r " *0 , B
% Z, - ! "P C - <ls; ! "# $ H
!8 & G "2 7 ! " - C -! 2 8! @ !8 & G
$tail +10 myf D XO

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
9- !8 -! 2 8 myf +10 ! "P tail S

D F8 -! 2 8 BF -! "& iG
! " <$#; - ! 28 G0 [ -! 28
ls 1 foo
cp 2 y y.bak

mv 2 y.bak y.okay

tail 2 -10 myf


mail 1 raj
sort 3 -r -n myf
date 0

clear 0

X@ % $ ! "# $ ) G0 - ! 28 G0 $# X@ D 0
% <) 9 0; ) ) * -! 28 - Z $* , $@ -

> ? 5 P S $myshell foo bar D % 7 ! "# $


bar ! 28 foo ! "# $ ! 28 "myshell"

5 @ 2 B G 3 ! "# $ -! 28 B- . 2 shell
DB
Myshell $0
foo $1
bar $2
*2 A <$0 , $1 , $2 , …; - $* & 7

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
D ) -C -! 28 % %, 9 & 1O
$ vi demo
#!/bin/sh
# command line args
echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* or $@"

$chmod 755 demo D - R0 % mode


$./demo Hello World D 8

G > ? ! , % ) $4 $B 9 bin > ?


- $ P&A

& *9 % 5:
9 ! 0 F8 # 0 - - VHQ [ 3X &) * ! ? Bash
DB S F8 & $ R0
" if 0
if …. then …. else …. fi
\ 2 # Hd !8 8 E 0 - $ E + \ ( H
- $P S % E ^ +`
# M+ 3 M M M ) * [ 3X & - )& -5 U Hd
M ! M 5 [ F90 0 - )& [ 3X Z" 9
D 5? if H U 0 PS 5
if \
then j
elif \
then l
else m
fi [finished]

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
3 E@ & test & ) * - R + 1 test P "& bash
M M ) "! 8 - ' 9 (false) # Hd (true) ! 2&
) * ! 0 - > ?
$name P M M R mohammad P 8 E 9 - $ c "
"you're not 3 ME@ M M7 M M+ M 0 M` M$ ) ) $4
w $ ) _ , mohammad"
#! /bin/bash
# if then else loop to test value of $name variable.
if test"$name"="mohammad"
then
echo "hi mohammad"
else
echo "you're not mohammad"
fi

E - !C if … then … else &) * & [ )0


PS ! 0 E 9 - 3 &) * ) 1 O ( # V"
" while 0
) * ` $\ & 0 & C. ?0
( % "superuser" H! 5 V H b c " 2
% Z, $ & H 9 2 )
while … do … done D 5? while U 0
D 3 ` *2 A O
#! /bin/bash
# chck login name
echo "enter your name :"
logname="superuser"
while test $logname="superuser"
do
echo –n "Please Enter Another Login Name."
read logname
done

' root - . ( %- > ? 5 " E


M M Z % [ - VHQ ) VHQ 1 O ( # V"
) ^ W 0! - F8 0 ' ) * F8 & ! 0

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
bash & *9
& , b . [ 1 Q - VHQ if … then … fi 3
DB - x - VHQ \ bash 3
DB 5 @ &3 ` B ) * 0 03 ` if ,& B - . 2 j
if \
then
if \
then
3

else
3

fi
else
3
fi

- VHQ ( *2 3 & - 2 loop D 6 ; +0 < % & loop l


7 \ 0 -
while loop <B for loop <A D loop I Bash

D ) * 5? D for loop <A


for R P in < @ S ; 9
do
( 0( 0 3 3 9
! ' @ S 9 i@ 0 9 i@
done

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
D ) * 5? D while loop <B
while \
do
j
l
m

done
?0 VHQ 7 \ 0

& t 3 - & ? B- D case 0 m


write read & ) * & &) * ) * R ( *H. V &
0 "` 0! 8 9
< ! " G command; D ) * 5? case
case R P in
j % ; Command
….
….
Command ;;
l % ; Command
….
….
Command ;;
m % ; Command
….
….
Command ;;

esac

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
(de-bug) 6 ; +0 < % 1 = +%
8 0 V [ <Shell Programming; 5 9 N ' F
3 A? <error; 0X?C , 5 9 3 -2
"
) * bash sh 3 ) - –x –v - Z2 & 0 7
D 5? &) * H) +
sh Z2 I > ? 5 P

bash Z2 I > ? 5 P

V ) _ ,) Z2 ) * –x Z2 & &1 ? 2
N ' $ 0 9 B- - R
9 & <1 O
$ cat > debug.sh
#!/bin/bash
#
# Script to show debug of shell
#
tot=`expr $1 + $2`
echo $tot
D q o % !8
$ chmod 755 debug.sh
$ ./debug.sh 4 5
& 1 ? –x Z2 !8 2 - $y $3 `
D - $
$ sh -x dsh1.sh 4 5
#
# Script to show debug of shell
#
tot=`expr $1 + $2`
expr $1 + $2
++ expr 4 + 5
+ tot=9
echo $tot
+ echo 9
9
) * ) f' U F> ? ! debug Z –v Z2 &

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
## 6 ; +0 < % 1 >?@

- ! C pm am t 9 Q ! &

# script to display time in am/pm format using "expr" command.


# we are using the back quote characters (`) to capture the output
# of the date command and assign that output to shell variables.

hours=`date +'%k'`
mins=`date +'%M'`
if [ $hours -eq 0 ]
then
hours=12
fi
if [ $hours -gt 12 ]
then
hours=`expr $hours - 12`
ampm=pm
else
ampm=am
fi
echo $hours:$mins $ampm

case &) * - PS HE

# script to display time in am/pm format using "case" statement

hours=`date +'%k'`
mins=`date +'%M'`
if [ $hours -ge 12 ]
then
case $hours in
13) hours=1 ;;
14) hours=2 ;;
15) hours=3 ;;
16) hours=4 ;;
17) hours=5 ;;
18) hours=6 ;;
19) hours=7 ;;
20) hours=8 ;;
21) hours=9 ;;
22) hours=10 ;;
23) hours=11 ;;
esac
ampm=pm
else
case $hours in
0 ) hours=12 ;;
esac
ampm=am
fi
echo $hours:$mins $ampm

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com
* EO @ z .C

$ vi isnump_n
#!/bin/sh
#
# Script to see whether argument is positive or negative
#
if [ $# -eq 0 ]
then
echo "$0 : You must give/supply one integers"
exit 1
fi

if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi

- HV 5 0@X[ ) case if…then…fi

$ cat > car


#
# if no vehicle name is given
# i.e. -z $1 is defined and it is NULL
#
# if no command line arg

if [ -z $1 ]
then
rental="*** Unknown vehicle ***"
elif [ -n $1 ]
then
# otherwise make first arg as rental
rental=$1
fi

case $rental in
"car") echo "For $rental Rs.20 per k/m";;
"van") echo "For $rental Rs.10 per k/m";;
"jeep") echo "For $rental Rs.5 per k/m";;
"bicycle") echo "For $rental 20 paisa per k/m";;
*) echo "Sorry, I can not gat a $rental for you";;
esac

http://linuxnews.persianblog.com + mohammad@ostadhashemi.com

Вам также может понравиться