Shell腳本傳遞參數(shù)的4種方式實例說明
1.Shell基礎(chǔ)知識
1.1 解釋器
#!/bin/bash
稱為shebang或sha-bang、hashbang。是一個特殊的字符序列,由井號“#”和嘆號“!”組成,放在腳本文件的第一行開頭。在Linux和類Unix操作系統(tǒng)中,這個字符序列用于指定腳本文件的解釋器路徑。當一個腳本文件的第一行包含了shebang,操作系統(tǒng)在執(zhí)行該文件時會將該路徑后的所有參數(shù)傳遞給指定的解釋器,讓它來解釋執(zhí)行該腳本。
當系統(tǒng)運行一個腳本時,首先會檢查腳本的shebang行,然后找到指定的解釋器,并把腳本傳遞給它執(zhí)行。如果shebang行不存在或格式不正確,系統(tǒng)會將腳本視為普通文本文件,并無法執(zhí)行腳本。常見的解釋器包括Bash shell、Python、Perl等,可以使用對應(yīng)的路徑來指定解釋器。例如,#!/usr/bin/python
表示使用Python解釋器來執(zhí)行腳本。
如果一個文件沒有shebang行,系統(tǒng)就不知道用哪個解釋器來執(zhí)行它。此時,如果這個文件具有可執(zhí)行權(quán)限(例如755),系統(tǒng)會把它作為一個shell腳本來執(zhí)行,使用的是默認的shell解釋器(在大多數(shù)Unix/Linux系統(tǒng)中,這是Bash shell)。
Shell腳本是一種編程語言,可以用于在Unix/Linux系統(tǒng)上編寫自動化任務(wù),批量處理數(shù)據(jù),系統(tǒng)管理等。下面是Shell腳本的基礎(chǔ)知識:
1.2 變量
用于存儲數(shù)據(jù),可以使用=
來賦值,例如name="kite"
。
1.3 參數(shù)
Shell腳本可以接收命令行傳入的參數(shù),使用$1
、$2
等變量來引用,例如$1
表示第一個參數(shù)。這部分第二章會詳細舉例。
1.4 條件語句
使用if
語句來實現(xiàn)條件判斷,例如:
# 腳本內(nèi)容script.sh【使用位置參數(shù)】 #!/bin/bash if [ $1 -gt 18 ]; then echo "You are an adult." else echo "You are not an adult yet." fi # 腳本調(diào)用 ./script.sh 19
1.5 循環(huán)語句
使用for
和while
語句來實現(xiàn)循環(huán),例如:
# for語句 for i in 1 2 3 4 5; do echo $i done # while語句【使用了變量】 i=0 while [ $i -lt 10 ]; do echo $i i=$((i+1)) done
1.6 函數(shù)
使用function
或()
定義函數(shù),例如:
# 注意函數(shù)的定義和調(diào)用都是在腳本內(nèi)部的 function sayHi { echo "Hello, $1!" } sayHi "Kite"
1.7 輸入輸出
使用echo
命令輸出文本,使用read
命令讀取用戶輸入,例如:
# read 命令會等待輸入 echo "What's your name?" read name echo "Hello, $name!"
1.8 命令執(zhí)行
使用反引號或美元符來執(zhí)行命令,并將結(jié)果賦值給變量,例如:
today=`date +%Y-%m-%d` echo "Today is $today"
1.9 運算符
Shell腳本支持算術(shù)運算、字符串運算和邏輯運算,例如:
# 算術(shù)運算 num=$((1+2)) echo $num # 字符串運算 if [ "$name" == "John" ]; then echo "Hello, John!" fi # 邏輯運算 if [ $age -gt 18 ] && [ $gender == "male" ]; then echo "You are a man." fi
以上是Shell腳本的基礎(chǔ)知識,掌握了這些知識可以編寫簡單的腳本來自動化執(zhí)行任務(wù),進一步學(xué)習(xí)可以掌握Shell腳本的高級用法,例如正則表達式、管道、重定向、進程控制等。
2. 參數(shù)傳遞
2.1 位置參數(shù)
在shell腳本中,可以使用位置參數(shù)來傳遞信息。可以使用1、1、1、2、$3等來訪問它們。例如:
#!/bin/bash echo "The first argument is $1" echo "The second argument is $2"
在命令行中執(zhí)行該腳本時,可以傳遞兩個參數(shù),如下所示:
$ ./script.sh hello world
輸出:
The first argument is hello
The second argument is world
2.2 特殊變量
shell提供了許多特殊變量來傳遞額外的信息,例如:
$0
:表示腳本名稱。$#
:表示傳遞給腳本的參數(shù)個數(shù)。$@
:表示所有傳遞給腳本的參數(shù)的列表。$?
:表示上一個命令的返回值。
這也解釋了為什么位置參數(shù)是從1開始的了,$0
經(jīng)常用在日志里,說明當前執(zhí)行的腳本名稱。
例如:
#!/bin/bash echo "The script name is $0" echo "The number of arguments is $#" echo "The arguments are $@" echo "The return value of the last command is $?"
在命令行中執(zhí)行該腳本時,可以傳遞任意個數(shù)的參數(shù),如下所示:
$ ./script.sh a b c
輸出:
The script name is ./script.sh
The number of arguments is 3
The arguments are a b c
The return value of the last command is 0
2.3 環(huán)境變量
可以使用環(huán)境變量來傳遞信息。在shell腳本中,可以使用$VAR的形式來訪問環(huán)境變量。例如:
#!/bin/bash echo "The value of HOME is $HOME" echo "The value of PATH is $JAVA_HOME"
在命令行中執(zhí)行該腳本時,輸出環(huán)境變量的值,如下所示:
$ ./script.sh The value of HOME is /root The value of PATH is /usr/local/java/jdk1.8.0_241
在腳本里獲取環(huán)境變量,可對執(zhí)行環(huán)境進行檢驗。
2.4 命名參數(shù)
2.4.1 getopts
getopts
是Bash shell自帶的命令行參數(shù)處理工具,它的語法比較簡單,只支持處理單個字母選項,例如-a
、-b
等。getopts
只能處理短選項,也就是只能使用一個字母來表示選項,如果要處理長選項,需要編寫更多的代碼。另外,getopts
處理命令行參數(shù)時會把選項和參數(shù)分別處理,不能處理連續(xù)的選項,例如-abc
。
# 測試腳本 #!/bin/bash while getopts n:a:g: opt do case "${opt}" in n) name=${OPTARG};; a) age=${OPTARG};; g) gender=${OPTARG};; esac done echo "NameVal: $name"; echo "AgeVal: $age"; echo "GenderVal: $gender";
# 腳本調(diào)用 ./script.sh -n Kite -a 18 -g f NameVal: Kite AgeVal: 18 GenderVal: f
2.4.2 getopt
getopt
是GNU工具集中的一個命令行參數(shù)處理工具,它支持更多的選項和語法,可以處理短選項和長選項,還可以處理連續(xù)的選項。getopt
的語法比getopts
更加復(fù)雜,需要指定一個選項字符串,包含了所有支持的選項和參數(shù)。getopt
將解析后的選項和參數(shù)保存在一個數(shù)組中,需要在代碼中處理這個數(shù)組。
getopt
命令有以下幾個參數(shù):
-o
:指定單字符選項。選項之間無需分隔符。--long
:指定長選項。長選項之間使用逗號分隔。:
:選項后添加冒號說明當前選項需要參數(shù)值。--
:分割選項和參數(shù)。"$@"
:表示將所有命令行參數(shù)作為一個字符串傳遞給getopt
命令。
options=$(getopt -o n:a:g:p --long name:,age:,gender:,print -- "$@")
會解析命令行選項和參數(shù),并將轉(zhuǎn)換后的選項和參數(shù)存儲在變量options
中。這個變量通常會傳遞給一個eval
命令進行處理,例如:
eval set -- "$options"
# 測試腳本 #!/bin/bash # 解析命令行參數(shù) options=$(getopt -o n:a:g:p --long name:,age:,gender:,print -- "$@") eval set -- "$options" # 提取選項和參數(shù) while true; do case $1 in -a | --age) shift; age=$1 ; shift ;; -n | --name) shift; name=$1 ; shift ;; -g | --gender) shift; gender=$1 ; shift ;; -p | --print) print=true; shift ;; --) shift ; break ;; *) echo "Invalid option: $1" exit 1 ;; esac done # 檢查變量 if [ -z "$age" ]; then echo "Error: age is required" exit 1 fi if [ -z "$name" ]; then echo "Error: name is required" exit 1 fi if [ -z "$gender" ]; then echo "Error: gender is required" exit 1 fi # 判斷開關(guān)選項 if [ "$print" = true ]; then echo "NameVal: $name; AgeVal: $age; GenderVal: $gender"; fi
# 腳本調(diào)用(長選項) ./script.sh --name Kite --age 18 --gender f -p NameVal: Kite; AgeVal: 18; GenderVal: f # 腳本調(diào)用(單字符選項) ./script.sh -n Kite -a 18 -g f -p NameVal: Kite; AgeVal: 18; GenderVal: f
3.總結(jié)
如果能夠靈活運用shell腳本,也是很強大的。
以上就是Shell腳本傳遞參數(shù)的4種方式實例說明的詳細內(nèi)容,更多關(guān)于Shell腳本傳參方式的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
編寫B(tài)ash Shell通過gnuplot繪制系統(tǒng)性能數(shù)據(jù)圖的方法
這篇文章主要介紹了編寫B(tài)ash Shell通過gnuplot繪制系統(tǒng)性能數(shù)據(jù)圖的方法,做到可視化數(shù)據(jù)收集,需要的朋友可以參考下2015-07-07ubuntu編譯pyav報錯libx264?not?found解決示例
這篇文章主要為大家介紹了ubuntu編譯pyav報錯libx264?not?found解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09Shell腳本中判斷輸入?yún)?shù)個數(shù)的方法
這篇文章主要介紹了Shell腳本中判斷輸入?yún)?shù)個數(shù)的方法,使用內(nèi)置變量$#即可實現(xiàn)判斷輸入了多少個參數(shù),需要的朋友可以參考下2014-10-10shell腳本實現(xiàn)mysql數(shù)據(jù)庫雙機定時備份的方法
最近有個需求,要求實現(xiàn)對某個數(shù)據(jù)庫進行雙機備份,每天凌晨備份一次,要求主機器只保留最近十五天的記錄,我們決定用shell腳本加定時任務(wù)的方式來實現(xiàn)這個需求,接下來通過本文給大家介紹shell腳本實現(xiàn)mysql數(shù)據(jù)庫雙機定時備份的方法,感興趣的朋友一起看看吧2022-07-07