Hi all, The following is my bash code snippets: if [ $( cmd | wc -l ) -eq 0 ]; then echo "bla bla." until [ $( cmd | wc -l ) -gt 0 ]; do sleep 0.5 done fi Is it possible to simplify the code with running cmd ( and the echo command ) only once? Regards -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
![]() |
0 |
![]() |
In article <o3j0vi$57s$1@aspen.stu.neva.ru>, Hongyi Zhao <hongyi.zhao@gmail.com> wrote: > Hi all, > > The following is my bash code snippets: > > if [ $( cmd | wc -l ) -eq 0 ]; then > echo "bla bla." > until [ $( cmd | wc -l ) -gt 0 ]; do > sleep 0.5 > done > fi > > Is it possible to simplify the code with running cmd ( and the echo > command ) only once? If you only run it once, then how will the until loop work? Isn't the point to keep running it until the result is empty? -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me ***
![]() |
0 |
![]() |
In article <barmar-92EE2A.12372423122016@88-209-239-213.giganet.hu>, Barry Margolin <barmar@alum.mit.edu> wrote: >In article <o3j0vi$57s$1@aspen.stu.neva.ru>, > Hongyi Zhao <hongyi.zhao@gmail.com> wrote: > >> Hi all, >> >> The following is my bash code snippets: >> >> if [ $( cmd | wc -l ) -eq 0 ]; then >> echo "bla bla." >> until [ $( cmd | wc -l ) -gt 0 ]; do >> sleep 0.5 >> done >> fi >> >> Is it possible to simplify the code with running cmd ( and the echo >> command ) only once? > >If you only run it once, then how will the until loop work? Isn't the >point to keep running it until the result is empty? I think the OP is using the word "running" to mean "writing". I.e., the goal is to have it appear only once in the source code. There are many ways to do this - but I'm not inclined to re-write other people's shell code. -- Faced with the choice between changing one's mind and proving that there is no need to do so, almost everyone gets busy on the proof. - John Kenneth Galbraith -
![]() |
0 |
![]() |
Hongyi Zhao <hongyi.zhao@gmail.com> wrote: > Hi all, > > The following is my bash code snippets: > > if [ $( cmd | wc -l ) -eq 0 ]; then > echo "bla bla." > until [ $( cmd | wc -l ) -gt 0 ]; do > sleep 0.5 > done > fi > > Is it possible to simplify the code with running cmd ( and the echo > command ) only once? e=echo while [ $( cmd | wc -l ) -eq 0 ]; do $e "bla bla." e=: sleep 0.5 done
![]() |
0 |
![]() |
In article <o3jnro$css$1@news.xmission.com>, gazelle@shell.xmission.com (Kenny McCormack) wrote: > In article <barmar-92EE2A.12372423122016@88-209-239-213.giganet.hu>, > Barry Margolin <barmar@alum.mit.edu> wrote: > >In article <o3j0vi$57s$1@aspen.stu.neva.ru>, > > Hongyi Zhao <hongyi.zhao@gmail.com> wrote: > > > >> Hi all, > >> > >> The following is my bash code snippets: > >> > >> if [ $( cmd | wc -l ) -eq 0 ]; then > >> echo "bla bla." > >> until [ $( cmd | wc -l ) -gt 0 ]; do > >> sleep 0.5 > >> done > >> fi > >> > >> Is it possible to simplify the code with running cmd ( and the echo > >> command ) only once? > > > >If you only run it once, then how will the until loop work? Isn't the > >point to keep running it until the result is empty? > > I think the OP is using the word "running" to mean "writing". > > I.e., the goal is to have it appear only once in the source code. > > There are many ways to do this - but I'm not inclined to re-write other > people's shell code. Put it in a function: func() { test $(cmd | wc -l) -eq 0 } Then you can write: if func; then echo blah blah until func; do sleep 0.5 done i -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me ***
![]() |
0 |
![]() |
On Fri, 23 Dec 2016 12:37:24 -0500, Barry Margolin wrote: >> Hi all, >> >> The following is my bash code snippets: >> >> if [ $( cmd | wc -l ) -eq 0 ]; then >> echo "bla bla." >> until [ $( cmd | wc -l ) -gt 0 ]; do >> sleep 0.5 >> done >> fi >> >> Is it possible to simplify the code with running cmd ( and the echo >> command ) only once? > > If you only run it once, then how will the until loop work? Isn't the > point to keep running it until the result is empty? The cmd here is another script which will running for ever and give some outputs changing along the time. Regards -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
![]() |
0 |
![]() |
On Fri, 23 Dec 2016 19:03:26 +0000, Dave Sines wrote: > e=echo > while [ $( cmd | wc -l ) -eq 0 ]; do > $e "bla bla." > e=: > sleep 0.5 > done Thanks, I previously also tried with another variable in the following way: i=0 while i=$(( i + 1 )); [ $( cmd | wc -l ) -eq 0 ]; do [ $i -eq 1 ] && echo "bla bla." sleep 0.5 done But, I still want to find some more concise forms. Regards -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
![]() |
0 |
![]() |
On Sat, 24 Dec 2016 02:16:42 +0000 (UTC), Hongyi Zhao <hongyi.zhao@gmail.com> wrote: >On Fri, 23 Dec 2016 12:37:24 -0500, Barry Margolin wrote: > >>> Hi all, >>> >>> The following is my bash code snippets: >>> >>> if [ $( cmd | wc -l ) -eq 0 ]; then >>> echo "bla bla." >>> until [ $( cmd | wc -l ) -gt 0 ]; do >>> sleep 0.5 >>> done >>> fi >>> >>> Is it possible to simplify the code with running cmd ( and the echo >>> command ) only once? >> >> If you only run it once, then how will the until loop work? Isn't the >> point to keep running it until the result is empty? > > The cmd here is another script which will running for ever and give some > outputs changing along the time. Change cmd so it writes to a file. Make sure it flushes its output. In the wait script, periodically check the file size. -- HTH Kees Nuyt
![]() |
0 |
![]() |
On Sat, 24 Dec 2016 10:47:16 +0100, Kees Nuyt wrote: >> The cmd here is another script which will running for ever and give >> some outputs changing along the time. > > Change cmd so it writes to a file. Make sure it flushes its output. In > the wait script, periodically check the file size. It seems still clumsy, anyway, thanks a lot. Regards -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
![]() |
0 |
![]() |