期望的結果
有沒有辦法使用包含引數的字串來呼叫 a script
?
str_params="this/one 'that one' and \"yet another\""
stdout
下面的函式列印關于如何接收引數的反饋:
display_args () {
all_args=("${0}" "${@}")
for i in "${!all_args[@]}"; do
printf " $%d is '%q'\n" "${i}" "${!i}"
done
}
所需的結果將如下所示,其中使用陣列擴展(請參閱ary_params
使用,在拆分中str_params
):
ary_params=(this/one 'that one' and "yet another")
display_args "${ary_params[@]}"
$0 is 'bash'
$1 is 'this/one'
$2 is 'that\ one'
$3 is 'and'
$4 is 'yet\ another'
問題
第一次嘗試使用str_params
表明,無論單引號和雙引號如何,字串在轉換為引數時都會被空格分隔:
$ display_args ${str_params}
$0 is 'bash'
$1 is 'this/one'
$2 is '\'that'
$3 is 'one\''
$4 is 'and'
$5 is '\"yet'
$6 is 'another\"'
第二次嘗試拋出一個仍在試圖理解的錯誤。是否bash
真的試圖找到一個名為 all that string 的檔案?
$ cmd="display_args ${str_params}"
$ echo "${cmd}"
display_args this/one 'that one' and "yet another"
$ "$cmd"
bash: display_args this/one 'that one' and "yet another": No such file or directory
- 并且只呼叫
$cmd
(沒有雙引號)將我們帶回到第一次嘗試的結果(進一步向上)。
研究
圍繞這個特定問題有一些很好的檔案和帖子。僅提及一些:
此常見問題解答建議使用引數構建一個陣列(如本問題中所述)并擴展其元素,圍繞引數擴展的一些警告,
IFS= read -r
漫無邊際和其他一些不相關的示例。深入研究將陣列引數傳遞給 bash 腳本,這個答案清楚地反映了這一點,但對于在某些情況下可能有效的一些解決方法,這不是一個得到很好支持的方法(它也指bash 作者的報價:沒有t 真的是將陣列變數編碼到環境中的好方法)。這個答案(bash 用引號將行分割成引數)清楚地反映了shell如何處理命令與字串的區別。在執行命令之前,shell 執行引號洗掉作為最后一步。相反,在解釋
str_params
變數時,shell 將引號視為另一個字符,因此它們不是引號洗掉的物件。最令人驚訝的是,在同一篇文章的另一個答案中,似乎有一些示例可以重寫引數串列(無論這是否是一種通用方法)。這個另一個答案(如何在 bash 腳本中迭代引數)解釋說,shell 在擴展變數之前會處理引號,并且為了讓 shell注意到變數
str_params
中的引號(在cmd
上面的示例中),它需要使用eval
. 然而,使用eval
似乎是一種冒險的方法,我仍然懷疑是否沒有其他解決方案可以解決原則上似乎比這更容易的事情。
使用eval
通過遵循上面的示例,我嘗試了eval
并且似乎有效:
$ echo "$cmd"
display_args this/one 'that one' and "yet another"
$ eval $cmd
$0 is 'bash'
$1 is 'this/one'
$2 is 'that\ one'
$3 is 'and'
$4 is 'yet\ another'
$ eval display_args $str_params
$0 is 'bash'
$1 is 'this/one'
$2 is 'that\ one'
$3 is 'and'
$4 is 'yet\ another'
問題
- 有沒有辦法使用包含引數的字串,我們將使用它們將它們傳遞給 a而不使用?
script
eval
- 或者,是否有一種直接的方法可以將該字串轉換為一個
Array
看似bash
會做什么的引數?(這將允許通過擴展陣列的元素來呼叫,正如上面提到的這些行)。
uj5u.com熱心網友回復:
通常,xargs
可以這樣做。Xargs 無法處理引號內的換行符,換行符結束引數并開始另一行引數。例如,當您想要多次運行命令并在檔案中的不同行上具有引數時,xargs
這就是完美的工具。
$ echo "this/one 'that one' and \"yet another\"" | xargs -t printf "%q\n"
printf '%q\n' this/one 'that one' and 'yet another'
this/one
'that one'
and
'yet another'
好的解決方案是首先不要將引數存盤在字串中。將引數序列化為您可以在 Bash 中輕松閱讀的內容。例如,每個引數單獨一行或零分隔。或從declare -p
.
另一種解決方案是不使用 Bash。將 python 與shlex.split()
.
有沒有辦法使用包含引數的字串,我們將使用它們將它們傳遞給腳本而不使用 eval?
是的,撰寫一個決議器,將輸入決議為引數。對于每個字符,標記化,檢測它是否是引號,處理轉義序列,并自己構建引數陣列。就像使用任何其他編程語言一樣。或者使用一個外部現有的程式來完成這項作業。就像xargs
或shlex.split()
在 python 中一樣。
看起來很容易使用 xargs 將其加載到陣列中:
$ readarray -t -d '' args < <(echo "this/one 'that one' and \"yet another\"" | xargs -t printf "%s\0"); declare -p args
printf '%s\0' this/one 'that one' and 'yet another'
declare -a args=([0]="this/one" [1]="that one" [2]="and" [3]="yet another")
使用 Python,您可以正確處理換行符,但如果您的 Bash 腳本必須使用 Python,您不妨將整個 Bash 重寫為 Python:
$ readarray -t -d '' args < <(python -c 'import sys, shlex; print("\0".join(shlex.split(sys.argv[1])), end="")' "this/one 'that "$'\n'"one' and \"yet another\""); declare -p args
declare -a args=([0]="this/one" [1]=$'that \none' [2]="and" [3]="yet another")
bash 真的會嘗試找到一個名為 all that string 的檔案嗎?
是的,你參考"$cmd"
了 ,所以這是一個論點,所有的內容cmd
都是一個論點。因為它是行中的第一個,所以它是命令的名稱。另請注意,未參考的擴展會進行分詞 和 檔案名擴展!幸運的是,您沒有*
在字串中進行測驗以注意到它。參考在 shell中非常重要。記得用 shellcheck 檢查你的腳本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/526294.html
標籤:重击论据可变扩展参数扩展
上一篇:使用備忘錄合并成單個條件