mirror of
https://github.com/Hizenberg469/Makefile-tutorial.git
synced 2026-04-19 22:02:24 +03:00
update
This commit is contained in:
@@ -193,3 +193,108 @@
|
|||||||
|
|
||||||
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 ]]
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user