f



Want to running cmd only once in my bash code snippets.

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
Hongyi
12/23/2016 11:14:58 AM
comp.unix.shell 15484 articles. 2 followers. Post Follow

8 Replies
109 Views

Similar Articles

[PageSpeed] 43

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
Barry
12/23/2016 5:37:24 PM
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
gazelle
12/23/2016 5:45:28 PM
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
dave
12/23/2016 7:03:26 PM
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
Barry
12/24/2016 1:45:00 AM
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
Hongyi
12/24/2016 2:16:42 AM
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
Hongyi
12/24/2016 2:24:52 AM
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
Kees
12/24/2016 9:47:16 AM
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
Hongyi
12/25/2016 2:11:10 AM
Reply: