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:
ToddAndMargo <[log in to unmask]>
Reply To:
ToddAndMargo <[log in to unmask]>
Date:
Sat, 7 Jun 2014 19:17:18 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (77 lines)
On 06/07/2014 06:00 PM, zxq9 wrote:
> 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.
>

Hi zxq9,

    I see what I did wrong.  Thank you!

    It is a small log.  Only about 20 line
of text in it.

-T


-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ATOM RSS1 RSS2