= STRIN2 # Trueifstrings arenotequal#算术测试操作var1 -eq var2 # Trueifvar1 is equal to var2var1 -ne var2 # Trueifvar1notequal to var2var1 -lt var2 # Trueifvar1 is less than var2var1 -le var2 # Trueifvar1 is less thanorequal ...
如果你有过POSIX编程经验(比如Linux下C编程),你会知道一个errno的东西。你也会知道大量的if语句用来测试一个函数的调用结果,每个函数基本上都是返回0时表示操作成功,而如果返回非0则出错,此时你也要exit(0)。 下面言归正传 测试整数 基本规则 整数的test就是大小关系的比较,与其他语言不同,Bash中没有使用<,>来...
If the exit status code is not equal to zero, an error message is printed. Otherwise, a success message is printed.#!/bin/bash #Take a Linux command name echo -n "Enter a command: " read cmd_name #Run the command $cmd_name #Check whether the command is valid or invalid if [ ...
How to check if a string is in an array? How to use the Bash ternary operator? How to negate an if condition in a Bash if statement? (if not command or if not equal) How to use the BASH_REMATCH variable with the Regular Expression Operator =~?
In this script, after attempting to pull changes from a Git repository, we check the exit code. If it is not equal to 0 (indicating failure), we print an error message and exit the script with a status of 1. If the command is successful, we print a success message. This method allo...
在if语句中,我们喜欢用if [ expression ]; then ... fi的单括号的形式,但看大神们的脚本,他们更常用if [[ expression ]]; then ... fi的双括号形式。 [ ... ]等效于test命令,而[[ ... ]]是另一种命令语法,相似功能却更高级,它除了传统的条件表达式(Eg. [ ${val} -eq 0 ])外,还支持表达式...
We get 0 for a command executed correctly and 127 for a command not found in the example above. To use the exit code for an action, we do: #!/bin/bash if [[$? -eq 0]]; then echo "Success" else: echo "Fail" fi In the script above, we check if the exit code is equal to...
if [ -e "$file_to_check" ]; then echo "File exists" exit 0 # Exit with code 0 indicating success else echo "File not found" exit 1 # Exit with code 1 indicating failure fi Output: ad@DESKTOP-3KE0KU4:~$ ./test1.sh File exists ...
-z STRING # True if STRING is empty -n STRING # True if STRING is not empty STRING1 = STRIN2 # True if strings are equal STRING1 != STRIN2 # True if strings are not equal 算术测试操作 var1 -eq var2 # True if var1 is equal to var2 ...
if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints a success message; otherwise, it prints a failure message. 6.