您的位置:首页 > 运维架构 > Shell

Shell编程基础(2)

2015-06-18 10:12 453 查看
test 测试条件
用于判断:整数,字符串,文件

test str1=str2 测试字符串是否相等
test str1!=str2 测试字符串是否不等
test -n str1 测试字符串是否不为空
test -z str1 测试字符串是否为空

test 1 -eq 2 测试整数是否相等
test 1 -ge 2 测试1是否>=2
test 1 -gt 2 测试1是否>2
test 1 -le 2 测试1是否<=2

test 1 -lt 2 测试1是否<2

test 1 -ne 2 测试整数是否不相等

test -d file 测试指定文件是否是目录
test -f file 测试指定文件是否是常规文件

test -x file 测试文件是否可执行

test -r file 测试文件是否可读

test -w file 测试文件是否可写

test -a file 测试文件是否存在

test -s file 测试文件大小是否非0

if分支循环语句
if test -d $1 then .... else ..... fi
test -d $1可以简化写成 [ -d $1]

一个相对复杂的if结构
if 条件 then 命令
elif 条件 then 命令
else 命令
fi

-a 逻辑与 -o 逻辑或

exit 0 直接退出脚本运行正确退出
exit 非0数字 退出,脚本运行中有错误退出
for ...... done 语句

for 变量 in 一堆变量
do
命令
done

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: