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

"sh: 1: 0: not found " issue while using awk.

122 views
Skip to first unread message

Hongyi Zhao

unread,
Jul 23, 2019, 11:27:26 AM7/23/19
to
Hi,

I try to run awk with the following code:

----------------------
#!/usr/bin/env bash

awk '
BEGIN {

while ( ( system("/bin/echo show stat | /usr/bin/sudo socat stdio /var/
run/haproxy.sock") | getline var ) >0 ) {
print var
}

} '
----------------------

It can run, but at the end of the output, I meet the following error:



sh: 1: 0: not found


How to solve this issue?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Keith Thompson

unread,
Jul 23, 2019, 3:36:59 PM7/23/19
to
Hongyi Zhao <hongy...@gmail.com> writes:
> I try to run awk with the following code:
>
> ----------------------
> #!/usr/bin/env bash
>
> awk '
> BEGIN {
>
> while ( ( system("/bin/echo show stat | /usr/bin/sudo socat stdio /var/run/haproxy.sock") | getline var ) >0 ) {
> print var
> }
>
> } '
> ----------------------
>
> It can run, but at the end of the output, I meet the following error:
>
> sh: 1: 0: not found

The way to pipe a command's output to getline in awk is:

"command args..." | getline var

Drop the "system(" and ")".

I'm guessing this is part of a larger script. If it isn't, it would
make more sense for this to be an awk script rather than a bash script
that invokes awk:

#!/usr/bin/awk -f

BEGIN {
# ...
}

Or "#!/usr/bin/env awk -f" if you prefer -- assuming your system accepts
more than one argument on a #! line. (If it doesn't, the
"#!/usr/bin/env bash ..." trick is a reasonable workaround.)

Incidentally, there are arguments for and against the "#!/usr/bin/env
bash" trick. For one thing, there are systems (mostly older ones) on
which the env command isn't in /usr/bin/env.

The advantage of the "#!/usr/bin/env bash" trick is that it uses the
first bash in the user's $PATH.

The disadvantage of the "#!/usr/bin/env bash" trick is that it uses the
first bash in the user's $PATH.

More discussion here: https://unix.stackexchange.com/a/29620/10454

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Hongyi Zhao

unread,
Jul 23, 2019, 8:44:35 PM7/23/19
to
On Tue, 23 Jul 2019 12:36:55 -0700, Keith Thompson wrote:

> Or "#!/usr/bin/env awk -f" if you prefer -- assuming your system accepts
> more than one argument on a #! line. (If it doesn't, the
> "#!/usr/bin/env bash ..." trick is a reasonable workaround.)

These two methods don't work on my box, the following is the errors given
by them:

/usr/bin/env: ‘awk -f’: No such file or directory

and

/usr/bin/env: ‘bash awk -f’: No such file or directory

Keith Thompson

unread,
Jul 23, 2019, 9:00:43 PM7/23/19
to
Hongyi Zhao <hongy...@gmail.com> writes:
> On Tue, 23 Jul 2019 12:36:55 -0700, Keith Thompson wrote:
>> Or "#!/usr/bin/env awk -f" if you prefer -- assuming your system accepts
>> more than one argument on a #! line. (If it doesn't, the
>> "#!/usr/bin/env bash ..." trick is a reasonable workaround.)
>
> These two methods don't work on my box, the following is the errors given
> by them:
>
> /usr/bin/env: ‘awk -f’: No such file or directory

OK, so your system (what OS are you using?) doesn't support more than
one argument to the command on a #! line.

> and
>
> /usr/bin/env: ‘bash awk -f’: No such file or directory

I didn't mean to suggest that. I was referring to the method you were
already using:

#!/usr/bin/env bash

awk ...

But you should be able to use:

#!/usr/bin/awk -f

assuming your system has awk installed in /usr/bin.

Hongyi Zhao

unread,
Jul 24, 2019, 3:44:36 AM7/24/19
to
On Tue, 23 Jul 2019 18:00:37 -0700, Keith Thompson wrote:

>> /usr/bin/env: ‘awk -f’: No such file or directory
>
> OK, so your system (what OS are you using?) doesn't support more than
> one argument to the command on a #! line.

$ uname -a
Linux localhost 4.9.0-9-amd64 #1 SMP Debian 4.9.168-1+deb9u3 (2019-06-16)
x86_64 GNU/Linux

Thanks for your instruction.

Thomas 'PointedEars' Lahn

unread,
Jul 24, 2019, 6:27:18 AM7/24/19
to
Hongyi Zhao wrote:

