如何将 $line
传递给 cmd
命令?
#!/bin/bash
while read line
do
sshpass -p "..." ssh -o StrictHostKeyChecking=no -tt windows@172....-p 333 cmd /c "cd C:\ & download_give_id.exe '$@$line' "
done <apps.txt
基本上,如果你想将变量插入到 bash 字符串中,你需要使用双引号而不是单引号:
str="${var} foo bar" # works
str='${var} for bar' # works not
在 while 循环内运行 ssh 命令的特殊情况下,我强烈建议将 /dev/tty
作为命令的输入显式传递,因为一旦远程命令应从标准输入读取 -无论出于何种原因 - 否则它将吞噬 while 循环的标准输入:
while read line ; do
ssh ... -- "${line} ..." < /dev/tty # Pass tty to stdin
done < input.txt
注意:以上命令只有在进程已经在终端中启动时才有效。如果该进程未在终端中运行,您需要传递其他内容作为内部 ssh 命令的标准输入。
我是一名优秀的程序员,十分优秀!