awk remote ssh命令 [英] awk remote ssh command
问题描述
我有一个运行远程awk命令的bash脚本,但是我想我没有正确地转义特殊字符,因为在远程服务器上没有文件生成.仍然我没有错误.
I have a bash script that run a remote awk command but I guess I haven't correctly escape specials characters since no file is generated on the remote server. Still I have no error.
我的变量在本地声明,可以在没有问题的情况下远程使用(脚本的其他部分确认了这一点).
My variables are declared locally and can be used remotely without issue (other part of the script confirm this).
ssh -q -t server '
logfiles=$(find /var/log/httpd/ -type f -name *access_log -cmin -'"$past"')
for log in $logfiles;
awk -vDate=\`date -d'now-'"$past"' minutes' +[%d/%b/%Y:%H:%M:%S\` ' { if \(\$4 > Date\) print \$0}' $log | sort |uniq -c |sort -n | tail | cut -d " " -f 11,15,16
done
'
谢谢!
通过此脚本
#!/bin/bsh
logfiles=$(find /var/log/httpd/ -type f -name *access_log -cmin -120)
for log in $logfiles; do
awk -vDate=`date -d'now-120 minutes' +[%d/%b/%Y:%H:%M:%S` ' { if ($4 > Date) print $0}' $log | sort |uniq -c |sort -n | tail | cut -d " " -f 11,15,16 > /root/httpd.log;
done
像这样工作
ssh user @ host< script.sh
ssh user@host < script.sh
当我从控制台运行相同的脚本时:
When I run the same script from the console :
ssh -q -t $apache '
logfiles=$(find /var/log/httpd/ -type f -name *access_log -cmin -120)
for log in $logfiles; do
awk -vDate=`date -d'now-120 minutes' +[%d/%b/%Y:%H:%M:%S` ' { if ($4 > Date) print $0}' $log | sort |uniq -c |sort -n | tail | cut -d " " -f 11,15,16 > /root/httpd.log;
done'
-bash: syntax error near unexpected token `('
所以我试图逃脱括号
ssh -q -t $apache '
logfiles=$(find /var/log/httpd/ -type f -name *access_log -cmin -120)
for log in $logfiles; do
awk -vDate=`date -d'now-120 minutes' +[%d/%b/%Y:%H:%M:%S` ' { if \($4 > Date\) print $0}' /var/log/httpd/royalcanin_com.access_log | sort |uniq -c |sort -n | tail | cut -d " " -f 11,15,16 > /root/httpd.log;
done'
但是什么也没产生.
已在服务器上生成了文件,但此文件为空:
Having the file generated on the server but empty with this:
awk -vDate=\`date -d'now-120 minutes' +[%d/%b/%Y:%H:%M:%S\` ' { if '"($4 > Date)"' print $0}' $log | sort |uniq -c |sort -n | tail | cut -d " " -f 11,15,16 > /root/httpd.log;done'
推荐答案
您还需要转义单引号.例如,第一个脚本…
You also need to escape your single quotes. For example, the first script…
ssh -q -t server '
logfiles=$(find /var/log/httpd/ -type f -name *access_log -cmin -'"$past"')
for log in $logfiles;
awk -vDate=\`date -d'now-'"$past"' minutes' +[%d/%b/%Y:%H:%M:%S\` ' { if \(\$4 > Date\) print \$0}' $log | sort |uniq -c |sort -n | tail | cut -d " " -f 11,15,16
done
'
…必须真正写成:
ssh -q -t server '
logfiles=$(find /var/log/httpd/ -type f -name *access_log -cmin -'\''"$past"'\'')
for log in $logfiles;
awk -vDate=\`date -d'\''now-'\''"$past"'\'' minutes'\'' +[%d/%b/%Y:%H:%M:%S\` '\'' { if \(\$4 > Date\) print \$0}'\'' $log | sort |uniq -c |sort -n | tail | cut -d " " -f 11,15,16
done
'
这篇关于awk remote ssh命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!