日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

控制流结构

發(fā)布時(shí)間:2024/9/27 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 控制流结构 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


1.if語句

if 條件1

then?

? ? ?命令1

elif 條件2

then?

? ? ?命令2

else

? ? ?命令3

fi

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

if 條件

then 命令

fi

eg

#!/bin/bash

#if test

#this is a comment line

if [ "10" -lt "12" ];then

#yes 10 is less than 12

echo "yes,10 is less than 12"

else

echo "no"

fi

注意:if語句必須以fi終止

? ?"10" 前一個(gè)空格,“12”后也有一個(gè)空格。這個(gè)條件都是通過test命令來指定。條件表達(dá)為test expression或者[expression]

?

條件表達(dá)式中的比較函數(shù)

man test

NAME

? ? ? ?test - check file types andcompare values

SYNOPSIS

? ? ? ?test EXPRESSION

? ? ? ?[ EXPRESSION ]

? ? ? ?[ OPTION

DESCRIPTION

? ? ? ?Exit with the statusdetermined by EXPRESSION.

? ? ? ?--help display this help andexit

? ? ? ?--version

? ? ? ? ? ? ? outputversion information and exit

? ? ? ?EXPRESSION is true or falseand sets exit status. It is one of:

? ? ? ?( EXPRESSION )

? ? ? ? ? ? ?EXPRESSION is true

? ? ? ?! EXPRESSION

? ? ? ? ? ? ?EXPRESSION is false

? ? ? ?EXPRESSION1 -a EXPRESSION2

? ? ? ? ? ? ? bothEXPRESSION1 and EXPRESSION2 are true

? ? ? ?EXPRESSION1 -o EXPRESSION2

? ? ? ? ? ? ? eitherEXPRESSION1 or EXPRESSION2 is true

? ? ? ?[-n] STRING

? ? ? ? ? ? ? thelength of STRING is nonzero

? ? ? ?-z STRING

? ? ? ? ? ? ? thelength of STRING is zero

? ? ? ?STRING1 = STRING2

? ? ? ? ? ? ? thestrings are equal

? ? ? ?STRING1 != STRING2

? ? ? ? ? ? ??the strings are not equal

? ? ? ?INTEGER1 -eq INTEGER2

? ? ? ? ? ? ? INTEGER1is equal to INTEGER2

? ? ? ?INTEGER1 -ge INTEGER2

? ? ? ? ? ? ? INTEGER1is greater than or equal to INTEGER2

? ? ? ?INTEGER1 -gt INTEGER2

? ? ? ? ? ? ? INTEGER1is greater than INTEGER2

? ? ? ?INTEGER1 -le INTEGER2

? ? ? ? ? ? ? INTEGER1is less than or equal to INTEGER2

? ? ? ?INTEGER1 -lt INTEGER2

? ? ? ? ? ? ? INTEGER1is less than INTEGER2

? ? ? ?INTEGER1 -ne INTEGER2

? ? ? ? ? ? ? INTEGER1is not equal to INTEGER2

? ? ? ?FILE1 -ef FILE2

? ? ? ? ? ? ? FILE1and FILE2 have the same device and inode numbers

? ? ? ?FILE1 -nt FILE2

? ? ? ? ? ? ? FILE1 isnewer (modification date) than FILE2

? ? ? ?FILE1 -ot FILE2

? ? ? ? ? ? ? FILE1 isolder than FILE2

? ? ? ?-b FILE

? ? ? ? ? ? ? FILEexists and is block special

? ? ? ?-c FILE

? ? ? ? ? ? ? FILEexists and is character special

? ? ? ?-d FILE

? ? ? ? ? ? ? FILEexists and is a directory

? ? ? ?-e FILE

? ? ? ? ? ? ? FILEexists

? ? ? ?-f FILE

? ? ? ? ? ? ? FILEexists and is a regular file

? ? ? ?-g FILE

? ? ? ? ? ? ? FILEexists and is set-group-ID

? ? ? ?-h FILE

? ? ? ? ? ? ? FILEexists and is a symbolic link (same as -L)

? ? ? ?-G FILE

? ? ? ? ? ? ? FILEexists and is owned by the effective group ID

? ? ? ?-k FILE

? ? ? ? ? ? ? FILEexists and has its sticky bit set

? ? ? ?-L FILE

? ? ? ? ? ? ? FILEexists and is a symbolic link (same as -h)

? ? ? ?-O FILE

? ? ? ? ? ? ? FILEexists and is owned by the effective user ID

? ? ? ?-p FILE

? ? ? ? ? ? ? FILEexists and is a named pipe

? ? ? ?-r FILE

? ? ? ? ? ? ? FILEexists and is readable

? ? ? ?-s FILE

? ? ? ? ? ? ? FILEexists and has a size greater than zero

? ? ? ?-S FILE

? ? ? ? ? ? ? FILEexists and is a socket

? ? ? ?-t [FD]

? ? ? ? ? ? ? filedescriptor FD (stdout by default) is opened on a terminal

? ? ? ?-u FILE

? ? ? ? ? ? ? FILEexists and its set-user-ID bit is set

? ? ? ?-w FILE

? ? ? ? ? ? ? FILEexists and is writable

? ? ? ?-x FILE

? ? ? ? ? ? ?FILEexists and is executable

eg.

#!/bin/bash

#if test

#this is a comment line

echo "Enter your filename:"

read myfile

if [ -e $myfile ]

then

? ?if [ -s $myfile ];then

? ? echo "$myfile exist and size greaterthan zero"

? ?else

? ? echo "$myfile exist but size iszero"

? ?fi

else

echo "file no exist"

fi

[test@szbirdora 1]$ sh iftest.sh?

Enter your filename:

11

11 exist but size is zero

2.case語句

case語句為多選擇語句。

case in

模式1

? ? 命令1

? ? ;;

模式2)

? ? 命令2

? ? ;;

esac

eg.

#!/bin/bash

#case select

echo -n "enter a number from 1 to 3:"

read ans

case $ans in

1)

echo "you select 1"

;;

2)

echo "you select 2"

;;

3)

echo "you select 3"

;;

*)

echo "`basename $0`:this is not between 1 and3">2

exit;

;;

esac

3.for 循環(huán)

for循環(huán)一般格式:

for 變量名 in 列表 (列表以空格作為分割)

do

? ?命令1

? ?命令2

done

eg

#!/bin/bash

#forlist1

for loop in 1 2 3 4 5

do

echo $loop

done

4.until循環(huán)

until 條件

do

? ?命令1

? ?命令2

? ?...

done

條件測試發(fā)生在循環(huán)末尾,所以循環(huán)至少可以執(zhí)行一次。

5.

while循環(huán)

while 命令 (可以是一個(gè)命令也可以是多個(gè),做條件測試)

do

? ? ? 命令1

? ? ? 命令2

? ? ? ...

done

注意:如果從文件中讀入變量<filename要放到done

6.breakcontinue控制

break跳出,continue跳過

總結(jié)

以上是生活随笔為你收集整理的控制流结构的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。