在 solaris 的 shell 脚本中的 ssh 会话中使用 sed 命令有困难
Difficulty in using sed command in ssh session in shell script in solaris
我正在尝试在 ssh 会话中做这样的事情:
脚本
ssh remoteservername "
col=`sed -n "8p" /tmp/temp.txt`
echo $col>>/tmp/Ankur.txt
"
这不起作用,它正在打印空行而不是文本我想存储在 col 变量中的内容,为什么这样,而且它起作用了:
ssh remoteservername "
sed -n "8p" /tmp/temp.txt>>/tmp/Ankur.txt
"
这个 Ankur.txt 文件在远程服务器上....主要重点是如何在变量中获取命令的输出,以便我可以进一步使用它。
请告诉我如何让它工作。
谢谢
当您使用双引号时,变量名称将在传递之前展开,因此 $col
在远程服务器上 运行 之前在本地展开。您可以像 $col
一样转义 $
或在其周围使用单引号,这可能更好,因为您也想在命令中使用双引号
ssh remoteservername '
col=`sed -n "8p" /tmp/temp.txt`
echo $col>>/tmp/Ankur.txt
'
不更改引号
ssh remoteservername 'sed -n "8p" /tmp/temp.txt >> /tmp/Ankur.txt'
如您所述,通过将输出直接重定向到文件中,仍然有效。这样就避免了上面双引号的变量扩展问题。
如果你要执行很多步骤,你可能只想在 remoteservername
上创建一个脚本并在你的 ssh
命令中调用它,而不是在同一个命令上做很多事情行。
您可以使用本地文件来执行复杂的命令,并通过 SSH 在远程计算机中使用变量,如下所示。
1. Create a input file 'input_file.txt'
#-- input_file.txt
col=`sed -n "8p" /tmp/temp.txt`
echo $col>>/tmp/Ankur.txt
2. Execute the commands of input file in remote server via SSH
ssh remoteservername "sh -s" < input_file.txt
我正在尝试在 ssh 会话中做这样的事情:
脚本
ssh remoteservername "
col=`sed -n "8p" /tmp/temp.txt`
echo $col>>/tmp/Ankur.txt
"
这不起作用,它正在打印空行而不是文本我想存储在 col 变量中的内容,为什么这样,而且它起作用了:
ssh remoteservername "
sed -n "8p" /tmp/temp.txt>>/tmp/Ankur.txt
"
这个 Ankur.txt 文件在远程服务器上....主要重点是如何在变量中获取命令的输出,以便我可以进一步使用它。
请告诉我如何让它工作。
谢谢
当您使用双引号时,变量名称将在传递之前展开,因此 $col
在远程服务器上 运行 之前在本地展开。您可以像 $col
一样转义 $
或在其周围使用单引号,这可能更好,因为您也想在命令中使用双引号
ssh remoteservername '
col=`sed -n "8p" /tmp/temp.txt`
echo $col>>/tmp/Ankur.txt
'
不更改引号
ssh remoteservername 'sed -n "8p" /tmp/temp.txt >> /tmp/Ankur.txt'
如您所述,通过将输出直接重定向到文件中,仍然有效。这样就避免了上面双引号的变量扩展问题。
如果你要执行很多步骤,你可能只想在 remoteservername
上创建一个脚本并在你的 ssh
命令中调用它,而不是在同一个命令上做很多事情行。
您可以使用本地文件来执行复杂的命令,并通过 SSH 在远程计算机中使用变量,如下所示。
1. Create a input file 'input_file.txt'
#-- input_file.txt col=`sed -n "8p" /tmp/temp.txt` echo $col>>/tmp/Ankur.txt
2. Execute the commands of input file in remote server via SSH
ssh remoteservername "sh -s" < input_file.txt