簡體   English   中英

Bash:根據用戶輸入,在本地或遠程計算機上的功能中運行所有命令

[英]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”,則可以這樣做。

如果

  1. run_tool_commands下要執行的所有命令也都存在於遠程系統上,
  2. 所有命令都是可執行文件,而不是別名/功能
  3. 所有這些可執行文件均位於默認路徑中。 (無需在遠程上獲取.bashrc或任何其他文件的源。)

然后,也許這段代碼可以工作:(未經測試):

{ 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM