国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
Bourne Shell及shell編程 3

(5)循環(huán)控制
 <1> while循環(huán):
 格式
 while command
 do
 command
 command
 command
 ...
 done
 
 例 計(jì)算1到5的平方
 #!/bin/sh
 #
 #Filename: square.sh
 int=1
 
 while [ $int -le 5 ]
 do
 sq=`expr $int \* $int`
 echo $sq
 int=`expr $int + 1`
 done
 echo "Job completed"
 
 $ sh square.sh
 1
 4
 9
 16
 25

 Job completed
 
 <2> until循環(huán)結(jié)構(gòu)
 格式
 until command
 do
 command
 command
 ....
 command
 done
 
 示例使用until結(jié)構(gòu)計(jì)算1-5的平方
 #!/bin/sh
 
 int=1
 
 until [ $int -gt 5 ]
 do
 sq=`expr $int \* $int`
 echo $sq
 int=`expr $int + 1`
 done
 echo "Job completed"
 
 <3> 使用shift對(duì)不定長(zhǎng)的參數(shù)進(jìn)行處理
 在以上的示例中我們總是假設(shè)命令行參數(shù)唯一或其個(gè)數(shù)固定或者
使用$*將整個(gè)命
 行參數(shù)傳遞給shell script進(jìn)行處理對(duì)于參數(shù)個(gè)數(shù)不固定并且希
望對(duì)每個(gè)命令參
數(shù)
 進(jìn)行單獨(dú)處理時(shí)則需要shift命令使用shift可以將命令行位置參
數(shù)依次移動(dòng)位置
 
 即$2->$1, $3->$2. 在移位之前的第一個(gè)位置參數(shù)$1在移位后將
不在存在
 
 示例如下
 
 #!/bin/sh
 #
 #Filename: shifter
 

 until [ $# -eq 0 ]
 do
 echo "Argument is $1 and `expr $# - 1` argument(s) remain"
 shift
 done
 
 
 $ shifter 1 2 3 4
 Argument is 1 and 3 argument(s) remain
 Argument is 2 and 2 argument(s) remain
 Argument is 3 and 1 argument(s) remain
 Argument is 4 and 0 argument(s) remain
 $
 
 使用shift時(shí)每進(jìn)行一次移位$#減1使用這一特性可以用until循環(huán)對(duì)每個(gè)參
 數(shù)進(jìn)行處理如下示例中是一個(gè)求整數(shù)和的shell script:
 
 #!/bin/sh
 # sumints - a program to sum a series of integers
 #
 
 if [ $# -eq 0 ]
 then
 echo "Usage: sumints integer list"
 exit 1
 fi
 
 sum=0
 
 until [ $# -eq 0 ]
 do
 sum=`expr $sum + $1`
 shift
 done
 echo $sum
 
 
 $ sh sumints 324 34 34 12 34
 438
 $
 

 使用shift的另一個(gè)原因是Bourne Shell的位置參數(shù)變量為$1~$9,
因此通過(guò)位置變
 只能訪問(wèn)前9個(gè)參數(shù)但這并不等于在命令行上最多只能輸入9
個(gè)參數(shù)此時(shí)如果想
訪問(wèn)
 前9個(gè)參數(shù)之后的參數(shù)就必須使用shift.
 
 另外shift后可加整數(shù)進(jìn)行一次多個(gè)移位如
 
 shift 3
 
 
 <4>. for循環(huán)
 格式
 for var in arg1 arg2 ... argn
 do
 command
 ....
 command
 done
 
 示例
 $ for letter in a b c d e; do echo $letter;done
 a
 b
 c
 d
 e
 
 對(duì)當(dāng)前目錄下的所有文件操作
 $ for i in *
 do
 if [ -f $i ]
 then
 echo "$i is a file"
 elif [ -d $i ]
 echo "$i is a directory"
 fi
 done
 
 求命令行上所有整數(shù)之和
 #!/bin/sh
 

 sum=0
 
 for INT in $*
 do
 sum=`expr $sum + $INT`
 done
 
 echo $sum
 
 
 <6> 從循環(huán)中退出 break和continue命令
 break 立即退出循環(huán)
 continue 忽略本循環(huán)中的其他命令繼續(xù)下一下循環(huán)
 
 在shell編程中有時(shí)我們要用到進(jìn)行無(wú)限循環(huán)的技巧也就是說(shuō)這
種循環(huán)一直執(zhí)行
 到break或continue命令這種無(wú)限循環(huán)通常是使用true或false
命令開(kāi)始的UNIX
 系統(tǒng)中的true總是返加0值而false則返回非零值如下所示

 
 #一直執(zhí)行到程序執(zhí)行了break或用戶強(qiáng)行中斷時(shí)才結(jié)束循環(huán)
 while true
 do
 command
 ....
 command
 done
 
 上面所示的循環(huán)也可以使用until false, 如下
 
 until false
 do
 command
 ....
 command
 done
 
 在如下shell script中同時(shí)使用了continue,break以及case語(yǔ)句中
的正規(guī)表達(dá)式用
 
 #!/bin/sh

 # Interactive program to restore, backup, or unload
 # a directory
 
 echo "Welcome to the menu driven Archive program"
 
 while true
 do
 # Display a Menu
 echo
 echo "Make a Choice from the Menu below"
 echo _
 echo "1 Restore Archive"
 echo "2 Backup directory"
 echo "3 Unload directory"
 echo "4 Quit"
 echo
 
 # Read the user's selection
 echo -n "Enter Choice: "
 
 read CHOICE
 
 case $CHOICE in
 [1-3] ) echo
 # Read and validate the name of the directory
 
 echo -n "What directory do you want? "
 read WORKDIR
 
 if [ ! -d "$WORKDIR" ]
 then
 echo "Sorry, $WORKDIR is not a directory"
 continue
 fi
 
 # Make the directory the current working directory
 cd $WORKDIR;;
 
 4) :;; # :為空語(yǔ)句不執(zhí)行任何動(dòng)作
 *) echo "Sorry, $CHOICE is not a valid choice"


 continue
 esac
 
 case "$CHOICE" in
 1) echo "Restoring..."
 cpio -i </dev/rmt/0h;;
 
 2) echo "Archiving..."
 ls | cpio -o >/dev/rmt/0h;;
 
 3) echo "Unloading..."
 ls | cpio -o >/dev/rmt/0h;;
 
 4) echo "Quitting"
 break;;
 esac
 
 #Check for cpio errors
 
 if [ $? -ne 0 ]
 then
 echo "A problem has occurred during the process"
 if [ $CHOICE = 3 ]
 then
 echo "The directory will not be erased"
 fi
 
 echo "Please check the device and try again"
 continue
 else
 if [ $CHOICE = 3 ]
 then
 rm *
 fi
 fi
 done
 
(6)結(jié)構(gòu)化編程定義函數(shù)
 同其他高級(jí)語(yǔ)言一樣shell也提供了函數(shù)功能函數(shù)通常也稱之為子過(guò)
程(subroutine)
,
 其定義格式如下
 

 funcname()
 {
 command
 ...
 command; #分號(hào)
 }
 
 定義函數(shù)之后可以在shell中對(duì)此函數(shù)進(jìn)行調(diào)用使用函數(shù)定義可以將
一個(gè)復(fù)雜的程序
 為多個(gè)可管理的程序段如下所示
 
 # start program
 
 setup ()
 { command list ; }
 
 do_data ()
 { command list ; }
 
 cleanup ()
 { command list ; }
 
 errors ()
 { command list ; }
 
 setup
 do_data
 cleanup
 # end program
 
 技巧
 . 在對(duì)函數(shù)命名時(shí)最好能使用有含義的名字即函數(shù)名能夠比較準(zhǔn)
