Professional Documents
Culture Documents
Linux Shellscript Tutorial
Linux Shellscript Tutorial
- 2005-2006 -
shell scripts ;
shell script
. shellscipt
: #!/bin/bash
(bash). !
! test.sh
" :
#!/bin/bash
E#mkdir test
cp .login test
echo “h antigrafh egine”
# $ ! 755
root ./test.sh :
! test
.login test
" : H antigrafh egine
$ :
% " !
.
. X=”abc”
. echo $X . & " :
#!/bin/bash
$'=“ABC”
Eecho “$X” # kalo einai ean prokeitai gia metavlith keimenoy na
x#xrhshmopoiounatai eisagwgika
% ABC .
( echo
" . & echo –e
:
To \n
\t " tab
To \b backspace
To echo –e “12\t3\n456\b” ! :
12 3
45
K " "
":
!
#!/bin/bash
echo "Gia sou , $USER. Tha sou kanw mia lista me ta arxeia sou"
echo "Sou kanw lista me ta arxeia tou fakelou, $PWD"
ls
% $USER $PWD .
) :
PWD )
!
SHELL=/bin/bash shell
USERNAME "
! !
O
" :
!
#!/bin/bash
$#=”ls”
$B=”-l”
echo “$# $+ *.exe"
# ! : ls –l *.exe
: metavlhth="$(entolh)" x=”$(ls)
ls.
redirect . $ls>test.txt
ls
test.txt., !
! ! . -
: $ls>>.login.
, ! ! " input
" $ : $cat>test.txt.
! redirect
" $ : $ tr "[a-z]" "[A-Z]" <
mikra_grammata.txt >megala_grammata.txt !
If…fi
,
$ :
if synthikh1
then
endoles
..........
elif synthikh2
then
endoles
........
elif synthikh3
then
endoles
........
fi
$ "
" :
) ( ) !
-n
!
) ( ) !
-z
-s & !
-f (file) .
-d .
(directory)
-w , /
-r & !
-x &
. !
-d
. !
-f
)
-eq
-neq equal
)
=
!= # =
) 1
-lt
) 1
-gt
) 1
-ge
) 1
-le
! 0 NOT
-a 0 AND
-o 0 OR
& " :
! 1
#!/bin/bash
X=""
if [ -n "$X" ]; then
# to -n elenxei ean to x den exei mhdeniko megethos
echo "To X den einai keno"
else
echo “To x einai keno”
fi
# “To x einai keno”
! 2
#!/bin/bash
read X
read Y
if [ $X –nq $Y ] ; then
echo "X aniso toy Y"
fi
(loops)
, / !
. . $ ! / " :
For loops
) / for " $ :
! 1
#!/bin/bash
colour1="red"
colour2="light blue"
colour3="dark green"
for X in "$colour1" $colour2" $colour3"
do
echo “$X ,”
done
#!/bin/bash
for X in *.tar
do
gzip $X
done
# tar
& $ $ :
#!/bin/bash
for (( i = 0 ; i <= 5; i = i + 1 ))
do
echo $i
done
" " i 0
5
1. - " !
1 " 20.
While loops
( $ :
#!/bin/bash
X=0
while [ $X -le 20 ]
do
echo $X
X=$((X+1))
done
# 1 20
, " :
Onoma_synarthshs ( )
{
entoles
return
}
1
! . ,
: Onoma_synarthshs.
#!/bin/bash
$ SayHello()
{
echo "Hello $LOGNAME, Have nice computing"
return
}
$ SayHello()
' " :
- test.txt ! :
Nikos 1
Kostas 2
Maria 3
Eleni 4
- test.txt :
To /tea/
, $ s//milk
g (global) !
.
/tmp/result.tmp.$$ .
ex
, $ ex onoma_arxeiou
. "
" " . &
:
( p :
, :2 p 2 .
, :2,5 p 2 5 .
, :2,$ p 2
.
, :set number
:2 p
.
( d :
, :2 d 2 .
, :2,5 d 2 5 .
( co :
, :1,4 co 1 " 4
.
H / $ $ :
, :/linux/p
$ linux.
, :g/linux/p
$ linux.
( :w .
( :q ex.
Shell script !
terminal. #
cd .rmp
" ! ! :
# mount /mnt/cdrom
# cd /mnt/cdrom/RedHat/RPMS
# rpm -ivh dialog-0.6-16.i386.rpm
" $
:
Message
Msg box
Yes/no dialog
Input
na=`cat /tmp/input.$$`
case $sel in
0) echo "H metavlith pou phrame $na" ;;
1) echo "paththike cancel" ;;
255) echo "paththike esc" ;;
esac
rm -f /tmp/input.$$
Select
case $menuitem in
epilogh1) echo “epilexthike to 1”;;
epilogh2) echo “epilexthike to 2”;;
epilogh3) echo “epilexthike to 1”;;
esac
$ "
! ! ! :
chmod +x arxeio
# "
, sh [options] file
" :
-v ! " input.
-x
shell script.