简体   繁体   English

如何将bash变量传递给cmd?

[英]How to pass bash variable to cmd?

how can I pass $line to the cmd command? 如何将$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   

Basically, if you want to interpolate a variable into a bash string you need to use double quotes instead of single quotes: 基本上,如果要将变量插值到bash字符串中,则需要使用双引号而不是单引号:

str="${var} foo bar" # works
str='${var} for bar' # works not

In the special case that you are running ssh commands inside a while loop, I strongly recommend to pass /dev/tty explicitly as input to the command since once the remote command should read from stdin - for whatever reason - it will slurp stdin of the while loop otherwise: 在特殊情况下,如果您在while循环内运行ssh命令,我强烈建议显式传递/dev/tty作为命令的输入,因为一旦远程命令应从stdin中读取(无论出于何种原因),它将使stdin吞噬while循环,否则:

while read line ; do
    ssh ... -- "${line} ..."  < /dev/tty # Pass tty to stdin
done < input.txt

Note: The above command will work only if the process has been started in a terminal. 注意:仅当在终端中启动了进程时,以上命令才有效。 If the process is not running in a terminal, you need to pass something else as stdin for the inner ssh command. 如果该进程未在终端中运行,则需要将其他内容作为stdin传递给内部ssh命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM