[英]Bash: Based on user input run all commands in a function on local OR on remote machine
我有一個bash函數,它將數組作為參數,它執行多個命令。
根據用戶輸入,我想在本地或遠程計算機上運行此方法中的所有命令。 它在命令中有很多引號,用“”回顯將變得很丑陋。
This is how I am invoking the function right now:
run_tool_commands "${ARGS[@]}"
function run_tool_commands {
ARGS=("$@")
.. Loads of commands here
}
if [ case 1 ]; then
# run locally
else
# run remotely
fi
這似乎很有幫助,但是如果我將方法文本傳遞到“ here document”,則可以這樣做。
如果
run_tool_commands
下要執行的所有命令也都存在於遠程系統上, 然后,也許這段代碼可以工作:(未經測試):
{ declare -f run_tool_commands; echo run_tool_commands "${ARGS[@]}"; } | ssh -t user@host
要么
{ declare -f run_tool_commands;
echo -n run_tool_commands;
for arg in "${ARGS[@]}"; do
echo -ne "\"$t\" ";
done; } | ssh -t user@host
使用for循環,以保留參數周圍的引號。 (可能需要,也可能不需要,未經測試。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.