Prev: List full path and file name for selected directory
Next: use variable to do some substitution with awk.
From: Lao Ming on 30 Mar 2010 14:26 On Mar 29, 11:29 pm, Bill Marcum <marcumb...(a)bellsouth.net> wrote: > On 2010-03-30, Lao Ming <laoming...(a)gmail.com> wrote: > > > > > > > In the loop, I append a format string to a variable called format: > > > format="$format %s" > > > When the loop completes, I prepend and append quotes and newline. > > > q='"' > > format="${q}${format}\\n${q}" > > > When I echo that format, it has the correct number of months but when > > I printf it, I get null. > > > printf "$format" "$month_args" > > > What did I do wrong? Thanks a bunch. > > Does your loop have its input or output redirected? If so, the loop is > executed in a subshell. It doesn't seem to be an issue with the output since I do obtain correct output -- just not formatted output because some simple problem which I can't see is affecting the format variable. If the subshell affects the format variable below, shouldn't it also affect the accompanying monthly_args variable as well? It must something else I've done or not done. Is there a good source on the web for reading about subshells? While I understand the issue, I have never fully understood every aspect of it. Sorry for bothering you all with what seems to be a trivial issue. I should have been able to figure it out by myself. Thanks. for mm in 01 02 03 04 05 06 07 08 09 10 11 12 do if [ "$mm" -le "$current_mm" ] then kmonthly=`./kdaily "$yyyy" |grep "^${yy}${mm}" |grep -w "k" |wc - l` format="$format %s" monthly_args="$monthly_args $k_monthly" fi done format="\"${format}\\n\"" printf "$format" $monthly_args As to using a variable instead of \" -- all I can say is old habits die hard. :)
From: Janis Papanagnou on 30 Mar 2010 15:40 Lao Ming schrieb: > On Mar 29, 11:29 pm, Bill Marcum <marcumb...(a)bellsouth.net> wrote: >> On 2010-03-30, Lao Ming <laoming...(a)gmail.com> wrote: >> >> >> >> >> >>> In the loop, I append a format string to a variable called format: >>> format="$format %s" >>> When the loop completes, I prepend and append quotes and newline. >>> q='"' >>> format="${q}${format}\\n${q}" >>> When I echo that format, it has the correct number of months but when >>> I printf it, I get null. >>> printf "$format" "$month_args" >>> What did I do wrong? Thanks a bunch. >> Does your loop have its input or output redirected? If so, the loop is >> executed in a subshell. > > It doesn't seem to be an issue with the output since I do obtain > correct output -- just not formatted output because some simple > problem which I can't see is affecting the format variable. If the > subshell affects the format variable below, shouldn't it also affect > the accompanying monthly_args variable as well? It must something > else I've done or not done. > > Is there a good source on the web for reading about subshells? While > I understand the issue, I have never fully understood every aspect of > it. > > Sorry for bothering you all with what seems to be a trivial issue. I > should have been able to figure it out by myself. Thanks. Helpful information to be able to help you is; meaningful sample input data, and expected output (of course correlating to your sample input). Please provide that information and I'm sure there's a simple solution. > > [snip some code] Posting code is indeed helpful if it's clear what you want to achieve. Analysing such 'any_tool|grep|grep|wc' sequences is boring. We don't know what 'any_tool' will produce and bulky grep|grep|wc sequences are mostly unnecessary, anyway. Janis > > As to using a variable instead of \" -- all I can say is old habits > die hard. :) >
From: Bill Marcum on 30 Mar 2010 18:53 On 2010-03-30, Lao Ming <laomingliu(a)gmail.com> wrote: > > Sorry for bothering you all with what seems to be a trivial issue. I > should have been able to figure it out by myself. Thanks. > > > > for mm in 01 02 03 04 05 06 07 08 09 10 11 12 > do > if [ "$mm" -le "$current_mm" ] > then > kmonthly=`./kdaily "$yyyy" |grep "^${yy}${mm}" |grep -w "k" |wc - > l` > > format="$format %s" > monthly_args="$monthly_args $k_monthly" > > fi > done > format="\"${format}\\n\"" > > printf "$format" $monthly_args > You have "kmonthly" in one place and "$k_monthly" in another. -- THEY'RE IN UR BED, EATING UR DREAMZ
From: Lao Ming on 30 Mar 2010 22:36 On Mar 30, 12:40 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com> wrote: > Lao Ming schrieb: > > > > > > > On Mar 29, 11:29 pm, Bill Marcum <marcumb...(a)bellsouth.net> wrote: > >> On 2010-03-30, Lao Ming <laoming...(a)gmail.com> wrote: > > >>> In the loop, I append a format string to a variable called format: > >>> format="$format %s" > >>> When the loop completes, I prepend and append quotes and newline. > >>> q='"' > >>> format="${q}${format}\\n${q}" > >>> When I echo that format, it has the correct number of months but when > >>> I printf it, I get null. > >>> printf "$format" "$month_args" > >>> What did I do wrong? Thanks a bunch. > >> Does your loop have its input or output redirected? If so, the loop is > >> executed in a subshell. > > > It doesn't seem to be an issue with the output since I do obtain > > correct output -- just not formatted output because some simple > > problem which I can't see is affecting the format variable. If the > > subshell affects the format variable below, shouldn't it also affect > > the accompanying monthly_args variable as well? It must something > > else I've done or not done. > > > Is there a good source on the web for reading about subshells? While > > I understand the issue, I have never fully understood every aspect of > > it. > > > Sorry for bothering you all with what seems to be a trivial issue. I > > should have been able to figure it out by myself. Thanks. > > Helpful information to be able to help you is; meaningful sample input > data, and expected output (of course correlating to your sample input). > Please provide that information and I'm sure there's a simple solution. > > > > > [snip some code] > > Posting code is indeed helpful if it's clear what you want to achieve. > Analysing such 'any_tool|grep|grep|wc' sequences is boring. I agree it is boring but I'm sure you recognize that 'wc -l' is only going to produce non-negative integers. If I introduced the other script (kdaily) or the data to be searched ... now *that* would be boring. My only issue is formatting the format strings -- nothing else. :) > We don't > know what 'any_tool' will produce and bulky grep|grep|wc sequences are > mostly unnecessary, anyway. They're only unnecessary if you know a better way which, admittedly, I do not. Hopefully, I am not sounding unappreciative because, be assured, I am sincerely appreciative.
From: Lao Ming on 30 Mar 2010 22:39 On Mar 30, 3:53 pm, Bill Marcum <marcumb...(a)bellsouth.net> wrote: > On 2010-03-30, Lao Ming <laoming...(a)gmail.com> wrote: > > > > > > > Sorry for bothering you all with what seems to be a trivial issue. I > > should have been able to figure it out by myself. Thanks. > > > for mm in 01 02 03 04 05 06 07 08 09 10 11 12 > > do > > if [ "$mm" -le "$current_mm" ] > > then > > kmonthly=`./kdaily "$yyyy" |grep "^${yy}${mm}" |grep -w "k" |wc - > > l` > > > format="$format %s" > > monthly_args="$monthly_args $k_monthly" > > > fi > > done > > format="\"${format}\\n\"" > > > printf "$format" $monthly_args > > You have "kmonthly" in one place and "$k_monthly" in another. Sorry. It was just an inadvertent modification. It doesn't affect the output. To try to determine the issue, I decided to skirt the build of the format strings and entered the format strings as a series of 12 format strings ( %3d repeated 12 times ). That worked fine so I decided to just stick with that. Thanks.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: List full path and file name for selected directory Next: use variable to do some substitution with awk. |