Linux Bash Shell學習(十):流程控制——for
本文也即《Learning the bash Shell》3rd Edition的第五章Flow Control之讀書筆記之二,但我們將不限於此。flow control是任何程式語言中很常用的部分,也包括了bash。在這裡,我們將繼續學習他們。
和C不一樣的是,在shell中是匹配list中的元素,因此非常適合用於命令的引數,檔案列表。for格式如下:
for name [in list ]
do
statements that can use $name...
done
我們通過下面的例作進一步學習。一共設定了三個例子
# Test for the bash loop : for method
#
# Test 1: 顯示$PATH中各個具體路徑的詳細資訊function getpath
{
# 設定分割符號為“:”,預設為空格
IFS=:
# for測試,其中ls -ld中的-d表示之顯示目錄的屬性,不顯示目錄下屬的檔案。
for dir in $PATH
do
ls -ld $dir
done
}# 通常$PATH中包含~/bin目錄,一般不存在,在ls的顯示中會報告錯誤,需要挑出進行處理。同時我們增加一個空的元素來進行分析。
function getpath1
{
path=$PATH::
echo check : $path
IFS=:for dir in $path
do
# 如果是空元素,將其轉換為當前目錄。
if [ -z $dir ] ; then dir=. ; fi
if ! [ -e $dir ]; then
echo "$dir is not exist!"
elif ! [ -d $dir ]; then
echo "$dir is not a directory!"
else
ls -ld $dir
fi
done
}#echo 'run getpath'
#getpathecho 'run getpath1'
getpath1# Test 2 顯示指定檔案的是否是有效檔名
function finfo
{
if ! [ -e "$1" ]; then
echo "$1 is not an availble file."
else
echo "$1 is a file."
fi
}echo 'run fileinfo'
# 如果命令沒帶引數,即list為空,則不執行for裡面的程式碼
for filename in $@; do
echo "in for loop..."
finfo "$filename"
done
下面是一個遞迴的例子,我們希望將目錄下的檔案也顯示出來。根據層次結構,下一級比上一級向後挪一個tab。
# Test 3: 顯示目錄下的檔案,採用遞迴方式 。showdirfile 表示顯示所在目錄的所有檔案,如果也是目錄,遞迴執行。為了記錄方便,將註釋載入命令列後面,注意這只是為了閱讀方便,而不是shell應有的語法。
function showdirfile
{
#step1 :只有是有效目錄檔案,才search
if [ -d $1 ] ; then
#step2 ,比上一級後退tab,記錄前面tab的字元
currenttab+="/t"
#$(command <command>) 用於給出command的輸出結果,作為字串。
#step 3 :搜尋ls <dir_name>所顯示的entry,如果是檔案,顯示,如果是字串顯示,並在後面加上"/",表明是目錄,使用遞迴,檢索該目錄下的檔案。
for file in $(command ls $1); do
echo -e -n ${currenttab}$file
#step 4: 檔案的含路徑名字為$1/$file,由於我們沒有采用cd $file,即沒有進入該目錄進行訪問,因此要記錄path,如果是目錄,用dir記錄下這個子目錄的名字
if [ -d $1/$file ]; then
echo "/"
showdirfile $1/$file
else
echo
fi
done
#step2 ,執行完推出,恢復上一級的前面的tab字串,即去掉最後的/t。
currenttab=${currenttab%"/t"}
# end of step 1
fi
}
# 之前設定了IFS,會使得ls的輸出不是以空格為分割,異常,需要重置它
unset IFS
echo 'run file architechture'
showdirfile ${1:-"~"}
相關連結: 我的Linux操作相關文章
相關文章
- Linux Bash Shell學習(九):流程控制——if/elseLinux
- Shell學習【流程控制】
- Shell(Bash)學習· 總章
- Bash流程控制
- Linux Bash Shell學習(八):shell程式設計基礎——string操作Linux程式設計
- Linux Bash Shell學習(七):shell程式設計基礎——執行Shell指令碼、functionLinux程式設計指令碼Function
- 『忘了再學』Shell流程控制 — 39、特殊流程控制語句
- 鳥哥的Linux私房菜基礎篇 第十章認識和學習bash這個shellLinux
- Linux的Bash Shell詳解Linux
- linux自定義shell(bash)命令Linux
- 【java學習】控制執行流程Java
- Java學習之流程控制Java
- 學習bash
- Java學習之流程控制練習Java
- Shell Script(bash)--教學例(轉)
- Linux深入探索04-Bash shellLinux
- 『忘了再學』Shell流程控制 — 36、for迴圈介紹
- Linux學習(Shell基礎)Linux
- 20天學會bash shell script (二)
- 20天會學bash shell script(一)
- 20 天學會bash shell script (三)
- 快速學習Bash
- Linux中bash shell環境變數Linux變數
- 《前端運維》一、Linux基礎--06Shell流程控制前端運維Linux
- Shell指令碼的基本流程控制指令碼
- Linux學習-shell基礎02Linux
- Linux學習之(shell展開)Linux
- linux shell 學習摘記(3)Linux
- linux shell 學習摘記(4)Linux
- linux shell 學習摘記(5)Linux
- linux shell 學習摘記(6)Linux
- linux shell 學習摘記(7)Linux
- linux shell 學習摘記(8)Linux
- linux shell 學習摘記(9)Linux
- linux shell 學習摘記(1)Linux
- linux shell 學習摘記(2)Linux
- Linux程式設計-11.Shell Script(bash)--(2)教學例(轉)Linux程式設計
- 12 Bash For Loop Examples for Your Linux Shell ScriptingOOPLinux