在Bash脚本中,条件可以是各种各样的表达式,比如判断两个变量是否相等,判断一个文件是否存在,或者判断一个命令的返回值是否为0。下面是一些常见的条件表达式的示例: ```bash # Check if two variables are equal if [ $var1 -eq $var2 ] then echo "Variables are equal" else echo "Variables are not equa...
Example 2: Check if strings are not equal in Bash Instead of checking the quality, let’s do the opposite and check the inequality. Bash also provides the negation operator so that you can easily use “if not equal” condition in shell scripts. if [ "$string1" != "Not MyString" ] ...
Line 7checks if the variablesiandjare equal ($i -eq $j). If they are, thecontinue 2statement resumes the outer loop at the next iteration. However, if the values are different, the program continues as expected. Run the script to see the program output. Each time the two values are ...
To check if two strings are equal, use==operator. Strings are case sensitive, so you have to run the comparison keeping this point in mind. $ X="Linux" $ Y="Linux" $ [[ $X == $Y ]] && echo "Equal" || echo "Not equal" String equality To check if two strings are not equal...
The first “if” statement is used to check the total number of command line arguments and print an error message if the value is less than 2. Next, the dividend and divisor values are taken from the command line arguments. If the divisor value is equal to 0, an error is generated ...
Now you are ready to execute your first bash script: ./hello_world.sh 2. Simple Backup bash shell script #!/bin/bash tar -czf myhome_directory.tar.gz /home/linuxconfig 3. Variables In this example we declare simple bash variable and print it on the screen ( stdout ) with echo comman...
Check if strings are equals The '=' operator checks if string1 equals string2. If the two strings are equal, then it returns 0; if the two strings are not equal, then it returns 1: $ [ "sam" = "SAM" ] && echo $? || echo $?
In the above example, the default case (*)) was used, which was matched if any of the above patterns did not match. Until now, we learned how to check a single Boolean variable to find if that is true; what if we are required to validate multiple variables? Validate Multiple Boolean ...
In this example, we have two variables, ‘a’ and ‘b’. The script checks if ‘a’ is equal to ‘b’. If it’s not, it moves on to the ‘elif’ statement to check if ‘a’ is greater than ‘b’. If neither condition is met, it executes the ‘else’ statement, which in ...
#Check if salary and expenses are not equal elif [ $salary != $expenses ]; then echo "Salary and expenses are not equal" fi This script creates two new variables and compares whether they are equal or not. 10. Functions A bash function is a set of commands that can be reused numerous...