150 likes | 390 Vues
Kabuk Programlama Shell Scripting(bash). Aslı Ergün. Shell Türleri. Bourne Bash Z-dhell C-shell TC-shell Korn Man csh yazarsak bilgi ediniriz. Hangi Kabuktayız?. Echo $SHELL yazarsak /bin/sh ise Bourne /bin/ksh93 ise Korn /bin/bash ise Bash /bin/zsh ise Z shell
E N D
Kabuk ProgramlamaShell Scripting(bash) Aslı Ergün
Shell Türleri • Bourne • Bash • Z-dhell • C-shell • TC-shell • Korn • Man csh yazarsak bilgi ediniriz.
Hangi Kabuktayız? • Echo $SHELL yazarsak • /bin/sh ise Bourne • /bin/ksh93 ise Korn • /bin/bash ise Bash • /bin/zsh ise Z shell • /bin/csh ise C shell • /bin/tcsh ise TC shell
Bash scripting dilidir. "Hello World" script ile baslayalım. Uzantısı .sh olacak bir dosya içine script yazılır. Dosyaya, • #!/bin/bash • echo "Hello, World " yaz • Mod değiştir chmoda+x /where/i/saved/it/hello_world.sh veya • chmod 700 /where/i/saved/it/hello_world.sh • /where/i/saved/it/hello_world.sh yazarak calıstır. • Pwd calisilan klasoru gosterir.
Değişkenler • #!/bin/bashSTRING="HELLO WORLD!!!"echo $STRING
Yönlendirme • ls > dosya.txt • sort < dosya.txt > sirali.txt • date >> sirali.txt
Değer Girme(read) • #!/bin/bash • clear • echo " Adınızı Girin" • read name • echo " Yasınızı girin" • read age • echo " Cinsiyet girin: K/E" • read sex • echo " Siz $age yasında $sex cinsiyetinde $name adlı kisisiniz« • Aynısı echo olmadan read komutu ile: • clear read -p "Please enter your name : " name read -p "Please enter your age : " age read -p "Please enter your sex. Male/Female : " sex echo "So you're a $age year old $sex called $name« • Temiz bosluklar basacak sekilde: • clear read -p "Please enter your name : " name echo "" read -p "Please enter your age : " age echo "" read -p "Please enter your sex. Male/Female : " sex echo "" echo "So you're a $age year old $sex called $name"Now we have an efficient and clean Bash script.
If Cumleleri • if [ $fruit = " elma " ] then echo " Elmalar..." elif [ $fruit = armut] then echo " Armutlar..." elif [ $fruit = muz ] then echo " Muzlar" else echo " Geriye Portakal kaldı!" fi • #!/bin/bashdirectory="./BashScripting"# bash check if directory existsif [ -d $directory ]; thenecho "Directory exists"else echo "Directory does not exists"fi
If-else • #!/bin/bash# Declare variable choice and assign value 4choice=4# Print to stdoutecho "1. Bash"echo "2. Scripting"echo "3. Tutorial"echo -n "Please choose a word [1,2 or 3]? "# Loop while the variable choice is equal 4# bash while loopwhile [ $choice -eq 4 ]; do# read user inputread choice# bash nested if/elseif [ $choice -eq 1 ] ; thenecho "You have chosen word: Bash" • else if [ $choice -eq 2 ] ; thenecho "You have chosen word: Scripting"elseif [ $choice -eq 3 ] ; thenecho "You have chosen word: Tutorial"elseecho "Please make a choice between 1-3 !"echo "1. Bash"echo "2. Scripting"echo "3. Tutorial"echo -n "Please choose a word [1,2 or 3]? "choice=4fi fifidone
Aritmetik Karşılaştırma • -lt < • -gt > • -le <= • -ge >= • -eq == • -ne != • #!/bin/bash • # declare integers • NUM1=2 • NUM2=2 • if [ $NUM1 -eq $NUM2 ]; then • echo "Both Values are equal" • else • echo "Values are NOT equal" • fi
String Karşılaştırma • = equal • != not equal • < less then • > greater then • -n s1 string s1 is not empty • -z s1 string s1 is empty • #!/bin/bash • #Declare string S1 • S1="Bash" • #Declare string S2 • S2="Scripting" • if [ $S1 = $S2 ]; then • echo "Both Strings are equal" • else • echo "Strings are NOT equal" • fi
For loop • #!/bin/bash# bash for loopfor f in $( ls /var/ ); doecho $fdone
While • #!/bin/bashCOUNT=6# bash while loopwhile [ $COUNT -gt 0 ]; doecho Value of count is: $COUNTlet COUNT=COUNT-1done
Until • #!/bin/bashCOUNT=0# bash until loopuntil [ $COUNT -gt 5 ]; doecho Value of count is: $COUNTlet COUNT=COUNT+1done
Fonksiyonlar • echoFunction() { echo "echo is Called" } fooBar() { echo "Functions are FUN!" } echoFunction; • fooBar; // fonksiyon çağırımı • echoFunction;