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 is empty in remote host !!
Locally:
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
I followed this link for awk running on remote host
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.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.
(I was tempted to make a more substantial refactoring of your Awk script, but that would perhaps obscure the point.)