2017-04-13 115 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