控制流结构

来源:互联网 发布:今年好听的网络歌曲 编辑:程序博客网 时间:2024/05/01 22:56

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