shell编程(四)

 第四张 控制流结构

控制结构
if then else 语句
case语句
for循环
until循环
while循环
break控制
continue控制。


流控制是什么?

#!/bin/bash
#创建一个目录
make /home/chinaitlab/shell/txt
#复制所有txt文件到
/home/chinaitlab/shell/txt/
cp *.txt /home/chinaitlab/shell/txt/
rm -f *.txt
上面的三个命令其中一个出错了怎办,这样就需要使用流控制语句

if语句:

if 条件1
then 命令1
elif 条件2
then 命令2
else 命令3
fi  完成

注意:if语句必须以fi结束。
For example:

#!/bin/bash
#iftest
#this is a comment line,all comment lines start with a #
if["10" -lt "12"]
then
 #yes 10 is less than 12
echo "Yes ,10 is less than 12"
else
    echo "NO"
fi


test[expression]/[!expression]/[expression1 -a expression2 两个都为真则整个条件就为真]

[expression1 -o expression2 其中一个都为真则整个条件就为真]


[-n]STRING
     the length of STRING is nonzero
-z STRING
    the length of STRING is zero

STRING1=STRING2

    the strings are equal


INTEGER1 -eq INTEGER2
    INTEGER1 is equal to INTEGER2

INTEGER1 -ge INTEGER2
>=
INTEGER1 -gt INTEGER2

>
INTEGER1 -le INTEGER2
<=
INTEGER1 -lt INTEGER2

<
INTEGER1 -ne INTEGER2

FILE1 -ef FILE2
    FILE1 and FILE2 have the same device and inode numbers

稍后自己查test命令。

man test


脚本测试

#!/bin/bash
#iftest2
echo -n "Enter your name:"
read NAME
#did the user just hit return
if["$NAME"==""];then
echo "You did not enter any information"
fi


#!bin/bash
#ifcp
if cp myfile.bak myfile;then
echo"good copy"
else
echo "`basename $0`:error could not copy the files">&2
fi


#!/bin/bash
#ifelif
echo -n "Enter your name:"
read NAME
if[ -z $NAME] ||["$NAME"=""];then
    echo "You did not enter a name."
elif["$NAME"="root"];then
    echo "Hello root"
elif["$NAME"="chinaitlab"];then
    echo "Hello chinaitlab"
else
    echo "You are not root or chinaitlab,but hi,#NAME"
fi

set nu

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值