> On Tue, 23 Jul 2019 18:00:37 -0700, Keith Thompson wrote:
>>> /usr/bin/env: ‘awk -f’: No such file or directory
>>
>> OK, so your system (what OS are you using?) doesn't support more than
>> one argument to the command on a #! line.
>
> $ uname -a
> Linux localhost 4.9.0-9-amd64 #1 SMP Debian 4.9.168-1+deb9u3 (2019-06-16)
> x86_64 GNU/Linux
>
> Thanks for your instruction.

I can confirm that the Shebang

#!/usr/bin/env awk -f

produces the error above on a Debian-based GNU+Linux distribution (if awk is
alternatives-linked to gawk):

$ uname -srvmo
Linux 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5 (2019-06-19) x86_64 GNU/Linux

(I am using Devuan GNU+Linux 2.0 “ASCII”.)

The Shebang

#!/usr/bin/awk -f

works then.

--
PointedEars
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2
Please do not cc me. /Bitte keine Kopien per E-Mail.

Keith Thompson

unread,
Jul 24, 2019, 4:22:39 PM7/24/19
to
Thomas 'PointedEars' Lahn <Point...@web.de> writes:
> Hongyi Zhao wrote:
>> On Tue, 23 Jul 2019 18:00:37 -0700, Keith Thompson wrote:
>>>> /usr/bin/env: ‘awk -f’: No such file or directory
>>>
>>> OK, so your system (what OS are you using?) doesn't support more than
>>> one argument to the command on a #! line.
>>
>> $ uname -a
>> Linux localhost 4.9.0-9-amd64 #1 SMP Debian 4.9.168-1+deb9u3 (2019-06-16)
>> x86_64 GNU/Linux
>>
>> Thanks for your instruction.
>
> I can confirm that the Shebang
>
> #!/usr/bin/env awk -f
>
> produces the error above on a Debian-based GNU+Linux distribution (if awk is
> alternatives-linked to gawk):
>
> $ uname -srvmo
> Linux 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5 (2019-06-19) x86_64 GNU/Linux
>
> (I am using Devuan GNU+Linux 2.0 “ASCII”.)
>
> The Shebang
>
> #!/usr/bin/awk -f
>
> works then.

Same on Ubuntu 18.04.2.

I was sure that some recent version of *some* OS had introduced the
ability to have multiple arguments on a #! line. I can currently find
no evidence in support of that. (I don't often use the "#!/usr/bin/env"
trick myself, so it likely wouldn't affect me one way or the other.)

A non-portable workaround: GNU Coreutils "env" starting with release
8.30 has a "-S" option For example, this:

#!/usr/bin/env -S awk -f

tells env to split the single argument "awk -f" into three separate
arguments, as if you typed "awk -f <name-of-script>" at a shell prompt.
(This should work even on systems where #! supports multiple arguments.)

Coreutils 8.30 is fairly new, and is likely not to be installed on
a lot of systems (Ubuntu 18.04.2 LTS has coreutils 8.28).

(My own preference is to use "#!/usr/bin/awk -f", and modify the "#!"
line as needed when installing scripts on a system where awk is
elsewhere.)

Thomas 'PointedEars' Lahn

unread,
Jul 24, 2019, 5:07:12 PM7/24/19
to
Keith Thompson wrote:

> Thomas 'PointedEars' Lahn <Point...@web.de> writes:
>> I can confirm that the Shebang
>>
>> #!/usr/bin/env awk -f
>>
>> produces the error above on a Debian-based GNU+Linux distribution (if awk
>> is alternatives-linked to gawk):
>>
>> $ uname -srvmo
>> Linux 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5 (2019-06-19) x86_64
>> GNU/Linux
>>
>> (I am using Devuan GNU+Linux 2.0 “ASCII”.)
>>
>> The Shebang
>>
>> #!/usr/bin/awk -f
>>
>> works then.
>
> Same on Ubuntu 18.04.2.

That is what I expected from the results in Debian and Devuan.
Ubuntu Linux *is* a Debian-based Linux distribution, too.

Kaz Kylheku

unread,
Jul 25, 2019, 12:39:57 AM7/25/19
to
On 2019-07-24, Keith Thompson <ks...@mib.org> wrote:
> A non-portable workaround: GNU Coreutils "env" starting with release
> 8.30 has a "-S" option For example, this:
>
> #!/usr/bin/env -S awk -f

I implemented it first and sent a patch, complete with tests and doc
update. Then some disussion came up with making it BSD compatible,
and I pulled out.

https://lists.gnu.org/archive/html/coreutils/2017-05/msg00021.html

Martijn Dekker

unread,
Jul 26, 2019, 9:51:00 AM7/26/19
to
Op 23-07-19 om 21:36 schreef Keith Thompson:
> Incidentally, there are arguments for and against the "#!/usr/bin/env
> bash" trick. For one thing, there are systems (mostly older ones) on
> which the env command isn't in /usr/bin/env.

"Mostly" older ones?

Does anyone know of any Unix-type system released in the last decade
where env is located somewhere else? I've been looking for years and
haven't found any, but I don't have access to expensive commercial Unixes.

Modernish depends on /usr/bin/env in the hashbang path for portable-form
scripts, so if there is any remotely current system where that breaks,
it would really be good to know.

Maybe z/OS? But IBM actively instructs its users to fix that:
https://www.ibm.com/support/knowledgecenter/en/SSCTFE_1.1.0/com.ibm.azk.v1r1.azka100/topics/azkic_t_verifyenvpath.htm

- M.

--
/ modernish -- harness the shell \
https://github.com/modernish/modernish

Kenny McCormack

unread,
Jul 26, 2019, 10:04:51 AM7/26/19
to
In article <gq0epv...@mid.individual.net>,
Martijn Dekker <mar...@inlv.demon.nl> wrote:
>Op 23-07-19 om 21:36 schreef Keith Thompson:
>> Incidentally, there are arguments for and against the "#!/usr/bin/env
>> bash" trick. For one thing, there are systems (mostly older ones) on
>> which the env command isn't in /usr/bin/env.
>
>"Mostly" older ones?
>
>Does anyone know of any Unix-type system released in the last decade
>where env is located somewhere else? I've been looking for years and
>haven't found any, but I don't have access to expensive commercial Unixes.

A lot of the lore in Usenet newsgroups like this one has to do with
portability to systems that are:
1) Multiple decades (and by "multiple", I mean like 4 or 5)
2) Nobody under 40 has ever used.

