-f FILE - 如果FILE存在且为常规文件(不是目录或设备),则为True。 -G FILE - 如果FILE存在且与运行命令的用户具有相同的组,则为True。 -h FILE - 如果FILE存在且为符号链接,则为True。 -g FILE - 如果FILE存在且设置了set-group-id(sgid)标志,则为True。 -k FILE - 如果FILE存在且设置了粘滞位标志,...
test 50 -gt 49 && echo "Bingo! True" || echo "Nah! False"In the above example, we are using the test command to check if 50 is greater than 49, and based on that the echo command is executed. So the output for this command will be,...
checkresult # prints TRUEall "beginswith x" x xy abc ; checkresult # p...
Check if a File Does not Exist Testing for a file returns0(true) if the file exists and1(false) if it does not exist. For some operations, you may want to reverse the logic. It is helpful to write a script to create a particular file only if it doesn’t already exist. To do so...
在脚本中,if语句的不同部分通常是良好分隔的。以下是一些简单的例子: 7.1.1.3. 检查文件 第一个例子检查一个文件是否存在: anny ~> msgcheck.shanny ~> 7.1.1.4. 检查shell选项 加入到你的Bash配置文件中去: # These lines will print a message if the noclobber option is set: ...
Here is another option, “-n”, to check whether the specified string is empty or not. It works on the rule of checking the length of a string by counting the string characters in it. If the length of a particular string turns out to be other than zero, it will return “true”; ...
由于greet 变量被分配给一个空字符串,测试返回 true,并且脚本打印到标准输出 greet 变量为空。 #!/bin/bash greet='' if [ -z "$greet" ] then echo "\$greet is empty" else echo "\$greet is not empty" fi 输出: $greet is empty 在Bash 中使用 -n 选项来检查变量是否为空 下面的脚本使用...
Method 1: Theif [ -d $directory ]Command ([Operator) Theif [ -d $directory ]command can be used to check for the existence of a directory. If the specified directory exists, the command will return an exit status of0(true). If the directory does not exist, the command will return ...
4. if与test及[] 4.1 数字判断 代码语言:javascript 代码运行次数:0 运行 AI代码解释 #!/bin/bash -xi=$1 #变量i的值取第1个参数的值iftest $i-gt89;then #如果i>89echo'A'elif test $i-gt79;then #如果i>79echo'B'elif test $i-eq60-o $i-gt60;then #如果i=60或i>60(即:i>=60) ...
I do this first, by looping over every element of the array. Then, by using the==operator to check if theexact stringis present or not. If present, I set the value of our flag variable (matched) astrue, since we did find a match :) ...