This commit is contained in:
2024-11-20 12:27:31 +05:30
parent 8c47c6182f
commit 847beb446f

View File

@@ -192,4 +192,109 @@
non-zero value. non-zero value.
We can exit script using exit command. We can exit script using exit command.
exit command can be used with number 0 - 255. exit command can be used with number 0 - 255.
-> If Statement
if[ condition ]; then
....
elif[ condition ]; then
....
elif[ condition ]; then
....
else
....
fi
* AND with if statement ?
if[ condition ] && [ condition ]; then
....
fi
* OR wiht if statement ?
if[ condition ] || [ condition ]; then
....
fi
* Negate if condition
if[ !condition ]; then
....
fi
if![ condition ]; then
....
fi
-> Mathematical comparison:
* Operands
$ -eq equal to
$ -ne not equal to
$ -gt greater than
$ -lt less than
$ -ge greater than or equal to
$ -le less than or equal to
Examples:
if [ $VAR -eq 5 ]; then
if [ $VAR -gt 7 ]; then
if [ $VAR -eq 5 ] && [ $COUNT -ne 1 ]; then
-> String Comaparison
* Comapare two strings
[ "$STR1" = "$STR2" ]
[ "$STR1" != "$STR2" ]
[ "$STR1" = "hello" ]
[ "$STR1" != "hello" ]
* Compare two strings - quotation marks?
Bash can handle it in another way [[ ]]
[[ $STR1 = $STR2 ]]
[[ $STR1 != $STR2 ]]
[[ $STR1 = "hello" ]]
[[ $STR1 != "hello ]]
This is not widely used and most scripts
are using quotation marks with single
brackets
* Test if string is empty
[ -z "$STR1" ] returns true if STR1 holds
an empty string
[[ -z $STR1 ]] possible, but not widely used
[ -n "$STR1" ] returns true if STR1 holds
a non-empty string
[[ -n $STR1 ]] possible, but not widely used
* Alphabetically compare two strings
[[ $STR1 > $STR2 ]]
[[ $STR1 < $STR2 ]]
No choice!!!
* Wildcards
[[ $STR1 == string-with-wildcards ]]
* Regular expression
[[ $STR1 =~ $regex ]]