test能够判断变量是否为数字类型。 •-eq:等于 •-ne:不等于 •-gt:大于 •-ge:大于等于 •-lt:小于 •-le:小于等于 下面是一些示例用法: #判断变量是否等于某个数字 iftest$var-eq10 then echo"Variable is equal to 10" fi #判断变量是否大于某个数字 iftest$var-gt20
这个例子中就有大量的Test。 case case其实就是我们熟悉的那个swich,但语法形式上有很大的不同。 代码语言:javascript 代码运行次数:0 运行 AI代码解释 case"$variable"in"$condition1")command...;;"$condition2")command...;;esac 双引号包裹变量,这不是必须的 每一个Test语句,必须以)结尾 每一个条件区块,...
[[expression]]或 [expression] 或 testexpression 后面两个之所以放一起,是因为它们是Bash内置命令,并且[]完全可以看成test的别名。这里我们只详细介绍第一种使用方式[[ expression ]],它是Bash的关键字(Reserved words),使用方式更自然,且更不容易出错。感兴趣的读者可以参考这篇文章shell中(),[]和[[]]的区别。
- 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]...
在这个程序中,string 是一个空变量。由于 -z 运算符返回 true,如果 string 的长度是 0,因此我们得到 The variable String is an empty string. 作为给定程序的输出。 String="Test" if [[ -n $String ]]; then echo "The variable String is not an empty string." ...
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...
[student@studentvm1 testdir]$ expr length "We can also find the length of a literal string as well as a variable." 70 关于比较操作符,在我们的脚本中使用了大量的检测两个字符串是否相等(例如,两个字符串是否实际上是同一个字符串)的操作。我使用的是非 POSIX 版本的比较表达式: ...
TestFile1 does not exist or is empty. 现在创建一个空文件用来测试: [student@studentvm1 testdir]$ File="TestFile1" ; touch $File ; if [ -s $File ] ; then echo "$File exists and contains data." ; else echo "$File does not exist or is empty." ; fi ...
test/[ ]基本情况 双中括号[[ ]] 参考文档 变量的基本操作 数值运算 declare references expr bash_array
echo "the variable X is not the empty string" fi 运行这个脚本,输出如下: the variable X is not the empty string 为何?这是因为shell将$X展开为空字符串,表达式[-n]返回真值(因为改表达式没有提供参数)。再看这个脚本: #!/bin/bash X="" ...