確的描述函數(shù)所
完成
 的任務(wù)
 . 為了程序的維護(hù)方便請(qǐng)盡可能使用注釋
 
 
 使用函數(shù)的另一個(gè)好處就是可以在一個(gè)程序中的不同地方執(zhí)行相同的
命令序列(函數(shù)),
 如下所示
 
 iscontinue()

 {
 while true
 do
 echo -n "Continue?(Y/N)"
 read ANSWER
 
 case $ANSWER in
 [Yy]) return 0;;
 [Nn]) return 1;;
 *) echo "Answer Y or N";;
 esac
 done
 }
 
 這樣可以在shell編程中調(diào)用iscontinue確定是否繼續(xù)執(zhí)行
 
 if iscontinue
 then
 continue
 else
 break
 fi
 
 
 ** shell函數(shù)與shell程序非常相似但二者有一個(gè)非常重要的差別shell程序是由子sh
ell
 執(zhí)行的而shell函數(shù)則是作為當(dāng)前shell的一部分被執(zhí)行的因此在
當(dāng)前shell中可以
 變函數(shù)的定義此外在任意shell(包括交互式的shell)中均可定義函數(shù)
 
 $ dir
 dir: not found
 $ dir () { ls -l ;}
 $ dir
 total 5875
 -rw-r--r-- 1 hbwork 100 Nov 10 23:16 doc
 -rw-r--r-- 1 hbwork 2973806 Nov 10 23:47 ns40docs.zip
 -rw-r--r-- 1 hbwork 1715011 Nov 10 23:30 ns840usr.pdf
 -rw-r--r-- 1 hbwork 1273409 Sep 23 1998 radsol21b6.tar.
Z
 -rw-r--r-- 1 hbwork 7526 Nov 10 23:47 wget-log

 -rw-r--r-- 1 hbwork 1748 Nov 13 21:51 wget-log.1
 $ unset dir
 $ dir () {
 > echo "Permission Link Owner Group File_SZ LastAccess
FileName"
 > echo "-----------------------------------------------------------"
 > ls -l $*;
 > }
 
 $ dir
 Permission Link Owner Group File_SZ LastAccess FileName
 -----------------------------------------------------------
 total 5875
 -rw-r--r-- 1 hbwork 100 Nov 10 23:16 doc
 -rw-r--r-- 1 hbwork 2973806 Nov 10 23:47 ns40docs.zip

 -rw-r--r-- 1 hbwork 1715011 Nov 10 23:30 ns840usr.pdf

 -rw-r--r-- 1 hbwork 1273409 Sep 23 1998 radsol21b6.tar.Z
 -rw-r--r-- 1 hbwork 7526 Nov 10 23:47 wget-log
 -rw-r--r-- 1 hbwork 1748 Nov 13 21:51 wget-log.1
 
 通常情況下shell script是在子shell中執(zhí)行的困此在此子shell中
對(duì)變量所作的
 修改對(duì)父shell不起作用點(diǎn)(.) 命令使用shell在不創(chuàng)建子shell而由
當(dāng)前shell讀取
 并執(zhí)行一個(gè)shell script, 可以通過(guò)這種方式來(lái)定義函數(shù)及變量此外
點(diǎn)(.)命令最
 常用的功能就是通過(guò)讀取.profile來(lái)重新配置初始化login變量如下
所示
 
 $ . .profile
 (csh相對(duì)于.命令的是source命令).
 
(7)使用And/Or結(jié)構(gòu)進(jìn)行有條件的命令執(zhí)行
 <1> And , 僅當(dāng)?shù)谝粋€(gè)命令成功時(shí)才有執(zhí)行后一個(gè)命令,如同在邏輯與表
