Programming in shell script - 2

TheFlow

Üye
19 Şub 2021
202
1


Shell Scripts-2


Hello, I am here with our second lesson. So let's start;
The most basic branching statement in Bash is the if statement. While the syntax may seem intimidating to beginners. It is the same with other programming languages ​​such as C, C++, Java. Notice the spaces in front of and behind the square brackets. Shell is not whitespace independent, so you need to make sure you put them in the right places. Otherwise you will get a syntax error for every whitespace error.


Kod:
#!/bin/bash

if [ condition ]; then
#make here
fi -> Used for end the if.
#----------------------------below is a different type of programming
if [ condition ]
then
#make this
fi

Operators


-eq: equals
-ne: not equal
-gt: greater than
-ge: greater than or equal
-lt: less than
-le: less than or equal
<: less than (in double parentheses)
<=: less than or equal to (in double brackets)
>: greater than (in double brackets)
>=: greater than or equal to (in double brackets)


Operators are not limited to the above list. You can find the full list of operators on the "Linux documéntation Project" website.

Kod:
#!/bin/bash


count=10


if [ $count -eq 9 ]
then
echo "Condition is ture"
fi


if (($count > 9))
then
echo "Condition is ture"
fi

#---------------2th example----------------------

word="abc"


if [ $word == "abc" ] # the single equals sign is also valid
then
echo "Condition is ture"
fi

#---------------3th example----------------------

word=a


if [[ $word < "b" ]]
then
echo "Condition is true" 
else
echo "Condition is false"
fi

#---------------4th example----------------------


word=a


if [[ $word == "b" ]]
then
echo "char b is equal" 
elif [[ $word == "a" ]] ->means else if, since it does not end, fi is put at the end point.
then
echo "char a is equal" 
else
echo "char a or b isn`t equal"
fi -> ending point

File Operations



Dealing with files is a crucial part of Linux scripting. The job has an enormous amount of query options for file testing in bash. You can find the full list on the Linux documéntation Project website.



The source has an bonus part of this thread but I didnt translate it. But i will open a thread named BONUS PART and i will translate every bonus part there.

 
Son düzenleme:

THT SON MESAJLAR

Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.