Linux Shell

您所在的位置:网站首页 mplrcparams是什么意思 Linux Shell

Linux Shell

2024-03-16 21:46| 来源: 网络整理| 查看: 265

退出状态

在Linux系统中,每当命令执行完成后,系统都会返回一个退出状态,该退出状态用一整数表示,状态值为0,表示命令运行成功,不为0时表示运行失败。最后一次执行命令的退出状态被曝存在内置变量"$?"中,可以通过echo语句测试命令是否运行成功。

退出状态以及含义 状态值含义0表示运行成功1~125运行失败,脚本命令、系统命令错误或者参数传递错误126找到该命令但无法执行127未找到要运行的命令>128命令被系统强制结束 ls 111.csv anaconda-ks.cfg home echo $? 0 # 访问的目录不存在 ll tt ls: 无法访问tt: 没有那个文件或目录 echo $? 2 #执行的命令不存在 fkjng -bash: fkjng: 未找到命令 [root@localhost ~]# echo $? 127 测试 测试结构

测试命令用于测试表达式的条件的真假,如果测试条件为真则返回0,如果测试条件为假,返回非0,。

两种格式:

test expression

[ expression ]

整数比较运算符

test测试数值时有一整套比较运算符,格式:

test "num1" numeric_operator "num2"或者[ "num1" numeric_operator "num2" ]

整数比较运算符描述-eq等于-ge大于等于-gt大于-le小于等于-lt小于-ne不等于 [root@localhost ~]# num1=15 [root@localhost ~]# [ "$num1" -eq 15 ] [root@localhost ~]# echo $? 0 [root@localhost ~]# [ "$num1" -eq 20 ] [root@localhost ~]# echo $? 1 [root@localhost ~]# [ "$num1" -gt 15 ] [root@localhost ~]# echo $? 1 #运算符点数 需要特定的函数 [root@localhost ~]# [ 1.5 -lt 2.2 ] -bash: [: 1.5: 期待整数表达式 字符串运算符 字符串运算符描述string测试是否不为空-n string是否不为空-z string是否为空string1=string2两个字符串是否相等string1!=string2是否不相等 [root@localhost ~]# str1="" [root@localhost ~]# test "$str1" [root@localhost ~]# echo $? # 该字符串为空 1 [root@localhost ~]# test -n "$str1" [root@localhost ~]# echo $? # 该字符串为空 1 [root@localhost ~]# test -z "$str1" [root@localhost ~]# echo $? # 该字符串为空 0 文件操作符

格式: test file_operator File或者[ file_operator file ]

文件运算符描述-d是否为目录-e是否存在-f是否为普通文件-r是否是进程可读文件-s文件长度是都不为0-w是否是进程可写文件-x是否是进程可执行文件-L是否符号化链接 [ -d dialer-service.conf ] [root@localhost demo]# echo $? 1 [root@localhost demo]# [ -d test ] [root@localhost demo]# echo $? 0 逻辑运算符 逻辑运算符描述!expression表达式真假-a相当于与-o相当于或 判断 简单if结构 if expression then command command fi exit命令

exit status

if/else结构 if expression then command else command fi if/else 语句嵌套 if expression1 then if expression2 then command else command fi else if expression3 then command command else command fi fi if/elif/else结构 if expression1 then command elif expression2 then command elif expression3 then command else command fi case结构 case variable in value1) command ... command;; value2) command ... command;; valueN) command ... command;; esac 运算符

和编译语言一样

数字常量 #!/bin/bash # 默认十进制 let "num1=40" echo "num1=$num1" #八进制表示 let "num2=040" echo "num2=$num2" #十六进制表示 let "num3=0x40" echo "num3=$num3" [root@localhost demo]# ./constant_exam1.sh num1=40 num2=32 num3=64

还可以用num#来表示不同进制



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3