LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell scipting: append output of a command to a variable (http://www.linuxquestions.org/questions/linux-newbie-8/shell-scipting-append-output-of-a-command-to-a-variable-356404/)

jonhewer 08-24-2005 04:08 AM

shell scipting: append output of a command to a variable
 
hi

doing a little bit of shell scripting.....

it's possible to append the output of a command to a file:

eg. echo "hello" >> file

but is there an equivalent to append the output to a variable? (i'm building up a log message which i want to both write to a log file and email)

the only way i currently know how to do this is:

var=${var}`echo "hello"`

thanks for your help
jon

slackie1000 08-24-2005 04:41 AM

hi there,
Code:

var=`echo "Hello World"`
won't work?
regards
slackie1000

jonhewer 08-24-2005 04:48 AM

Quote:

Originally posted by slackie1000
hi there,
Code:

var=`echo "Hello World"`
won't work?
regards
slackie1000

works for me....:confused:

slackie1000 08-24-2005 05:31 AM

hi there,
so, if it works... what is your question? :confused:
i thought, you want to have the output of a command assigned to a variable.
regards,
slackie1000

jonhewer 08-24-2005 05:34 AM

i want to append the output to a variable, ie. keeping its existing value, and appending the new data

var=${var}`echo "hello"` will do it, but i was wondering whether there's a cleaner way using the >> operator maybe?

theYinYeti 08-24-2005 06:04 AM

I know two methods:
1/ You don't need a line break:
Code:

var="${var}$(/path/to/command params)"
2/ You need a line break:
Code:

var="$(echo "$var"; /path/to/command params)"
Yves.

jonhewer 08-24-2005 06:09 AM

thanks theYinYeti that second method is handy

is it possible to append the stderr of a command to a variable?

theYinYeti 08-24-2005 06:33 AM

Of course. Instead of writing:
/path/to/command params

you write:
/path/to/command params 2>&1
if you want both stdout and stderr,

/path/to/command params 2>&1 >/dev/null
if you only want stderr.

Yves.

jonhewer 08-24-2005 06:36 AM

ah of course, cheers :)

slackie1000 08-24-2005 06:41 AM

*slackie1000 hits his head in the wall and buys an english dictionary*
sorry mate...
my mistake..
regards,
slackie1000

jonhewer 08-24-2005 06:42 AM

lol, no worries


All times are GMT -5. The time now is 02:29 PM.