You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#!/bin/bash
clear
echo “Bugun tarihi ‘date’"echo~stepp/cowsay `whoami`echo “Su anki kullanıcılar:"
w | grep -v USER | sort
echo
#!/bin/bashecho “Script adı $0”
echo “1. Komut argumanı $1”
echo “ $# Kadar arguman vardı ve bunlar: $@”
Example.sh argument1 argument2 argument3
Her .txt dosyasını, .txt2 şeklinde değiştiren script yazın.
forfilein*.txt;do
mv $file${file}2
done
Vücut Index Hesaplama
#!/bin/bash# Body Mass Index (BMI) calculator –Vücut index Hesaplamaif [ $#-lt 2 ];thenecho"Usage: $0 kilo boy"exit 1
filet H2=“$2*$2”
let BMI="703 * $1 / $H2"echo “Sizin Vucut Indeksi( Body Mass Index (BMI)) is $BMI"if [ $BMI -le 18 ]; then echo “Çok zayıfsın….."elif [ $BMI-le 24 ];thenecho “Normal Kilo…..."elif [ $BMI -le 29 ]; then echo “Exersiz zamanı, kilo almıssın…."elseecho “Takma kafana…."fi
Select Case Örnek
PS3=“Sevdiğin kişi sec!”
Select CHOICE in “Josh” “Marty” “Dave” “HAL” “Me”;docase$CHOICEin
“Josh”|”Marty”)
echo “Akıllı Secim.”
;;
”Dave”|”HAL”)
echo “Bu senin son secimin.”
;;
“Me”)
echo “Super secim..”
;;
esacbreakdone
Dosya boyutu örnekleri
$ find /mp3klasor -name '*.mp3' -size -5000k
/mp3klasor adlı klasorde, boyu 5000 Kilobytes ( < 5MB) az mp3 dosyalarını bulur.
#!/bin/bash# Bu program 1 den 10 a kadar sayar:foriin 1 2 3 4 5 6 7 8 9 10;doecho$idone
Dosya varsa kopyala dosya yoksa çık
#!/bin/bashiftest -f /etc/foo
then# dosya var, kopyala ve message yaz .
cp /usr/foo .echo"Done."else# dosya yok , message yaz ve çık.echo"This file does not exist."exitfi
Command Line Parameters for ' test '
-d klasör kontrol
-e dosya bulunması kontrolü
-f sıradan dosya kontrolu
-r okunabilirlik kontrolu
-s dosya boyu kontrolu
-w yazılabilirlik kontrolu
-x calıstırılabilirlik kontrolu
Ornek
#!/bin/bash
x=0;while [ "$x"-le 10 ];doecho “Su anki x: $x" # x değerini artır: x=$(expr $x + 1) sleep 1done
Değişken tanımlanırken türü belirtmeye gerek yoktur. Ama istenirse declare kelimesiyle de tanımlanabilir. Değişken değerine ulaşmak için "$" kullanılır, atama yaparken "$" kullanılmaz. Atamalarda eşit işaretinin çevresine boşluk konulmaz.
#!/bin/bash
STRING="HELLO WORLD!!!"echo$STRINGdeclare -i x
x=10
x=x+1
echo$x
Atamalarda " (çift tırnak ) değişken değeri yazar. ' (tek tırnak) text olarak yazar.
#!/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 "
Örnek
Aynısı echo olmadan read komutu ile:
clear
read -p "Adınızı girin: " name
read -p "Yasınızı girin" age
read -p "Cinsiyet girin" sex
echo"Siz $age yasında $sex$name isimli kişisiniz"
Temiz bosluklar basacak sekilde:
clear
read -p "Adınızı girin: " name echo""read -p "Yasınızı girin" age echo""read -p "Cinsiyet girin" sex echo""echo"Siz $age yasında $sex$name isimli kişisiniz"
Örnek 2
#!/bin/bash echo -n "ilk sayıyı girin> "read a
echo -n "ikinci sayıyı girin> "read b
echo"Aritmetik işlem> "
sum=$(($a+$b))echo"a ve b toplam $sum"
IF
if [[ <şart> ]];then
komutlar
elif [[ <şart> ]];then#opsiyonel
komutlar
else#opsiyonel
komutlar
fi
If Örnek
if [ $fruit="elma" ] thenecho" Elmalar..."elif [ $fruit="armut"] thenecho" Armutlar..."elif [ $fruit="muz" ] thenecho" Muzlar"elseecho" Geriye Portakal kaldı!"fi
Dosya Karşılaştırmaları
-e : dosya bulunur(exist)
-f : sıradan –regular- dosyadır
-s : boş olmayan (boyu >0) dosyadır
-d
: klasördür.
-w : dosyaya yazma hakkı vardır.
-r : dosyayı okuma hakkı vardır.
-x : dosyayı çalıştırma hakkı vardır.
If Örneği
#!/bin/bashecho -n "Enter file name> "read file
# Use elif in bash for the “else if” construct.# The “>>” in the example is output redirection with appending.# The output of the ls command will be appended to the file.if [ -w"$file" ];then
ls >>$fileecho"More input has been appended"elif [ -e"$file" ];thenecho"You have no write permission on $file"elseecho"$file does not exist"fi
If Örneği-2
#!/bin/bashif [[ $1> 0 &&$(($2%10))!= 0 ]];thenecho Operands are valid
let “a = $2 % 10”
let “r = $(($1*$2)) / $a”
echo"expression value is $r"elseecho"Operand problem"fi
If Örneği-2-2
#!/bin/bashif [ $#!= 2 ];# or, if ( test $# != 2 )thenecho “Usage: $0 integer1 integer2”
elseecho “Doing arithmetic> “
r=$(($1+$2));echo “the sum "$1" + "$2“ is $r” r=$(($1-$2)) ; echo "the subtraction "$1" - "$2" is $r“
r=$(($1*$2));echo"the product $1 * $2 is $r“ if [ $2 -ne 0 ] ; then r=$(($1/$2)) ; echo "the division $1 / $2 is $r" else echo "the division $1 / $2 is impossible" fifi