3

我正在研究一个运行并从 CSF 防火墙接收变量的 linux bash 脚本。CSF Firewall 发出的命令是system($config{RT_ACTION},$ip,$check,$block,$cnt,$mails);$config{RT_ACTION}的脚本的路径。

$mails=2013-04-16 11:57:14 1US8Fq-0001VC-Uu <= from@domain.com H=reverse.trace.of.ipaddress (server) [xxx.xxx.xxx.xxx]:PORT I=[xxx.xxx.xxx.xxx]:PORT P=esmtp S=5964 id=SOMEIDHERE$@com T="EMAILSUBJECT" from <from@domain.com> for to@domain.com

当我尝试运行此命令以获取 from@domain.com 时,问题就出现了。

DUMPED=$5
myvar=$(echo "$DUMPED" | awk '{print $5}')

如果不清楚,$mails 将传递给我的脚本,该脚本转换为 $5,而我想用 awk 提取的信息位于第 5 列,也转换为 $5,因此它输出的不是从@domain.com 输出的 $5 $mails 的全部内容。我错过了什么?为什么 awk 不将 myvar 设置为 from@domain.com?

4

2 回答 2

0

关于什么:

DUMPED=$5
myvar=`echo $DUMPED | cut -d" " -f5`

或者,使用 AWK:

DUMPED=$5
myvar=`echo $DUMPED | awk '{print $5}'`

它对我有用...

于 2013-04-16T23:40:15.450 回答
0

希望其中一些是说明性的。最终,我只是展示你已经拥有的东西,除非你试图用已经在其中的美元符号来定义变量

$ # Define mails. Don't do $mails=something, that will fail.
$ mails='2013-04-16 11:57:14 1US8Fq-0001VC-Uu <= from@domain.com H=reverse.trace.of.ipaddress (server) [xxx.xxx.xxx.xxx]:PORT I=[xxx.xxx.xxx.xxx]:PORT P=esmtp S=5964 id=SOMEIDHERE$@com T="EMAILSUBJECT" from <from@domain.com> for to@domain.com'
$ # Direct the value of $mails to awk's standard input using a here string:
$ awk '{print $5}' <<< "$mails"
from@domain.com
$ # Direct the value of $mails to awk's standard input using echo and a pipe:
$ echo "$mails"| awk '{print $5}'
from@domain.com
$ # Assign the fifth word of "$mails" to the name "myvar" using read;
$ read _ _ _ _ myvar _ <<< "$mails"
$ echo "$myvar"
from@domain.com
于 2013-04-16T23:49:15.000 回答