
我想對此進行調整,以便當函數到達最後一個位置參數時, while 條件會退出。
console_codes ()
{
local exec=0
local narg="$#" iarg=0
while (( narg > 0 )); do
opt="$1" ; iarg=$(( iarg + 1 ))
case $opt in
("-V"|"--version")
printf '%s\n' "Version"
return 0
;;
("-h"|"--help")
printf "Help.\n"
return 0
;;
("-e"|"--exec") exec=1 ; shift 1 ;;
(*) shift 1 ;;
esac
done
}
答案1
您正在使用shift,因此只需檢查“$1”何時為空
while true; do
[ -z "$1" ] && break
echo "$1"
shift
done
答案2
您應該閱讀man getopt getopts
,而不是重新重新發明解析選項。
$#
您可以透過意識到減少了以下內容來更緊湊地執行循環shift
:
while [[ $# -gt 0 ]] ; do
# some code using $1
shift
done