if-while-case-for-until控制流

【控制流结构】 --空命令永远为真

 

========================

if  expresion

   then [:]

       command1

   elif  command2

 else

       command3

 fi

       command4

========================

 

copyfile:

 

#!/bin/sh

#copyfile

if cp surfile desfile.bak

 then echo "haved copy"

else

 echo "`basename $0`:error could not copy the files">%2

fi

 

$ sh copyfile

 haved copy

----------------------------------------------------

#!/bin/sh

#copyfile

if cp nothisfile  desfile.bak >/dev/null 2>&1  --不在屏幕显示

then [:]

 echo "haved copy"

else

 echo "`basename $0`:error could not copy the files">&2

fi

 

$ sh copyfile

copyfile:error could not copy the files

----------------------------------------------------

filename=text.txt

if [ ! -w "$filename" ]  --判断文件 text.txt 为不可写文件

 

if [ $# -lt 3]  --调用传入脚本的参数个数小于3

 

if [ -t ]  --test返回值为1,则为交互模式(终端模式)

 

if [ -z $EDITOR ] --环境变量$EDITOR是否为空变量

 

$ ["$DIRECTORY" = ""]  --环境变量$DIRECTORY是否为空变量

 

if [ -z $LOGNAME != "root" ]  --登录账户为非root账户

 

----------------------------------------------------

========================

case [value] in

  mod1)

     command1

   ;;

  mod2)

     command2

   ;;

   ……

esac

========================

    *   任意字符。

    ?   任意单字符。

    [..]    类或范围中任意字符

----------------------------------------------------

#!/bin/sh

#text

echo -n "Enter a number from 1 to 5 :"

read ANS

 case $ANS in

 1) echo "You select 1"

 ;;

 2) echo "You select 2"

 ;;

 3) echo "You select 3"

 ;;

 4) echo "You select 4"

 ;;

 5|6) echo "You select 5/6"

 ;;

 yes|y) echo "You select 4"

 ;;

 *) echo "`basename $0 ` : This is not between 1 and 5 ">&2

 [break]

 exit 1

 ;;

esac

 

$ sh text

 Enter a number from 1 to 5 :3

 You select 3

$ sh text

 Enter a number from 1 to 5 :8

 text : This is not between 1 and 5

----------------------------------------------------

========================

for varname in list

do

 command1

 command2

 ...

done

========================

#!/bin/sh

#text

for loop in 1 2 3 4 5

do

 echo $loop

done

 

$ sh text

1

2

3

4

5

----------------------------------------------------

for loop in "orange red blue grey"

do echo $loop ; done        --打印字符串列表

----------------------------------------------------

for loop in `ls`

do echo $loop ; done        --打印当前ls的内容

----------------------------------------------------

========================

for varname in list1

 do

  for  varname2 in list2

  do

     command1

     command2

     ……

 done

done

========================

until format:

until condition

     command1

     command2

     ……

done

========================

while command

 do

   cammand1

   command2

   ……

done

========================

#!/bin/sh

#text

count=0

while [ $count -lt 5 ]

do

 count=`expr $count + 1 `

 echo $count

done

----------------------------------------------------

#!/bin/sh

#text

echo -n "enter your most liked film"

while read film

do

 echo "Yeah,great film the $film"

done

----------------------------------------------------

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值