1、case語句
case ... in
... ) do something here
;;
esac
文件解壓腳本(注:shell中case語句與 c語言switch的區別):
#!/bin/bash
if [ ! -f "$1" ]; then #=>判斷文件是否存在
echo "this file is not exit "
exit -1
fi
ftype="$(file "$1")" #=>$num爲shell傳遞參數, file 命令可以查看文件的類型
case "$ftype" in
"$1: Zip archive"*) #=>” “ 中的內容爲字符串匹配, ”)“爲case語句格式
unzip "$1";; #=> 不同類型文件的解壓命令
"$1: gzip compressed "*)
gunzip "$1";;
"$1: bzip2 compressed"*)
bunzip2 "$1";;
*)
echo "file $1 can't be uncompressed whith smartzip";;
esac #=>結束標誌
2、遍歷linux目錄下所有文件
#!/bin/bash
if [[ -z "$1" ]] || [[ ! -d "$1"]]; then #=>”-z“ 判斷是否爲空,-d判斷目錄是否存在
echo "the directory is empty or not exit!"
echo "it will use the current directory."
nowdir=$(pwd) #=> pwd 命令 獲取路徑
else
nowdir=$(cd $1; pwd) #=> 目錄存在,進入目錄,獲取路徑
fi
echo "nowdir"
#遞歸函數的實現
function SearchCfile() #=>shell函數的定義方式 ” function func_name(){ #函數體 }“
{
cd $1 #=> shell函數中通過$num參數列表
cfilelist=$(ls -l | grep "^-" | awk '{print $9}') #=>shell正則算法,獲取文件名
for cfilename in $cfilelist
do
echo $cfilelist
done
dirlist=$(ls)
for dirname in $dirlist # =>獲取目錄下所有文件的信息並填充到dirname中
do
if [[ -d "$dirname" ]];then #=>判斷是否爲文件夾
cd $dirname
SearchCfile $(pwd)
cd ..
fi;
done;
}
SearchCfile $nowdir #=>函數調用,$nowdir 爲函數體中的$1