简体   繁体   English

在远程服务器上执行带有功能的awk脚本

[英]execute awk script with functions on remote servers

I have a awk scrip which includes functions like below: 我有一个awk脚本,其中包括如下功能:

for file in logfile.log; 
do 
    echo "File Name- "$file; 
    grep -v "Some Text" $file | awk -F [:\ ] '{sub(/T/, " "); if($8~500) CNT[$1":"$2]++} END {for (c in CNT) print c, "--", CNT[c]}' | sort; 
    echo "==============Done with $file=================="; 
done

But i want to execute this script on many remove servers, so i just login to our deployment (Which basically have access to all the server that i want to execute) and i am getting many compilation errors, I am using the following code: 但是我想在许多删除服务器上执行此脚本,所以我只是登录到我们的部署(基本上可以访问我要执行的所有服务器),并且遇到很多编译错误,我在使用以下代码:

for server in 01 02 03 04; do 
ssh web_user@webserver$server '
hostname -f;
cd /logs/dir/path;
for file in log-file-name.log; 
do 
    echo "File Name- "$file; 
    grep -v "Some Text" $file | awk -F [:\ ] '{sub(/T/, " "); if($8~500) CNT[$1":"$2]++} END {for (c in CNT) print c, "--", CNT[c]}' | sort; 
    echo "==============Done with $file=================="; 
done
'  2>&1 | grep -v 'THIS SYSTEM IS RESTRICTED'; done

Can someone please help me on this? 有人可以帮我吗? My problem is though this script dosent have any compilation errors, i am getting many compilation errors. 我的问题是尽管此脚本有任何编译错误,但我遇到了许多编译错误。

Thank you in advance!! 先感谢您!!

In bash , a single quote ( ' ) escapes all following characters except for other single quotes. bash ,除其他单引号外,单引号( ' )会转义所有后续字符。 Because of that, things like {sub... in the OP will be interpreted by your local script. 因此,OP中的{sub...将由您的本地脚本解释。

To pass the script this way to the remote bash (the one opened by ssh ), you need to: 要将脚本以这种方式传递给远程bash (由ssh打开的脚本),您需要:

  • Enclose the entire script within single quotes. 将整个脚本用单引号引起来。

  • Replace every single quote ' inside your script (not the outer ones!) by '\\'' . '\\''替换脚本中的每个单引号' (而不是外部的!)。 How this works: the first ' ends the current quotation; 工作原理:第一个'结束当前报价; the \\' includes a quoted single apostrophe; \\'包含单引号。 the last ' reopens the quotation. 最后一个'重新打开报价。

Example script: 示例脚本:

foo=bar
echo 'foo=$foo'
echo "foo=$foo"

Expected output: 预期产量:

foo=$foo
foo=bar

Example invocation "by hand", ie, this is something you would type in a terminal: 示例“手工”调用,即,您可以在终端中键入以下内容:

ssh server '
foo=bar
echo '\''foo=$foo'\''
echo "foo=$foo"
'

However, there is a way to avoid all this quoting business: you could save the original script (with normal single quotes) in a file myscript , and run instead: 但是,有一种方法可以避免所有这样的报价工作:您可以将原始脚本(使用普通的单引号)保存在文件myscript ,然后运行:

ssh server "$(cat myscript)"

This way, the local bash will no longer be confused by the single quotes appearing in myscript , because the entire content will be quoted. 这样,本地bash将不再与myscript出现的单引号引起混淆,因为整个内容将被引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM