You are on page 1of 11

:

- 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 .

) :

System Variable Meaning


BASH=/bin/bash shell
BASH_VERSION ( ! shell
HOME home directory
PATH=/usr/bin:/sbin:/bin:/usr/sbin * !

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 !

mikra_grammata.txt, " output


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

# '=1 .=2 “X aniso toy Y”

(loops)

, / !
. . $ ! / " :

For loops

) / for " $ :

for X in (kapoia stoixeia)


do
entoles
done

! 1

#!/bin/bash
colour1="red"
colour2="light blue"
colour3="dark green"
for X in "$colour1" $colour2" $colour3"
do
echo “$X ,”
done

# : red , light blue , dark green


! 2

#!/bin/bash
for X in *.tar
do
gzip $X
done

# tar
& $ $ :

for (( arxikh timh; Synthikh ; apotelesma ))


do
entoles pou epanalambanontai
Done

#!/bin/bash
for (( i = 0 ; i <= 5; i = i + 1 ))
do
echo $i
done

" " i 0
5
1. - " !
1 " 20.

While loops

( $ :

while [ isxyei h synthikh ]


do
entoles
done

#!/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

1 $cut –f1 test.txt


1 ! $cut –f2
test.txt 2 .

- test.txt :

India's milk is good.


tea Red-Lable is good.
tea is better than the coffee.

! $ sed '/tea/s//milk/g' teaormilk >


/tmp/result.tmp.$$ :

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

dialog --title "titlos" --backtitle "sxolio grammi 1\


allagh grammhs. . . " 7 50 ; read

Msg box

dialog --title "titlos" --backtitle "sxolio… " 9 50

Yes/no dialog

dialog --title "Titlos" --backtitle "sxolio" 7 60


sel=$?
case $sel in
0) echo "H apanthsh einai nai";;
1) echo "H apantish einai oxi";;
255) echo "O xristis pathse esc";;
esac

Input

dialog --title "titlos" --backtitle "sxolio" --inputbox "Edw


grafoume" 8 60 2>/tmp/input.$$

#me ayto kleinoyme to dialogo


sel=$?

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

dialog --backtitle "titlos " --title "titlos menu" --menu "sxolio


epilogwn" 15 50 3\
epilogh1 "sxolio"\
epilogh2 " sxolio "\
epilogh3 "sxolio" 2>/tmp/menuitem.$$
menuitem=`cat /tmp/menuitem.$$`
opt=$?

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.

You might also like