我正在运行的Linux bash脚本运行并从CSF防火墙接收variables。 CSF防火墙发出的命令是system($config{RT_ACTION},$ip,$check,$block,$cnt,$mails);
$config{RT_ACTION}
是我的脚本的path。
$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}')
如果不清楚的话,$邮件被传递给我的脚本,转换为$ 5,我想用awk提取的信息位于第5列,也可以转换为$ 5,而不是$ 5输出from@domain.com它输出$邮件的全部内容。 我错过了什么? 为什么不awk将myvar设置为from@domain.com?
关于什么:
DUMPED=$5 myvar=`echo $DUMPED | cut -d" " -f5`
或者,使用AWK:
DUMPED=$5 myvar=`echo $DUMPED | awk '{print $5}'`
它为我工作…
希望有一些是说明性的。 最后,我只是表明,你已经工作,除非你试图用美元符号已经在字面上定义变量。
$ # 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