views:

95

answers:

2

Hi there! I have a small bash script that greps/awk paragraph by using a keyword.

But after adding in the extra codes : set var = "(......)" it only prints a blank line and not the paragraph.

So I would like to ask if anyone knows how to properly pass the awk output into a variable for outputting?

My codes:

#!/bin/sh

set var = "(awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/FileHeader/' /root/Desktop
/logs/Default.log)"
echo $var;

Thanks!

A: 

the sh way to capture the output of a process is

FOO=`process params`

in your case it would be

#!/bin/sh

VAR=`awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/FileHeader/' /root/Desktop/logs/Default.log`
echo $VAR
lesmana
Can't work "line 5: awk BEGIN{RS=ORS=\n\n: command not found"...
JavaNoob
use $() wherever possible!
@JavaNoob, you have to be carefull with backslashes inside the backticks. see here http://mywiki.wooledge.org/BashFAQ/082
lesmana
+2  A: 
  • You need to use "command substitution". Place the command inside either backticks, `COMMAND` or, in a pair of parentheses preceded by a dollar sign, $(COMMAND).
  • To set a variable you don't need to use set and you don't need spaces before and after the =.

Try this:

var=$(awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/FileHeader/' /root/Desktop/logs/Default.log)
echo $var
dogbane