linux shell 例項解析

2021-06-22 20:43:56 字數 1593 閱讀 9466

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 -1fi 

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)

#=> 目錄存在,進入目錄,獲取路徑

fiecho "nowdir"

#遞迴函式的實現

function searchcfile()   

#=>shell函式的定義方式   」 function func_name()「

')    

#=>shell正則演算法,獲取檔名

for cfilename in $cfilelist

doecho $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

Linux shell 指令碼例項

1.寫乙個指令碼,利用迴圈計算10的階乘 2.寫乙個指令碼,執行後,列印一行提示 please input a number 要求使用者輸入數值,然後列印出該數值,然後再次要求使用者輸入數值。直到使用者輸入 end 停止 執行指令碼方法 nuhup sh route.sh 注意前面要用上nohup,...

Linux shell 指令碼例項一

1.寫乙個指令碼,利用迴圈計算10的階乘 bin sh factorial 1 for a in seq 1 10 do factorial expr factorial a done echo 10 factorial 注 上面有一行,for a in seq 1 10 其中seq 1 10 即列...

以例項入門Linux shell

touch helloshell.sh bin bash 宣告使用的shell種類 echo hello shell 輸出 hello shell 首先獲取一下當前的工作目錄 presentpath pwd echo 當前目錄 presentpath 遍歷 需要打包的目錄 變數所有子目錄 僅僅遍歷目...