2013-04-16 89 views
3

我正在運行一個運行並從CSF防火牆接收變量的linux bash腳本。 CSF防火牆發出的命令是system($config{RT_ACTION},$ip,$check,$block,$cnt,$mails);其中$config{RT_ACTION}是我的腳本的路徑。Bash變量衝突

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

問題是當我嘗試運行此命令來獲取[email protected]

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

如果它不明確,$電子郵件傳遞給我的腳本轉換爲$ 5的信息,我想使用awk位於第5列也轉換爲$ 5所以不是$ 5輸出從提取@ domain.com輸出$郵件的全部內容。我錯過了什麼?爲什麼awk不能將myvar設置爲[email protected]

回答

0

什麼:

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

,或者使用AWK:

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

它的工作對我來說...

+0

實際上,管道意味着一個子shell一樣,用反引號您捕捉表達。 (反引號與'$()'同義) – kojiro

+0

這是真的,它只是我用來調用$(「subshel​​l運算符」和「只是」反向運算符「...:D – user435943

+0

第二個解決方案完美地工作!謝謝你那! – hiphopsmurf

0

希望一些這方面是說明性的。最終,我只是表明,你已經工作,除非你試圖定義變量字面與美元符號已經在它。

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