if kullanımı

Ağustos 17th, 2011 § Yorum yok

Dosya için varlık kontrolü

if [ -a tmp.tmp ]; then
   rm -f tmp.tmp # Make sure we're not bothered by an old temporary file
fi

Dizin için varlık kontrolü

if [ -d ~/.kde ]; then
    echo "You seem to be a kde user."
fi

Dosyanın regular dosya olarak varlığının kontrolü icin -f regularfile
Regular dosyalar blok veya karakter special dosyaları(/dev dizinindeki device tanımlayan kernel dosyaları) değildir.

if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

Dosyayı inceleyen kullanıcı dosyanın sahibi mi?

if [ -O file ]; then
    chmod 600 file # Makes the file private, which is a bad idea if you don't own it
fi

Dosya okunabiliyor mu?(izinleri var mı)

if [-r file ]; then
    content=$(cat file) # Set $content to the content of the file
fi

Dosya 0 bytedan farklı okunabilen bir dosya mı?

if [ -s logfile ]; then
  gzip logfile    # Backup the old logfile
  touch logfile # before creating a fresh one.
fi

Dosya yazılabilir mi?

if [ -w /dev/hda ]; then
    grub-install /dev/hda
fi

Dosya çalıştırılabilir mi?(Dizinler için içeriği listelenebilir mi?)

if [ -x /root ]; then
    echo "You can view the contents of the /root directory."
fi

Dosyaların değiştirilme tarihini karşılaştırmak:
(olderfile yokken de True döner)

if [ newerfile -nt olderfile ]; then
    echo "story.txt1 is newer than story.txt; I suggest continuing with the former."
fi

veya

if [ /mnt/remote/remotefile -ot localfile ]; then
    cp -f localfile /mnt/remote/remotefile # Make sure the remote location has the newest version of the file, too
fi

Dosyaların inodelarını karşılaştırmak.

if [ /dev/cdrom -ef /dev/dvd ]; then
    echo "Your primary cd drive appears to read dvd's, too."
fi

String karşılaştırmak:

if [ "$1" == "moo" ]; then
    echo $cow # Ever tried executing 'apt-get moo'?
fi
if [ "$userinput" != "$password" ]; then
    echo "Access denied! Wrong password!"
    exit 1 # Stops script execution right here
fi

Stringin uzunluğu 0’dan büyük mü?

if [ -n "$userinput" ]; then
    userinput=parse($userinput) # Only parse if the user actually gave some input.
fi

String boş string mi?

if [ -z $uninitializedvar ]; then
    uninitializedvar="initialized" # -z returns true on an uninitialized variable, so we initialize it here.
fi

String regex’e uygun mu?

if [[ "$email" =~ "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b" ]]; then
    echo "\$email contains a valid e-mail address."
fi

Sayıları karşılaştırmak:

if [ $? -eq 0 ]; then # $? returns the exit status of the previous command
    echo "Previous command ran succesfully."
fi
if [ $(ps -p $pid -o ni=) -ne $(nice) ]; then
    echo "Process $pid is running with a non-default nice value"
fi
 
if [ $num -lt 0 ]; then
    echo "Negative numbers not allowed; exiting..."
    exit 1
fi
[ NUM1 -ne NUM2 ]
NUM1 is Not Equal to NUM2.
[ NUM1 -gt NUM2 ]
NUM1 is Greater Than NUM2.
[ NUM1 -ge NUM2 ]
NUM1 is Greater than or Equal to NUM2.
[ NUM1 -lt NUM2 ]
NUM1 is Less Than NUM2.
[ NUM1 -le NUM2 ]
NUM1 is Less than or Equal to NUM2.

Diğer kullanım örnekleri

if (( $? == 0 )); then # $? returns the exit status of the previous command
    echo "Previous command ran succesfully."
fi
 
if (( $(ps -p $pid -o ni=) != $(nice) )); then
    echo "Process $pid is running with a non-default nice value"
fi
 
if (( $num < 0 )); then
    echo "Negative numbers not allowed; exiting..."
    exit 1
fi
(( NUM1 != NUM2 )) NUM1 is not equal to NUM2.
(( NUM1 > NUM2 )) NUM1 is greater than NUM2.
(( NUM1 >= NUM2 )) NUM1 is greater than or equal to NUM2.
(( NUM1 < NUM2 )) NUM1 is less than NUM2.
(( NUM1 <= NUM2 )) NUM1 is less than or equal to NUM2.

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Ne yapıyorum ben!?

if kullanımı başlıklı yazıyı okuyorsun.

Üst Veri