通过SSH在远程服务器上运行awk comand [英] Run awk comand on a remote server through ssh
问题描述
我正在尝试从远程服务器上的文件中获取一组特定的值.通过终端执行该命令时,该命令可以正常工作.
I'm trying to get a specific set of values from a file on a remote server. The command works fine when executing that through terminal.
第一个ssh命令
sshpass -p password ssh -T user@ip
第二个Awk命令
find /opt/Info_Source/*daily* -type f -mtime -1 -exec zcat {} \; 2>/dev/null | awk -F, -v OFS=',' '$5 ~ /Valid/ && length($2) {print $2}'
但是如果我将两者结合在脚本中
but if I combine both of them in a script
#!/bin/ksh
emp_id=`sshpass -p password ssh -T user@ip -q << EOF
find /opt/Info_Source/*daily* -type f -mtime -1 -exec zcat {} \; 2>/dev/null |
awk -F, '$5 ~ /Valid/ && length($2) {print $2}'
exit
EOF `
echo "$emp_id" > Request.txt
我收到此错误
awk: ~ /Valid/ && length() {print }
awk: ^ syntax error
有什么主意我可以解决吗?
Any idea how I can fix that?
推荐答案
在发送脚本之前,awk
变量在本地扩展为shell变量.引用here文档定界符以按原样发送here文档.
The awk
variables are expanded as shell variables locally before the script is sent. Quote the here document delimiter to send the here document as-is.
(此外,摆脱命令替换,而直接将其写入文件即可.)
(Also, get rid of the command substitution and just write directly to the file.)
#!/bin/ksh
sshpass -p password ssh -T user@ip -q << 'EOF' > Request.txt
find /opt/Info_Source/*daily* -type f -mtime -1 -exec zcat {} \; 2>/dev/null |
awk -F, '$5 ~ /Valid/ && length($2) {print $2}'
EOF
这篇关于通过SSH在远程服务器上运行awk comand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!