我使用此函式將一些檔案名提供給另一個命令。
function fun(){
find "${@}" -print0 | xargs -r0 other_command
}
呼叫時,所有引數 a 傳遞給以find過濾檔案名(-type, -name,等)
有什么方法可以將其中一些引數傳遞給other_command?如果可能,可變數量的引數。
像這樣的東西
fun [list of aguments for find] [list of aguments for other_command] # pseudo-phantasy syntax
是否可以?
uj5u.com熱心網友回復:
通過“nameref”將幾個陣列傳遞給函式。
fun() {
local -n first_args="$1"
local -n second_args="$2"
local -i idx
for idx in "${!first_args[@]}"; do
printf 'first arg %d: %s\n' "$idx" "${first_args[idx]}"
done
for idx in "${!second_args[@]}"; do
printf 'second arg %d: %s\n' "$idx" "${second_args[idx]}"
done
echo 'All first args:' "${first_args[@]}"
echo 'All second args:' "${second_args[@]}"
}
one_arg_pack=(--{a..c}{0..2})
another_arg_pack=('blah blah' /some/path --whatever 'a b c')
fun one_arg_pack another_arg_pack
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/343349.html
