我試圖寫一個bash腳本,將檢查的第一個參數,如果特定值的公司之一,那麼它會從參數中刪除該參數,並設置一些東西了:
#!/bin/bash -x
echo [email protected]
if [ "$1" == "valgrind" ]; then
echo running under valgrind
set $tool=valgrind
shift 1
elif [ "$1" == "debug" ]; then
echo running under gdb
set $tool=gdb --args
shift 1
else
echo running normally
set $tool=""
fi
echo tool=$tool
echo [email protected]
但是,當我運行它會消耗所有參數!例如: -
./my_script valgrind --something=y -r
+ echo args=valgrind --something=y -r
args=valgrind --something=y -r
+ '[' valgrind == valgrind ']'
+ echo running under valgrind
running under valgrind
+ set =valgrind
+ shift 1
+ echo tool=
tool=
+ echo args=
args=
,如果if語句不遵循:
./my_script valgrindx --something=y -r
+ echo args=valgrindx --something=y -r
args=valgrindx --something=y -r
+ '[' valgrindx == valgrind ']'
+ '[' valgrindx == debug ']'
+ echo running normally
running normally
+ set =
+ echo tool=
tool=
+ echo args==
args==
發生了什麼,以及如何解決它?