在远程主机中运行awk命令失败 [英] Running awk command in remote host fails
问题描述
我正在运行一个Shell脚本以在远程主机上运行awk命令. awk命令在本地运行良好,但是在远程主机上运行失败(重定向的文件为空):
I am running a shell script to run awk command on a remote host. The awk command runs fine locally, but when run for the remote host fails (the redirected file is empty):
以下是在远程主机上运行的脚本:
Here is the script run on remote host:
host=160.110.235.30
IFM_MOUNT=/opt/insiteone/fuse-mount1
ssh -T root@$host << 'EOF'
awk -v p="$IFM_MOUNT" '{a[NR]=$0}{if(match($0,p)>0){for(i=NR-2;i<=NR+7;i++)d[i]=1}}END{for(i=1;i<=NR;i++)if(!d[i])print a[i]} ' smb.conf >> smb.conf.tmp
EOF
smb.conf.tmp在远程主机中为空!
smb.conf.tmp is empty in remote host !!
本地:
cat smb.conf
[DCCAArchive]
comment = DCCA Archive File System
path = /opt/insiteone/fuse-mount/ifm
read only = No
case sensitive = yes
public = yes
case sensitive = yes
writeable = yes
create mask=0777
guest ok = Yes
[DCCAArchive1]
comment = DCCA Archive File System
path = /opt/insiteone/fuse-mount1/ifm
read only = No
case sensitive = yes
public = yes
case sensitive = yes
writeable = yes
create mask=0777
guest ok = Yes
Running awk locally from shell:
IFM_MOUNT=/opt/insiteone/fuse-mount1
awk -v p="$IFM_MOUNT" '{a[NR]=$0}{if(match($0,p)>0){for(i=NR-2;i<=NR+7;i++)d[i]=1}}END{for(i=1;i<=NR;i++)if(!d[i])print a[i]} ' smb.conf >> smb.conf.tmp
Output (deletes the line matching IFM_MOUNT):
[DCCAArchive]
comment = DCCA Archive File System
path = /opt/insiteone/fuse-mount/ifm
read only = No
case sensitive = yes
public = yes
case sensitive = yes
writeable = yes
create mask=0777
guest ok = Yes
对于在远程主机上运行的awk,我遵循了此链接. /p>
I followed this link for awk running on remote host
推荐答案
您选择的引用机制阻止了远程shell查看本地定义的变量IFM_MOUNT
.而是使用双引号将值插入到字符串中.
The quoting mechanism you have chosen prevents the remote shell from seeing the locally defined variable IFM_MOUNT
. Use double quotes instead to have the value interpolated into the string.
host=160.110.235.30
IFM_MOUNT=/opt/insiteone/fuse-mount1
ssh root@$host "awk -v p='$IFM_MOUNT' '
{a[NR]=\$0}
{if(match(\$0,p)>0)
for(i=NR-2;i<=NR+7;i++)
d[i]=1}
END{
for(i=1;i<=NR;i++)if(!d[i])print a[i]} ' smb.conf >> smb.conf.tmp"
请注意,双引号内的单引号实际上并未引用任何内容;因此任何字面的美元符号都必须以反斜杠转义.
Notice how single quotes inside double quotes do not actually quote anything; and so any literal dollar sign needs to be escaped with a backslash.
(我很想对您的Awk脚本进行更实质性的重构,但这可能会掩盖这一点.)
(I was tempted to make a more substantial refactoring of your Awk script, but that would perhaps obscure the point.)
这篇关于在远程主机中运行awk命令失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!