On 06/07/2014 07:17 PM, ToddAndMargo wrote:
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
Hi Guys,
Thank you for the help. If you are curious, this is
what became of all the bash syntax questions.
-T
DownloadApp () {
# $1 Application Name
# RtnState: 0 = unknown (default)
# 1 = no update found
# 2 = update found
# 3 = error
App=$1
Status=0
Col_Grey="\x1b[30;01m"
Col_Red="\x1b[31;01m"
Col_Blue="\x1b[38;49;00m"
Col_Reset="\x1b[39;49;00m"
AppDot="${App}: "; AppDot="${AppDot:0:30}"
eval "$App"; RtnState=$?
if [ "$RtnState" == "3" ]; then MasterStatus=1; fi
case $RtnState in
"1" )
StatusLog="${StatusLog}\n${AppDot} ${Col_Grey}no new
updates${Col_Reset}"
;;
"2" )
StatusLog="${StatusLog}\n${AppDot} ${Col_Blue}new update
downloaded${Col_Reset}"
;;
"3" )
StatusLog="${StatusLog}\n${AppDot} ${Col_Red}ERROR. See
details above${Col_Reset}"
MasterStatus="1"
;;
*)
StatusLog="${StatusLog}\n${AppDot} ${Col_Red}did not return
a recognized status <{$RtnState}>${Col_Reset}"
esac
tput sgr0 # Reset text attributes to normal without clear
}