You see it all the time. Somebody posts something that uses some feature
that has been common for decades now, and someone else will say:

"Ahem. That won't work on Solaris versions prior to 2.5"

or some such.
--
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 -

Keith Thompson

unread,
Jul 26, 2019, 1:57:42 PM7/26/19
to
Termux under Android has "env" in "/data/data/com.termux/files/usr/bin/env".

However the current version of termux supports "#!/usr/bin/env ..."
even though no such file exists (I think that's a fairly recent change).

Benjamin Esham

unread,
Jul 26, 2019, 2:28:51 PM7/26/19
to
Martijn Dekker wrote:

> Op 23-07-19 om 21:36 schreef Keith Thompson:
>
>> Incidentally, there are arguments for and against the "#!/usr/bin/env
>> bash" trick. For one thing, there are systems (mostly older ones) on
>> which the env command isn't in /usr/bin/env.
>
> "Mostly" older ones?
>
> Does anyone know of any Unix-type system released in the last decade
> where env is located somewhere else? I've been looking for years and
> haven't found any, but I don't have access to expensive commercial Unixes.
>
> Modernish depends on /usr/bin/env in the hashbang path for portable-form
> scripts, so if there is any remotely current system where that breaks,
> it would really be good to know.

For what it's worth, /usr/bin/env does exist on NixOS, even though that
distro jettisons almost all of the rest of the FHS.

--
Benjamin Esham
https://esham.io

Casper H.S. Dik

unread,
Jul 29, 2019, 9:51:22 AM7/29/19
to
gaz...@shell.xmission.com (Kenny McCormack) writes:

>In article <gq0epv...@mid.individual.net>,
>Martijn Dekker <mar...@inlv.demon.nl> wrote:
>>Op 23-07-19 om 21:36 schreef Keith Thompson:
>>> Incidentally, there are arguments for and against the "#!/usr/bin/env
>>> bash" trick. For one thing, there are systems (mostly older ones) on
>>> which the env command isn't in /usr/bin/env.
>>
>>"Mostly" older ones?
>>
>>Does anyone know of any Unix-type system released in the last decade
>>where env is located somewhere else? I've been looking for years and
>>haven't found any, but I don't have access to expensive commercial Unixes.

>A lot of the lore in Usenet newsgroups like this one has to do with
>portability to systems that are:
> 1) Multiple decades (and by "multiple", I mean like 4 or 5)
> 2) Nobody under 40 has ever used.

>You see it all the time. Somebody posts something that uses some feature
>that has been common for decades now, and someone else will say:

> "Ahem. That won't work on Solaris versions prior to 2.5"

>or some such.

Pretty sure Solaris 2.0 had /usr/bin/env :-)

Don't remember when I used "#!/usr/bin/env"; pretty sure it was
to invoke perl.

Casper
0 new messages