通过 ssh 命令从另一个 shell 文件传递​​ shell 文件中 SQL 语句的参数

发布于 2025-01-09 10:37:52 字数 529 浏览 0 评论 0原文

我将命令行参数传递给 shell 文件,即 allocateRole.sh,其中包含一个 SQL 命令,该命令将使用这些参数,如下所示

ssh -o StrictHostKeyChecking=no -T $key < /oracle/oracle_user/makhshif/./assignRole.sh name open_mode >> /oracle/oracle_user/dftest.txt

这给了我错误,并且不接受 nameopen_mode< 的参数/strong> 并给出错误,但如果我在 ssh 命令之外执行语句,例如:

/oracle/oracle_user/makhshif/./assignRole.sh name open_mode

这运行良好

ssh 命令有什么问题以及我应该如何调整这些参数以便 shell 脚本 allocateRole.sh 可以接受这些参数

I am passing command line arguments to a shell file i.e assignRole.sh which contains an SQL command which will use these arguments like below

ssh -o StrictHostKeyChecking=no -T $key < /oracle/oracle_user/makhshif/./assignRole.sh name open_mode >> /oracle/oracle_user/dftest.txt

This gives me error and does not accept arguments of name and open_mode and gives error, but if I execute the statement outside of ssh command like:

/oracle/oracle_user/makhshif/./assignRole.sh name open_mode

This runs fine

What is the problem with ssh command and how should I adjust these parameters so these can be accepted for the shell script assignRole.sh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兰花执着 2025-01-16 10:37:52
 < /oracle/oracle_user/makhshif/./assignRole.sh

此命令将该文件的内容发送到标准输入。显然它无法处理您尚未发送到远程计算机的变量。只需预处理您的脚本或在远程计算机上创建一个脚本并使用参数调用它

尽管传递这样的变量更容易:

ssh -o StrictHostKeyChecking=no -T $key "var1=$var1 var2=$var2" < /oracle/oracle_user/makhshif/./assignRole.sh name open_mode >> /oracle/oracle_user/dftest.txt

例如我在所有集群节点上执行更新脚本的函数:

# functions:
ssh_exec(){
  local DESCR="$1"; shift
  local SCRIPT="$1"; shift
  local hosts=("$@")
  echo =================================================
  echo =   $DESCR
  echo = Going to execute $SCRIPT...
  read -a res -p "Enter 'skip' to skip this step or press Enter to execute: "
  if [[ $res = "skip" ]]
  then
    echo Skipping $SCRIPT...
  else
    echo Executing $SCRIPT...
    for host in "${hosts[@]}"
        do
          local cur=${!host}
          echo Executing $SCRIPT on $host - $cur...
          sshpass -p "$rootpass" ssh -o "StrictHostKeyChecking no" root@${cur} \
            "ns1=$ns1 ns2=$ns2 search=$search zoo1=$zoo1 zoo2=$zoo2 zoo3=$zoo3 node0=$node0 pass=$pass CURIP=$cur CURHOST=$host bash -s" \
            <$SCRIPT >log-$SCRIPT-$cur.log 2>&1
          echo Done.
        done
    echo =================================================
  fi
}

然后我像这样使用它:

read -p "Please check that Solr started successfully and Press [Enter] key to continue..."

#Solr configset and collections:
ssh_exec "Solr configset and collections" script06.sh zoo1 zoo2 zoo3

此命令执行 < code>script06.sh 在 3 台服务器上(zoo1、zoo2、zoo3)

 < /oracle/oracle_user/makhshif/./assignRole.sh

This commands sends a content of that file to stdin. So obviously it can't process variables that you haven't send to remote machine. Just preprocess your script or create a script on remote machine and call it with arguments

Though it's even easier to pass variables like this:

ssh -o StrictHostKeyChecking=no -T $key "var1=$var1 var2=$var2" < /oracle/oracle_user/makhshif/./assignRole.sh name open_mode >> /oracle/oracle_user/dftest.txt

For example my function for executing update scripts on all cluster nodes:

# functions:
ssh_exec(){
  local DESCR="$1"; shift
  local SCRIPT="$1"; shift
  local hosts=("$@")
  echo =================================================
  echo =   $DESCR
  echo = Going to execute $SCRIPT...
  read -a res -p "Enter 'skip' to skip this step or press Enter to execute: "
  if [[ $res = "skip" ]]
  then
    echo Skipping $SCRIPT...
  else
    echo Executing $SCRIPT...
    for host in "${hosts[@]}"
        do
          local cur=${!host}
          echo Executing $SCRIPT on $host - $cur...
          sshpass -p "$rootpass" ssh -o "StrictHostKeyChecking no" root@${cur} \
            "ns1=$ns1 ns2=$ns2 search=$search zoo1=$zoo1 zoo2=$zoo2 zoo3=$zoo3 node0=$node0 pass=$pass CURIP=$cur CURHOST=$host bash -s" \
            <$SCRIPT >log-$SCRIPT-$cur.log 2>&1
          echo Done.
        done
    echo =================================================
  fi
}

Then I use it like this:

read -p "Please check that Solr started successfully and Press [Enter] key to continue..."

#Solr configset and collections:
ssh_exec "Solr configset and collections" script06.sh zoo1 zoo2 zoo3

This command executes script06.sh on 3 servers (zoo1,zoo2,zoo3)

和我恋爱吧 2025-01-16 10:37:52

正如 Sayan 所说,使用 < 会重定向本地运行 assignRole.sh 脚本的输出,但您希望使用参数在远程主机上执行该脚本。

将整个命令作为最后一个参数传递给 ssh,用引号括起来:

ssh -o StrictHostKeyChecking=no -T $key "/oracle/oracle_user/makhshif/./assignRole.sh name open_mode" >> /oracle/oracle_user/dftest.txt

或分成多行以提高可读性:

ssh -o StrictHostKeyChecking=no -T $key \ 
  "/oracle/oracle_user/makhshif/./assignRole.sh name open_mode" \
  >> /oracle/oracle_user/dftest.txt

As Sayan said, using < redirects the output of running the assignRole.sh script locally, but you want to execute that script on the remote host, with the arguments.

Pass the whole command as the final argument to ssh, in quotes:

ssh -o StrictHostKeyChecking=no -T $key "/oracle/oracle_user/makhshif/./assignRole.sh name open_mode" >> /oracle/oracle_user/dftest.txt

or split into multiple lines for readability:

ssh -o StrictHostKeyChecking=no -T $key \ 
  "/oracle/oracle_user/makhshif/./assignRole.sh name open_mode" \
  >> /oracle/oracle_user/dftest.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文