else echo "Strings are not equal." fi 2. 使用 tr 命令去除所有空白字符 代码语言:txt 复制 string1=$(echo "your_string" | tr -d '[:space:]') string2=$(echo "your_string" | tr -d '[:space:]') if [ "$string1" == "$string2" ]; then echo "Strings are equal." else echo...
在这个程序中,string是一个空变量。由于-z运算符返回true,如果string的长度是0,因此我们得到The variable String is an empty string.作为给定程序的输出。 String="Test"if[[-n$String]];thenecho"The variable String is not an empty string."fi 输出: The variable String is not an empty string. 在这...
if [[ -n $String ]]; then echo "The variable String is not an empty string." fi 输出: The variable String is not an empty string. 在这个程序中,String 是一个非空变量。由于 -n 运算符返回 true,如果 string 的长度不是 0,因此我们得到 The variable String is not an empty string. 作为...
Thepatternwillmatchif itmatchesanypartofthe string. Anchor thepatternusingthe ‘^’and‘$’ regular expression operatorstoforce ittomatchthe entire string. Thearrayvariable BASH_REMATCH records which partsofthe string matched the pattern. The elementofBASH_REMATCHwithindex0containstheportionofthe string...
最近编写脚本,常看到有 if [ -x $variable ] 类的条件语句,不知道相应参数的意义到底是什么, 特摘录如下:fromhttp://blog.csdn.net/aaaaatiger/article/details/1713611 thanks! 1[ -a FILE ] 如果 FILE 存在则为真。2[ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真。3[ -c FILE ] 如果 FIL...
[student@studentvm1 testdir]$ expr length "We can also find the length of a literal string as well as a variable." 70 关于比较操作符,在我们的脚本中使用了大量的检测两个字符串是否相等(例如,两个字符串是否实际上是同一个字符串)的操作。我使用的是非 POSIX 版本的比较表达式: ...
string2="MyString" if [ "$string1" == "$string2" ] then echo "Equal Strings" else echo "Strings not equal" fi 💡 Pay Attention to the spaces There must be a space between the[and the variable name and the equality operator==. If you miss any of the spaces here, you’ll see...
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 =~?
- Test if a given variable is equal/not equal to the specified string: [[ $variable ==|!= "string" ]] - Test if a given string conforms the specified glob/regex: [[ $variable ==|=~ pattern ]] - Test if a given variable is [eq]ual/[n]ot [e]qual/[g]reater [t]han/[l]...
TestFile1 does not exist or is empty. 向文件添加一些内容,然后再测试一次: [student@studentvm1 testdir]$ File="TestFile1" ; echo "This is file $File" > $File ; if [ -s $File ] ; then echo "$File exists and contains data." ; else echo "$File does not exist or is empty." ...