Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Bash:根据用户输入,在本地或远程计算机上运行函数中的所有命令_Linux_Bash_Shell_Ssh - Fatal编程技术网

Linux 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

我有一个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
似乎很有用,但如果我将方法文本管道传输到“heredocument”,这是可能的

如果

  • run\u tool\u commands
    下执行的所有命令也存在于远程系统中
  • 所有命令都是可执行的,而不是别名/函数
  • 所有这些可执行文件都位于默认路径中。(无需在远程服务器上查找.bashrc或任何其他文件。)
  • 那么也许这段代码可以工作:(未测试):


    使用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