Bash变量冲突

时间:2013-04-16 23:17:41

标签: linux bash shell

我正在开发一个运行的linux bash脚本,并从CSF Firewall接收变量。 CSF防火墙发出的命令是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美元而不是$ 5输出from@domain.com它输出$邮件的全部内容。我错过了什么?为什么不将awk设置为from@domain.com?

2 个答案:

答案 0 :(得分:0)

怎么样:

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

或者,使用AWK:

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

它对我有用......

答案 1 :(得分: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
相关问题