Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

30 views
Skip to first unread message

Hongyi Zhao

unread,
Dec 23, 2016, 6:15:01 AM12/23/16
to
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 :.

Barry Margolin

unread,
Dec 23, 2016, 12:37:27 PM12/23/16
to
In article <o3j0vi$57s$1...@aspen.stu.neva.ru>,
Hongyi Zhao <hongy...@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, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Kenny McCormack

unread,
Dec 23, 2016, 12:45:30 PM12/23/16
to
In article <barmar-92EE2A....@88-209-239-213.giganet.hu>,
Barry Margolin <bar...@alum.mit.edu> wrote:
>In article <o3j0vi$57s$1...@aspen.stu.neva.ru>,
> Hongyi Zhao <hongy...@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 -

Dave Sines

unread,
Dec 23, 2016, 2:03:42 PM12/23/16
to
Hongyi Zhao <hongy...@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

Barry Margolin

unread,
Dec 23, 2016, 8:45:03 PM12/23/16
to
In article <o3jnro$css$1...@news.xmission.com>,
gaz...@shell.xmission.com (Kenny McCormack) wrote:

> In article <barmar-92EE2A....@88-209-239-213.giganet.hu>,
> Barry Margolin <bar...@alum.mit.edu> wrote:
> >In article <o3j0vi$57s$1...@aspen.stu.neva.ru>,
> > Hongyi Zhao <hongy...@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

Hongyi Zhao

unread,
Dec 23, 2016, 9:16:45 PM12/23/16
to
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.

Hongyi Zhao

unread,
Dec 23, 2016, 9:24:54 PM12/23/16
to
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.

Kees Nuyt

unread,
Dec 24, 2016, 4:47:21 AM12/24/16
to
On Sat, 24 Dec 2016 02:16:42 +0000 (UTC), Hongyi Zhao
<hongy...@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

Hongyi Zhao

unread,
Dec 24, 2016, 9:11:15 PM12/24/16
to
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.

Joerg.S...@fokus.fraunhofer.de

unread,
Dec 25, 2016, 6:25:44 AM12/25/16
to
In article <u090jdx...@perseus.wenlock-data.co.uk>,
Dave Sines <dave.gma...@googlemail.com.invalid> wrote:
>Hongyi Zhao <hongy...@gmail.com> wrote:
>> sleep 0.5
>> done
>> fi
>>
>> Is it possible to simplify the code with running cmd ( and the echo
>> command ) only once?

> e=:
> sleep 0.5
>done

Do you know that sleep 0.5 is a non-portable kshism?

Sleep usually only supports full second arguments.

--
EMail:jo...@schily.net (home) Jörg Schilling D-13353 Berlin
joerg.s...@fokus.fraunhofer.de (work) Blog: http://schily.blogspot.com/
URL: http://cdrecord.org/private/ http://sourceforge.net/projects/schilytools/files/

Kenny McCormack

unread,
Dec 25, 2016, 8:02:47 AM12/25/16
to
In article <o3oabm$h9d$3...@news.albasani.net>,
<Joerg.S...@fokus.fraunhofer.de> wrote:
...
>Do you know that sleep 0.5 is a non-portable kshism?

Not really.

>Sleep usually only supports full second arguments.

Not really. It is 2016, you know.
Most major Unixes/toolsets support it (GNU, OSX, etc), although the OSX man
page does say:

The sleep command will accept and honor a non-integer number of
specified seconds (with a `.' character as a decimal point). This is a
non-portable extension, and its use will nearly guarantee that a shell
script will not execute properly on another system.

(With the second sentence in bold).

Like you, I think they're being a bit alarmist.

And yes, that was intended as a bit of a joke, since sleep(1) was
traditionally implemented using alarm(2) and alarm(2) does indeed have only
seconds granularity.

P.S. alarm() is in section 2 on Linux, but on OSX, for some reason, it is in
section 3. IMHO, it *should* be in section 2.

--
If Jeb is Charlie Brown kicking a football-pulled-away, Mitt is a '50s
housewife with a black eye who insists to her friends the roast wasn't
dry.

Joerg.S...@fokus.fraunhofer.de

unread,
Dec 26, 2016, 7:52:48 AM12/26/16
to
In article <o3og1k$1jd$1...@news.xmission.com>,
Kenny McCormack <gaz...@shell.xmission.com> wrote:
>In article <o3oabm$h9d$3...@news.albasani.net>,
> <Joerg.S...@fokus.fraunhofer.de> wrote:
>...
>>Do you know that sleep 0.5 is a non-portable kshism?
>
>Not really.
>
>>Sleep usually only supports full second arguments.
>
>Not really. It is 2016, you know.

Just beacuse you discovered a few implementations that support it does not
verify the general availability.

AIX and HP-UX do not support it at all and on newer Solaris versions, it is in
/bin/sh == ksh93, but not in /bin/sleep and not in the system supplied POSIX
shell.

It may be of interest that the platforms where sleep in general accepts floats
as argument do not implement working support for waitid(). waitid() is the
sycall that appeared in 1989 and that has been included in the POSIX standard
in 1996 and that is expected to support returning all 32 bits from the exit()
code of called programs.

In other words, Linux and OSX do not seem to live in 2016...

島鉄雄

unread,
Jan 6, 2017, 9:06:12 PM1/6/17
to
島鉄雄です。
This code won't work the same as the code Hongyi Zhao posted. This
probably would:

func() {
echo $(cmd | wc -l)
}

if [ `func` -eq 0 ]; then
echo "bla bla."
until [ `func` -gt 0 ]; do
sleep 0.5
done
fi

However, I'm not sure if original code was correct to begin with. It
will print "bla bla." only if first run of cmd did not produce any
output, and I'm not sure if this is what Hongyi Zhao wants.

--
41号

島鉄雄

unread,
Jan 6, 2017, 9:34:45 PM1/6/17
to
島鉄雄です。
Correction: it will print "bla bla." and *run the rest of the code*
only if first run of cmd did not produce any output.

--
41号
0 new messages