達(dá)式中如果前面的
 結(jié)果為真時(shí)才有必要繼續(xù)運(yùn)算否則結(jié)果肯定為假
 
 格式如下
 
 command1 && command2

 
 例rm $TEMPDIR/* && echo "File successfully removed"
 
 等價(jià)于
 
 if rm $TEMPDIR/*
 then
 echo "File successfully removed"
 fi
 
 <2>Or, 與AND相反僅當(dāng)前一個(gè)命令執(zhí)行出錯(cuò)時(shí)才執(zhí)行后一條命令
 
 例 rm $TEMPDIR/* || echo "File not removed"
 
 等價(jià)與
 
 if rm $TEMPDIR/*
 then
 command
 else
 echo "File not removed"
 fi
 
 <3> 混合命令條件執(zhí)行
 command1 && command2 && command3
 當(dāng)command1, command2成功時(shí)才執(zhí)行command3
 
 command1 && command2 || comamnd3
 僅當(dāng)command1成功command2失敗時(shí)才執(zhí)行command3
 
 當(dāng)然可以根據(jù)自己的需要進(jìn)行多種條件命令的組合在此不多講述
 
 
(8) 使用getopts命令讀取unix格式選項(xiàng)
 UNIX格式選項(xiàng)指如下格式的命令行參數(shù)
 command -options parameters
 
 使用格式
 getopts option_string variable
 
 具體使用方法請(qǐng)參考getopts的在線文檔(man getopts).
 
 示例如下
 

 #newdate
 if [ $# -lt 1 ]
 then
 date
 else
 while getopts mdyDHMSTjJwahr OPTION
 do
 case $OPTION
 in
 m) date '+%m ';; # Month of Year
 d) date '+%d ';; # Day of Month
 y) date '+%y ';; # Year
 D) date '+%D ';; # MM/DD/YY
 H) date '+%H ';; # Hour
 M) date '+%M ';; # Minute
 S) date '+%S ';; # Second
 T) date '+%T ';; # HH:MM:SS
 j) date '+%j ';; # day of year
 J) date '+%y%j ';;# 5 digit Julian date
 w) date '+%w ';; # Day of the Week
 a) date '+%a ';; # Day abbreviation
 h) date '+%h ';; # Month abbreviation
 r) date '+%r ';; # AM-PM time
 \?) echo "Invalid option $OPTION";;
 esac
 done
 fi
 
 $ newdate -J
 94031
 $ newdate -a -h -d
 Mon
 Jan
 31
 $ newdate -ahd
 Mon
 Jan
 31
 $
 
 
 示例程序復(fù)制程序
 
 # Syntax: duplicate [-c integer] [-v] filename

 # where integer is the number of duplicate copies
 # and -v is the verbose option
 
 COPIES=1
 VERBOSE=N
 
 
 while getopts vc: OPTION
 do
 case $OPTION
 in
 c) COPIES=$OPTARG;;
 v) VERBOSE=Y;;
 \?) echo "Illegal Option"
 exit 1;;
 esac
 done
 
 if [ $OPTIND -gt $# ]
 then
 echo "No file name specified"
 exit 2
 fi
 
 
 shift `expr $OPTIND -1`
 
 FILE=$1
 COPY=0
 
 while [ $COPIES -gt $COPY ]
 do
 COPY=`expr $COPY + 1`
 cp $FILE ${FILE}${COPY}
 if [ VERBOSE = Y ]
 then
 echo ${FILE}${COPY}
 fi
 done
 
 
 $ duplicate -v fileA
 fileA1
 $ duplicate -c 3 -v fileB

 fileB1
 fileB2
 fileB3

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
shell 十三問(wèn)之第三問(wèn):別人 echo、你也 echo ,是問(wèn) echo 知多少
echo命令行
SHELL十三問(wèn)之三:別人 echo、你也 echo ,是問(wèn) echo 知多少
shell之用command在終端判斷是否存在這個(gè)命令
shell程序編寫(xiě)從入門到精通(下)
shell調(diào)試技術(shù)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服