Linux Bash:根据用户输入,在本地或远程计算机上运行函数中的所有命令
我有一个bash函数,它将数组作为参数,并执行多个命令 根据用户输入,我希望在本地或远程计算机上运行此方法中的所有命令。它在命令中有许多引号,使用“”将变得很难看Linux Bash:根据用户输入,在本地或远程计算机上运行函数中的所有命令,linux,bash,shell,ssh,Linux,Bash,Shell,Ssh,我有一个bash函数,它将数组作为参数,并执行多个命令 根据用户输入,我希望在本地或远程计算机上运行此方法中的所有命令。它在命令中有许多引号,使用“”将变得很难看 This is how I am invoking the function right now: run_tool_commands "${ARGS[@]}" function run_tool_commands { ARGS=("$@") .. Loads of commands here } i
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
似乎很有用,但如果我将方法文本管道传输到“heredocument”,这是可能的 如果
run\u tool\u commands
下执行的所有命令也存在于远程系统中使用for循环,在参数周围保留引号。(可能需要也可能不需要,未测试。)使用
scp
将脚本复制到远程计算机。然后使用ssh登录并运行脚本。@sujin,哪个脚本?我只想运行一组命令,不幸的是,我无法将它们分离成脚本。login
通过远程pc使用ssh
然后您可以运行所有命令。使用这种方法,将为每个命令建立新的ssh连接。
{ 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