if [ -n "$string" ]; then echo "String is not empty" else echo "String is empty" fi 复制代码 使用-z选项判断字符串长度是否为零: if [ -z "$string" ]; then echo "String is empty" else echo "String is not empty" fi 复制代码 使用双括号[[和]]进行比较: if [[ -n $string ...
● -z :判断 string 是否是空串 ● -n :判断 string 是否是非空串 例子: #!/bin/sh string= if [ -z "$string" ]; then echo "string is empty"fi if [ -n "$string" ]; then echo "string is not empty"fi root@desktop:~# ./zerostring.sh string is empty 注:在进行字符串比较时, ...
STRING= if [ -z $STRING ]; then echo "STRING is empty" fi if [ -n $STRING ]; then echo "STRING is not empty" fi 输出错误结果: root@james-desktop:~# ./zerostring.sh STRING is empty STRING is not empty
1、判断字符串为空 if [ -z "$str" ]; then echo "empty string" fi 2、判断文件是否存在 if [ -f /home/builder/.profile ]; then echo "File exists;" fi 3、逻辑非 if [ ! -f /home/builder/.bash_profile ]; then echo "here!" else echo "test is ok" fi 逻辑非在语句前加“!”...
if [ -n "$str" ]; then echo "String is not empty" fi ``` 3. 判断两个字符串是否相等: ```shell if [ "$str1" = "$str2" ]; then echo "Strings are equal" fi ``` 4. 判断字符串是否包含某个子字符串: ```shell if [[ "$str" == *substring* ]]; then ...
if [ “$USER_NAME” = “terry” ]; then echo “I am terry” fi 小于比较 return 0 if the two string is equal, return 1 if $1 < $2, else 2 strCompare() { local x=0 if [ “$1” != “$2” ]; then x=2 local TEMP=printf "%s/n%s" "$1" "$2" local TEMP2=(echo ...
read a if [ $a ];then echo "$a : string is not empty" else echo "$a : string is empty" fi #abc : string is not empty #示例5.判断软硬链接 [ /root/student.txt -ef /tmp/su.txt ] && echo "true" || echo "false" #false #[[]] 必须有空格 = test #是否是文件,文件是否...
sh[options][file]#选项-c string:命令从-c后的字符串读取。-i:实现脚本交互。-n:进行shell脚本的语法检查。-x:实现shell脚本逐条语句的跟踪。-s:用于从标准输入中读取命令,接收命令参数在子shell中执行; 使用案例: 代码语言:javascript 代码运行次数:0 ...
private final static String INTERCEPTOR_TYPE = PropertyUtils.getString("shell.interceptor.type", "bash"); @SuppressWarnings("unchecked") public static IShellInterceptorBuilder newBuilder() { // TODO 默认的走的是这个逻辑 if (INTERCEPTOR_TYPE.equalsIgnoreCase("bash")) { ...
if [ $# >=3 ]; then shift 1 echo "此次需要更新的机器IP为:$@" for flat in $@ do echo "此次需要更新的机器IP为:$flat" 对相关IP机器进行的操作代码 then 我们将进一步详细说明Shell的知识要点。1. 运行Shell脚本Shell脚本有两种运行方式,第一种方式是利用sh命令,把Shell脚本文件名作为参数。这种...