我正在撰寫一個 bash 腳本,它依賴于通過引數提供的許多值并提供可選的(布林值)標志。
基于此頁面上的示例,我已使用-h選項將腳本擴展為以下內容:
human=false
while getopts u:a:f:h: flag
do
case "${flag}" in
u) username=${OPTARG};;
a) age=${OPTARG};;
f) fullname=${OPTARG};;
h) human=true;;
esac
done
if $human; then
echo "Hello human"
else
echo "You're not human"
fi
echo "Username: $username";
echo "Age: $age";
echo "Full Name: $fullname";
問題在于,該-h引數需要一個值,而它應該作為標志處理:
bash test.sh -u asdf -h
test.sh: option requires an argument -- h
You're not human
Username: asdf
Age:
Full Name:
如何在 bash 腳本中使用帶有值和標志的兩個引數?
uj5u.com熱心網友回復:
您應該替換getopts u:a:f:h:為getopts u:a:f:h.
洗掉:you tell getoptsthath沒有額外的引數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/333621.html
上一篇:連接連接字串
