控制流结构

if的使用

-rwxr--r-- 1 oracle oinstall 128 Mar 10 13:44 iftest1

[oracle@localhost iftest]$ ./iftest1

Enter your name :lubinsu

[oracle@localhost iftest]$ ./iftest1

Enter your name :

you didn't enter your name!

[oracle@localhost iftest]$ cat iftest1

#!/bin/sh

#if test1

echo -n "Enter your name :"

read NAME

if [ "${NAME}" = "" ] ; then

echo "you didn't enter your name!"

fi

[oracle@localhost iftest]$ ./iftest1

Enter your name :lubinsu

[oracle@localhost iftest]$ ./iftest1

Enter your name :

you didn't enter your name!

[oracle@localhost iftest]$

grep输出检查

[oracle@localhost iftest]$ ll

total 16

-rwxr--r-- 1 oracle oinstall 128 Mar 10 13:50 grepif

-rwxr--r-- 1 oracle oinstall 128 Mar 10 13:44 iftest1

[oracle@localhost iftest]$ cat grepif

#!/bin/sh

# grepif

if grep "if" iftest1 > /dev/null 2>&1

then

echo "if is in the file"

else

echo "if is not in the file"

fi

[oracle@localhost iftest]$ ./grepif

if is in the file

用变量测试grep输出

-rwxr--r-- 1 oracle oinstall 128 Mar 10 13:44 iftest1

#!/bin/sh

# grepif

if grep "if" iftest1 > /dev/null 2>&1

then

echo "if is in the file"

else

echo "if is not in the file"

fi

[oracle@localhost iftest]$ cat grepstr

#!/bin/sh

# grepif

echo -n "please enter a list of name:"

read list

if echo ${list} | grep "lubinsu" > /dev/null 2>&1

then

echo "lubinsu is in the list"

else

echo "lubinsu is not in the list"

fi

[oracle@localhost iftest]$ ./grepstr

please enter a list of name:lubinsu luzhou

lubinsu is in the list

[oracle@localhost iftest]$ ./grepstr

please enter a list of name:luzhou

lubinsu is not in the list

这里有必要提下一些特定的shell变量:


case的使用:

[oracle@localhost iftest]$ cat caseselect

#!/bin/sh

#case select

echo "please select a number betewwn 1..5:"

read number

case $number in

1) echo "you select 1"

;;

2) echo "you select 2"

;;

*) echo "you select greater than 2"

exit 1

;;

esac

[oracle@localhost iftest]$ ./caseselect

please select a number betewwn 1..5:

2

you select 2

[oracle@localhost iftest]$ ./caseselect

please select a number betewwn 1..5:

4

you select greater than 2

for的使用:

[oracle@localhost iftest]$ cat for_i

#!/bin/sh

#for

for i in 1 2 3 4 5

do

echo $i

done

[oracle@localhost iftest]$ ./for_i

1

2

3

4

5

循环遍历文件:

[oracle@localhost iftest]$ cat for_files

#!/bin/sh

#for files

counter=0

for files in *

do

counter=`expr $counter + 1`

echo ${files}

done

echo "There is $counter files in `pwd` we need to process"

[oracle@localhost iftest]$ ./for_files

caseselect

for_files

for_i

grepif

grepstr

iftest1

There is 6 files in /home/oracle/testDir/iftest we need to process

while读取文件:

[oracle@localhost iftest]$ cat while_file

#!/bin/sh

#while file

while read LINE

do

echo $LINE

done < $1

[oracle@localhost iftest]$ ./while_file ll.out

total 52

-rwxr--r-- 1 oracle oinstall 222 Mar 10 15:44 caseselect

-rwxr--r-- 1 oracle oinstall 162 Mar 10 16:16 for_files

-rwxr--r-- 1 oracle oinstall 56 Mar 10 16:05 for_i

-rwxr--r-- 1 oracle oinstall 128 Mar 10 13:50 grepif

-rwxr--r-- 1 oracle oinstall 199 Mar 10 14:17 grepstr

-rwxr--r-- 1 oracle oinstall 128 Mar 10 13:44 iftest1

-rw-r--r-- 1 oracle oinstall 0 Mar 10 16:27 ll.out

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值