SCIENTIFIC-LINUX-USERS Archives

June 2014

SCIENTIFIC-LINUX-USERS@LISTSERV.FNAL.GOV

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Reply To:
Date:
Sun, 8 Jun 2014 10:00:44 +0900
Content-Type:
text/plain
Parts/Attachments:
text/plain (59 lines)
On Saturday 07 June 2014 17:38:35 you wrote:
> Hi All,
> 
> Bash: I am trying to use a variable to hold
> a log and add to it as I go.  I can do this
> with a temp file, but I'd rather do it with
> a variable.
> 
> I have gotten this far:
> 
> A=$(echo -e  "abc\n")
> A="$A"$(echo -e  "def\n")
> A="$A"$(echo -e  "ghi\n")
> 
> echo $A
> abcdefghi
> 
> echo -e $A
> abcdefghi
> 
> What am I doing wrong?  Is it better to just break
> down and just use a file?

The assignment doesn't need echo, and the series of "echo -e" is swallowing 
the newlines you expected to have in place.

Here's an example:

-----------------
ceverett@jalapeno ~/Code/bash $ cat var-log.bash 
#! /bin/bash
a="start of log"
for newdata in $(seq 1 10)
do
  a="$a\n$newdata"
done
echo -e "$a"

ceverett@jalapeno ~/Code/bash $ ./var-log.bash 
start of log
1
2
3
4
5
6
7
8
9
10
-----------------

But really, you shouldn't store a log in a variable, especially if it might 
receive a lot of traffic (and especially since you lose everything in the 
event of a program interruption of any sort, which defeats the purpose). You 
could accumulate a bit of data in a variable, but you should always flush it 
to a file. The append redirect operator was designed specifically to make this 
easy; not using it is going against the grain.

ATOM RSS1 RSS2