2017-04-13 113 views
0

我想在我的awk代碼中將mystatus shell變量更新爲新值。 但我猜的語法是不正確的,也 我曾嘗試申報或EVAL,但沒有任何工程 您好,請幫我這個更改AWK條件塊內的Shell腳本變量值

我的代碼:

mystatus="resolved" -- shell variable 
    awk 'BEGIN { print "<table>" } -- awk code to write in new file 
    { 
    print "<tr><td>" $1 "</td><td>" $2 "</td><tr>" 
    if ($1=="stopped") mystatus="problem" -- change shell variable value 
    } 
    END { print "</table>" }' filestats.txt > email.html 
    echo $mystatus -- variabe value not getting changed. 
+0

你不能以這種方式更改shell變量。將其寫入awk中的文件並將其讀取到shell中的變量中。 –

回答

0

像這樣(未經) :

mystatus="resolved"       # shell variable 
awk ' 
BEGIN { print "<table>" }     # awk code to write in new file 
{ 
    print "<tr><td>" $1 "</td><td>" $2 "</td><tr>" 
    if ($1=="stopped") { 
     mystatus="mystatus.txt" 
     print "problem" > mystatus   # write to a file instead 
     close(mystatus) 
    } # I WAS MISSING I WAS MISSING I WAS MISSING I WAS MISSING I WAS MISSING 
} 
END { print "</table>" }' filestats.txt > email.html 
read mystatus < mystatus.txt    # read the status from the file 
echo $mystatus        # variabe value not getting changed. 

一些bash純粹可以評論如果這是從文件讀取任何方式的變量?

+0

james:上述代碼出現錯誤 ' END {print「」} awk:cmd。行:8:^語法錯誤 awk:cmd。第9行:END {print「」} awk:cmd。行:9:^意外的換行符或字符串結尾' – aejaz

+0

一個'}'丟失。 –

+0

沒有好友!代碼執行得很好,但mystatus.txt沒有創建! – aejaz