/bin/bash -xstr1="abc"if[-z"$str1"];then echo'str1 is empty'elseecho'str1 is not empty'fi printf"\n"str2=""if[-n"$str2"];then echo'str2 is not empty'elseecho'str2 is empty'fi printf"\n"if["$str1"="$str2"];then echo'str1 = str2'elseecho'str1 <> str2'fi 注...
4. 关于两个整数之间的判定,例如 test n1 -eq n2 -eq 两数值相等 (equal) -ne 两数值不等 (not equal) -gt n1 大于 n2 (greater than) -lt n1 小于 n2 (less than) -ge n1 大于等于 n2 (greater than or equal) -le n1 小于等于 n2 (less than or equal) 5. 判定字符串的数据 test -z ...
echo"the variable X is not the empty string" fi 运行这个脚本,输出如下: the variable X is not the empty string 为何?这是因为shell将$X展开为空字符串,表达式[-n]返回真值(因为改表达式没有提供参数)。再看这个脚本: 1 2 3 4 5 #!/bin/bash X="" if[ -n"$X"];then# -n 用来检查变量是...
How to Check Bash String Equality on Linux Checking if two Bash script strings are equal isn’t a big thing per se, and even if the Bash script strings are not equal, that’s not also a big thing. This article explains everything you need to know about Bash string equality, plus how...
Bash 简介 转自 https://wangdoc.com/bash/intro.html Bash 是 Unix 系统和 Linux 系统的一种 Shell(命令行环境),是目前绝大多数 Linux 发行版的默认 Shell。 目录 [隐藏] 简介 基本语法 模式扩展 引号和转义 变量 字符串操
is readable-s FILE_NAM # TrueifFILE_NAM existsandisnotempty-w FILE_NAM # TrueifFILE_NAM has write permission-x FILE_NAM # TrueifFILE_NAM is executable#字符串测试操作-z STRING # TrueifSTRING is empty-n STRING # TrueifSTRING isnotemptySTRING1...
The first condition is false since 5 is not equal to 4, but then the next condition in the ELIF statement is true since 5 is greater than 3, so that echo command is executed and the rest of the statement is skipped. Try to guess what will happen if we use2as an argument: ...
not equal:!= numeric equality:-eq not equals:-ne is empty:-z if [[ 1 -eq 1 ]]; if [[ -z $USER ]]; 1. 2. 3. Elif if [[ -z $USER ]]; then echo "user is empty" elif [[ 1 -eq 1 ]]; then echo "1==1"
we have been using the “not equal” operator i.e., “-ne” to check if the two values are not equal. The “!” operator is also used within the “if” statement. If the “-ne” operator returns “true” the “!” operator will reverse it by “false” and the else part will ...
# Function to check if a number is prime is_prime() { local num=$1 # Check if the number is less than 2 (not prime) if (( num < 2 )); then echo false return fi # Check divisibility from 2 to the square root of the number ...