Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The bigger the theory the better.


devel / comp.unix.shell / Try to understand complex sed command logic instructions.

SubjectAuthor
* Try to understand complex sed command logic instructions.hongy...@gmail.com
`* Re: Try to understand complex sed command logic instructions.Janis Papanagnou
 +* Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
 |`* Re: Try to understand complex sed command logic instructions.Janis Papanagnou
 | `* Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
 |  `* Re: Try to understand complex sed command logic instructions.Janis Papanagnou
 |   `- Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
 +* Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
 |`* Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
 | `* Re: Try to understand complex sed command logic instructions.John-Paul Stewart
 |  `* Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
 |   `- Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
 `* Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
  `* Re: Try to understand complex sed command logic instructions.Janis Papanagnou
   `* Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
    +* Re: Try to understand complex sed command logic instructions.Janis Papanagnou
    |`* Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
    | +* Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
    | |`* The exeve system call and the exec family of functions (was: Try to understand cHelmut Waitzmann
    | | `* Re: The exeve system call and the exec family of functions (was: Tryhongy...@gmail.com
    | |  `* Re: The exeve system call and the exec family of functions (was: Tryhongy...@gmail.com
    | |   `* Re: The exeve system call and the exec family of functions (was: TryDavid W. Hodgins
    | |    `* Re: The exeve system call and the exec family of functions (was: Tryhongy...@gmail.com
    | |     `* Re: The exeve system call and the exec family of functions (was: TryDavid W. Hodgins
    | |      `- Re: The exeve system call and the exec family of functions (was: TryKenny McCormack
    | +- Re: Try to understand complex sed command logic instructions.William Unruh
    | `* Re: Try to understand complex sed command logic instructions.Janis Papanagnou
    |  `- Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
    `* Re: Try to understand complex sed command logic instructions.Keith Thompson
     `* Re: Try to understand complex sed command logic instructions.hongy...@gmail.com
      `* Re: Try to understand complex sed command logic instructions.David W. Hodgins
       `- Re: Try to understand complex sed command logic instructions.hongy...@gmail.com

Pages:12
Try to understand complex sed command logic instructions.

<e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4713&group=comp.unix.shell#4713

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:620a:75a:: with SMTP id i26mr24624774qki.179.1639222222412;
Sat, 11 Dec 2021 03:30:22 -0800 (PST)
X-Received: by 2002:a05:620a:c50:: with SMTP id u16mr25495229qki.568.1639222222264;
Sat, 11 Dec 2021 03:30:22 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sat, 11 Dec 2021 03:30:22 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=143.198.81.125; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 143.198.81.125
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
Subject: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sat, 11 Dec 2021 11:30:22 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 36
 by: hongy...@gmail.com - Sat, 11 Dec 2021 11:30 UTC

I try to understand the following complex sed command logic instructions given here [1]:

If your ~/.profile sources ~/.bashrc (Debian, Ubuntu, Mint):

# the sed invocation inserts the lines at the start of the file
# after any initial comment lines
sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{n;ba};}' ~/.profile

I tried the above code, and found that the following lines will be inserted into `~/.profile' after the shebang line:

$ git diff
diff --git a/.profile b/.profile
index c99e6c3e..5b33339f 100644
--- a/.profile
+++ b/.profile
@@ -1,5 +1,8 @@
#!/usr/bin/env bash
+export PYENV_ROOT="$HOME/.pyenv"
+export PATH="$PYENV_ROOT/bin:$PATH"
+ # To do:
#bootstrap based on this file.
[1] https://github.com/pyenv/pyenv#basic-github-checkout

But the logic contained in the sed code snippet above is still obscure to me. Any hints?

OTOH, the final result achieved by the sed workflow does seem so trivial. Why do they still insist on providing such an ancient sed-based solution?

Regards,
HZ

Re: Try to understand complex sed command logic instructions.

<sp2577$6no$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4714&group=comp.unix.shell#4714

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Try to understand complex sed command logic instructions.
Date: Sat, 11 Dec 2021 13:23:03 +0100
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <sp2577$6no$1@dont-email.me>
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 11 Dec 2021 12:23:04 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="997349ed2cb674ecc76307499563d040";
logging-data="6904"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185P51maxy/DDRJUZPu+34o"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:Fh95ObgoZ8jrfA3mZN6tklRWv8A=
In-Reply-To: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sat, 11 Dec 2021 12:23 UTC

On 11.12.2021 12:30, hongy...@gmail.com wrote:
> I try to understand the following complex sed command logic
> instructions given here [1]:
> [...]
>
> But the logic contained in the sed code snippet above is still
> obscure to me. Any hints?

Get the sed manual and identify the used commands. Or formulate
the simple algorithm yourself informally and try to identify the
building blocks in the sed code.

>
> OTOH, the final result achieved by the sed workflow does seem so
> trivial. Why do they still insist on providing such an ancient
> sed-based solution?

Maybe it's code from the 1980's?
Maybe the programmer is used to that tool?
Maybe the programmer doesn't know any better tool?
Who knows?
But I see no one insisting in anything.

Though, for simple tasks you can always write your own version,
and use a language that is or appears to you as less cryptic.

I'd use Awk, for example, probably something like

awk '!/^#/ && !f++ { print "..." } ; 1'

(or replace the 1 by {print} to make it less cryptic). For many
folks that's easier to read because syntax is similar to the C
language and its language family.

But the code is just a quick shot; you may want your program to
also handle empty files, or files that have just comments and
no data lines (that triggers the condition), and maybe use GNU
Awk with the in-place editing feature that is also used in the
sed call.

Janis

Re: Try to understand complex sed command logic instructions.

<f65b7516-a4dd-4532-bb19-e214f10db32fn@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4715&group=comp.unix.shell#4715

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ae9:edd3:: with SMTP id c202mr26798909qkg.274.1639269767229;
Sat, 11 Dec 2021 16:42:47 -0800 (PST)
X-Received: by 2002:a05:622a:1012:: with SMTP id d18mr36722616qte.240.1639269767069;
Sat, 11 Dec 2021 16:42:47 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sat, 11 Dec 2021 16:42:46 -0800 (PST)
In-Reply-To: <sp2577$6no$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=103.149.249.6; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 103.149.249.6
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com> <sp2577$6no$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f65b7516-a4dd-4532-bb19-e214f10db32fn@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 12 Dec 2021 00:42:47 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 53
 by: hongy...@gmail.com - Sun, 12 Dec 2021 00:42 UTC

On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
> On 11.12.2021 12:30, hongy...@gmail.com wrote:
> > I try to understand the following complex sed command logic
> > instructions given here [1]:
> > [...]
> >
> > But the logic contained in the sed code snippet above is still
> > obscure to me. Any hints?
> Get the sed manual and identify the used commands. Or formulate
> the simple algorithm yourself informally and try to identify the
> building blocks in the sed code.
> >
> > OTOH, the final result achieved by the sed workflow does seem so
> > trivial. Why do they still insist on providing such an ancient
> > sed-based solution?
> Maybe it's code from the 1980's?
> Maybe the programmer is used to that tool?
> Maybe the programmer doesn't know any better tool?
> Who knows?
> But I see no one insisting in anything.
>
> Though, for simple tasks you can always write your own version,
> and use a language that is or appears to you as less cryptic.
>
> I'd use Awk, for example, probably something like
>
> awk '!/^#/ && !f++ { print "..." } ; 1'

I try to debug/understand your above code as follows:

$ awk '!/^#/ && !f++ { print "..." } ; {print $0, f, f++}' .profile | head -3
#!/usr/bin/env bash 0
2 2
# To do: 3 3

$ awk '!/^#/ && !f++ { print "..." } ; {print $0, f}' .profile | head -3
#!/usr/bin/env bash
....
1

As you can see, when I try to print the value of `f++`, the desired modification results will disappear. This should mean that the variable was altered as it was printed, which makes debugging in this form impossible.


> (or replace the 1 by {print} to make it less cryptic). For many
> folks that's easier to read because syntax is similar to the C
> language and its language family.
>
> But the code is just a quick shot; you may want your program to
> also handle empty files, or files that have just comments and
> no data lines (that triggers the condition), and maybe use GNU
> Awk with the in-place editing feature that is also used in the
> sed call.
>
> Janis

Re: Try to understand complex sed command logic instructions.

<9baba1db-8aff-418b-849f-edadb615939dn@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4716&group=comp.unix.shell#4716

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:180c:: with SMTP id t12mr38080086qtc.507.1639277025287;
Sat, 11 Dec 2021 18:43:45 -0800 (PST)
X-Received: by 2002:ac8:5e0f:: with SMTP id h15mr36281638qtx.490.1639277025137;
Sat, 11 Dec 2021 18:43:45 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sat, 11 Dec 2021 18:43:44 -0800 (PST)
In-Reply-To: <sp2577$6no$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=60.249.28.130; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 60.249.28.130
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com> <sp2577$6no$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <9baba1db-8aff-418b-849f-edadb615939dn@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 12 Dec 2021 02:43:45 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 57
 by: hongy...@gmail.com - Sun, 12 Dec 2021 02:43 UTC

On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
> On 11.12.2021 12:30, hongy...@gmail.com wrote:
> > I try to understand the following complex sed command logic
> > instructions given here [1]:
> > [...]
> >
> > But the logic contained in the sed code snippet above is still
> > obscure to me. Any hints?
> Get the sed manual and identify the used commands. Or formulate
> the simple algorithm yourself informally and try to identify the
> building blocks in the sed code.

I try to decrypt it as follows, please correct me if I'm wrong:

sed -Ei

use extended regular expressions in the script
and
edit files in place

-e

add the script to the commands to be executed

'
Begin the script

/^([^#]|$)/

Anchor lines that is empty or does not start with #

{

Begin a block of commands

a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"

Append text, which has each embedded newline preceded by a backslash.

a \
' -e ':a' -e '$!{n;ba};

Failed to figure out the above obscure code block.

}

End a block of commands

'
End the script

~/.profile

The input file to be manipulated.

Re: Try to understand complex sed command logic instructions.

<f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4717&group=comp.unix.shell#4717

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:620a:199a:: with SMTP id bm26mr28691759qkb.542.1639290736697;
Sat, 11 Dec 2021 22:32:16 -0800 (PST)
X-Received: by 2002:a05:622a:40d:: with SMTP id n13mr37705248qtx.511.1639290736516;
Sat, 11 Dec 2021 22:32:16 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!ecngs!feeder2.ecngs.de!178.20.174.213.MISMATCH!feeder1.feed.usenet.farm!feed.usenet.farm!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sat, 11 Dec 2021 22:32:16 -0800 (PST)
In-Reply-To: <sp2577$6no$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=172.104.65.59; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 172.104.65.59
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com> <sp2577$6no$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 12 Dec 2021 06:32:16 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1678
 by: hongy...@gmail.com - Sun, 12 Dec 2021 06:32 UTC

On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
> I'd use Awk, for example, probably something like
>
> awk '!/^#/ && !f++ { print "..." } ; 1'

The above code will add the stuff at the very beginning of the script if occasionally the shebang line is not the first line:

$ cat test.sh

#!/usr/bin/env bash
echo 'hello world.'

$ ./test.sh
hello world.

$ awk '!/^#/ && !f++ { print "..."} ; {print $0}' test.sh
....

#!/usr/bin/env bash
echo 'hello world.'

HZ

Re: Try to understand complex sed command logic instructions.

<6eb36997-6921-45a9-9d74-8668caaf79dan@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4718&group=comp.unix.shell#4718

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:620a:8d4:: with SMTP id z20mr27106556qkz.526.1639290972878;
Sat, 11 Dec 2021 22:36:12 -0800 (PST)
X-Received: by 2002:a05:620a:288c:: with SMTP id j12mr28410976qkp.66.1639290972745;
Sat, 11 Dec 2021 22:36:12 -0800 (PST)
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sat, 11 Dec 2021 22:36:12 -0800 (PST)
In-Reply-To: <9baba1db-8aff-418b-849f-edadb615939dn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=143.198.81.109; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 143.198.81.109
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me> <9baba1db-8aff-418b-849f-edadb615939dn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6eb36997-6921-45a9-9d74-8668caaf79dan@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 12 Dec 2021 06:36:12 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2635
 by: hongy...@gmail.com - Sun, 12 Dec 2021 06:36 UTC

On Sunday, December 12, 2021 at 10:43:47 AM UTC+8, hongy...@gmail.com wrote:
> On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
> > On 11.12.2021 12:30, hongy...@gmail.com wrote:
> > > I try to understand the following complex sed command logic
> > > instructions given here [1]:
> > > [...]
> > >
> > > But the logic contained in the sed code snippet above is still
> > > obscure to me. Any hints?
> > Get the sed manual and identify the used commands. Or formulate
> > the simple algorithm yourself informally and try to identify the
> > building blocks in the sed code.
> I try to decrypt it as follows, please correct me if I'm wrong:
>
> sed -Ei
>
> use extended regular expressions in the script
> and
> edit files in place
>
> -e
>
> add the script to the commands to be executed
>
> '
> Begin the script
>
>
> /^([^#]|$)/
>
> Anchor lines that is empty or does not start with #
>
> {
>
> Begin a block of commands
> a \
> export PYENV_ROOT="$HOME/.pyenv"
> a \
> export PATH="$PYENV_ROOT/bin:$PATH"
> Append text, which has each embedded newline preceded by a backslash.
> a \
> ' -e ':a' -e '$!{n;ba};
> Failed to figure out the above obscure code block.

Especially, there are two '-e' constructs embedded in outer '-e' construct, which is very confusing for me.

HZ

Re: Try to understand complex sed command logic instructions.

<sp4not$5bs$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4719&group=comp.unix.shell#4719

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Try to understand complex sed command logic instructions.
Date: Sun, 12 Dec 2021 12:51:56 +0100
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <sp4not$5bs$1@dont-email.me>
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me>
<f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 12 Dec 2021 11:51:57 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="edd6c9f1e2953654c43794cc44b20122";
logging-data="5500"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+p6J06UCvBTviL49bUqvBx"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:byikztx7AzL6LcMk1p0dwRwPkhg=
In-Reply-To: <f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sun, 12 Dec 2021 11:51 UTC

On 12.12.2021 07:32, hongy...@gmail.com wrote:
> On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
>> I'd use Awk, for example, probably something like
>>
>> awk '!/^#/ && !f++ { print "..." } ; 1'
>
> The above code will add the stuff at the very beginning of the
> script if occasionally the shebang line is not the first line:

1. The posted requirements had been
# the sed invocation inserts the lines at the start of the file
# after any initial comment lines

2. What do you expect from a shebang line that is _ineffective_;
because it is *not* on the first line?

If you happen to have other requirements adjust the condition.
(Don't expect that we can read your mind from that distance.)

You want to consider [initial] blank lines similar to comments,
for example?

!(/^#/||!NF) && !f++ ...

Janis

>
> $ cat test.sh
>
> #!/usr/bin/env bash
> echo 'hello world.'
>
> $ ./test.sh
> hello world.
>
> $ awk '!/^#/ && !f++ { print "..."} ; {print $0}' test.sh
> ...
>
> #!/usr/bin/env bash
> echo 'hello world.'
>
> HZ
>

Re: Try to understand complex sed command logic instructions.

<sp4o31$b1o$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4720&group=comp.unix.shell#4720

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Try to understand complex sed command logic instructions.
Date: Sun, 12 Dec 2021 12:57:21 +0100
Organization: A noiseless patient Spider
Lines: 65
Message-ID: <sp4o31$b1o$1@dont-email.me>
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me>
<f65b7516-a4dd-4532-bb19-e214f10db32fn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 12 Dec 2021 11:57:21 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="edd6c9f1e2953654c43794cc44b20122";
logging-data="11320"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OlDhV1JZQKJlyxxR2dZFG"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:Qa52AdBrUOOMLrJkxVI76gRjF60=
In-Reply-To: <f65b7516-a4dd-4532-bb19-e214f10db32fn@googlegroups.com>
 by: Janis Papanagnou - Sun, 12 Dec 2021 11:57 UTC

On 12.12.2021 01:42, hongy...@gmail.com wrote:
> On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
>> On 11.12.2021 12:30, hongy...@gmail.com wrote:
>>> I try to understand the following complex sed command logic
>>> instructions given here [1]:
>>> [...]
>>>
>>> But the logic contained in the sed code snippet above is still
>>> obscure to me. Any hints?
>> Get the sed manual and identify the used commands. Or formulate
>> the simple algorithm yourself informally and try to identify the
>> building blocks in the sed code.
>>>
>>> OTOH, the final result achieved by the sed workflow does seem so
>>> trivial. Why do they still insist on providing such an ancient
>>> sed-based solution?
>> Maybe it's code from the 1980's?
>> Maybe the programmer is used to that tool?
>> Maybe the programmer doesn't know any better tool?
>> Who knows?
>> But I see no one insisting in anything.
>>
>> Though, for simple tasks you can always write your own version,
>> and use a language that is or appears to you as less cryptic.
>>
>> I'd use Awk, for example, probably something like
>>
>> awk '!/^#/ && !f++ { print "..." } ; 1'
>
> I try to debug/understand your above code as follows:

Understand how f++ works - in *any* of the many languages that support
it - before you try to "debug".

GNU Awk has a debugger that makes it unnecessary in practice to add own
[possibly or likely wrong] code.

Janis

>
> $ awk '!/^#/ && !f++ { print "..." } ; {print $0, f, f++}' .profile | head -3
> #!/usr/bin/env bash 0
> 2 2
> # To do: 3 3
>
> $ awk '!/^#/ && !f++ { print "..." } ; {print $0, f}' .profile | head -3
> #!/usr/bin/env bash
> ...
> 1
>
> As you can see, when I try to print the value of `f++`, the desired modification results will disappear. This should mean that the variable was altered as it was printed, which makes debugging in this form impossible.
>
>
>> (or replace the 1 by {print} to make it less cryptic). For many
>> folks that's easier to read because syntax is similar to the C
>> language and its language family.
>>
>> But the code is just a quick shot; you may want your program to
>> also handle empty files, or files that have just comments and
>> no data lines (that triggers the condition), and maybe use GNU
>> Awk with the in-place editing feature that is also used in the
>> sed call.
>>
>> Janis

Re: Try to understand complex sed command logic instructions.

<c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4721&group=comp.unix.shell#4721

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ac8:7d84:: with SMTP id c4mr38098804qtd.94.1639311322135;
Sun, 12 Dec 2021 04:15:22 -0800 (PST)
X-Received: by 2002:ad4:596d:: with SMTP id eq13mr36811500qvb.91.1639311321991;
Sun, 12 Dec 2021 04:15:21 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 12 Dec 2021 04:15:21 -0800 (PST)
In-Reply-To: <sp4not$5bs$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=172.104.91.163; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 172.104.91.163
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me> <f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
<sp4not$5bs$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 12 Dec 2021 12:15:22 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 25
 by: hongy...@gmail.com - Sun, 12 Dec 2021 12:15 UTC

On Sunday, December 12, 2021 at 7:52:02 PM UTC+8, Janis Papanagnou wrote:
> On 12.12.2021 07:32, hongy...@gmail.com wrote:
> > On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
> >> I'd use Awk, for example, probably something like
> >>
> >> awk '!/^#/ && !f++ { print "..." } ; 1'
> >
> > The above code will add the stuff at the very beginning of the
> > script if occasionally the shebang line is not the first line:
> 1. The posted requirements had been
> # the sed invocation inserts the lines at the start of the file
> # after any initial comment lines
> 2. What do you expect from a shebang line that is _ineffective_;
> because it is *not* on the first line?

But as you can see, even if shebang is not the first line, such as the empty line preceding it, the script still works as expected.
> If you happen to have other requirements adjust the condition.
> (Don't expect that we can read your mind from that distance.)
>
> You want to consider [initial] blank lines similar to comments,
> for example?
>
> !(/^#/||!NF) && !f++ ...

I see. Thank you again

Re: Try to understand complex sed command logic instructions.

<ba27321b-ab8a-43a8-bb3f-06408f9aefbfn@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4722&group=comp.unix.shell#4722

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:6214:15c6:: with SMTP id p6mr36669249qvz.12.1639311494214;
Sun, 12 Dec 2021 04:18:14 -0800 (PST)
X-Received: by 2002:a05:620a:103a:: with SMTP id a26mr27619105qkk.785.1639311494012;
Sun, 12 Dec 2021 04:18:14 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 12 Dec 2021 04:18:13 -0800 (PST)
In-Reply-To: <sp4o31$b1o$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=172.105.226.15; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 172.105.226.15
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me> <f65b7516-a4dd-4532-bb19-e214f10db32fn@googlegroups.com>
<sp4o31$b1o$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ba27321b-ab8a-43a8-bb3f-06408f9aefbfn@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 12 Dec 2021 12:18:14 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 41
 by: hongy...@gmail.com - Sun, 12 Dec 2021 12:18 UTC

On Sunday, December 12, 2021 at 7:57:26 PM UTC+8, Janis Papanagnou wrote:
> On 12.12.2021 01:42, hongy...@gmail.com wrote:
> > On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
> >> On 11.12.2021 12:30, hongy...@gmail.com wrote:
> >>> I try to understand the following complex sed command logic
> >>> instructions given here [1]:
> >>> [...]
> >>>
> >>> But the logic contained in the sed code snippet above is still
> >>> obscure to me. Any hints?
> >> Get the sed manual and identify the used commands. Or formulate
> >> the simple algorithm yourself informally and try to identify the
> >> building blocks in the sed code.
> >>>
> >>> OTOH, the final result achieved by the sed workflow does seem so
> >>> trivial. Why do they still insist on providing such an ancient
> >>> sed-based solution?
> >> Maybe it's code from the 1980's?
> >> Maybe the programmer is used to that tool?
> >> Maybe the programmer doesn't know any better tool?
> >> Who knows?
> >> But I see no one insisting in anything.
> >>
> >> Though, for simple tasks you can always write your own version,
> >> and use a language that is or appears to you as less cryptic.
> >>
> >> I'd use Awk, for example, probably something like
> >>
> >> awk '!/^#/ && !f++ { print "..." } ; 1'
> >
> > I try to debug/understand your above code as follows:
> Understand how f++ works - in *any* of the many languages that support
> it - before you try to "debug".
>
> GNU Awk has a debugger that makes it unnecessary in practice to add own
> [possibly or likely wrong] code.

But this way it only supports running script from file, instead of the in-line ones from stdin:

$ awk --help |grep -i debug
-D[file] --debug[=file]

Re: Try to understand complex sed command logic instructions.

<sp53fj$5qt$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4723&group=comp.unix.shell#4723

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Try to understand complex sed command logic instructions.
Date: Sun, 12 Dec 2021 16:11:47 +0100
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <sp53fj$5qt$1@dont-email.me>
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me>
<f65b7516-a4dd-4532-bb19-e214f10db32fn@googlegroups.com>
<sp4o31$b1o$1@dont-email.me>
<ba27321b-ab8a-43a8-bb3f-06408f9aefbfn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 12 Dec 2021 15:11:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="edd6c9f1e2953654c43794cc44b20122";
logging-data="5981"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180WowyXk4JJkkXYLilWhG3"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:HDxItrVevGYco+duZaJOezldnD4=
In-Reply-To: <ba27321b-ab8a-43a8-bb3f-06408f9aefbfn@googlegroups.com>
 by: Janis Papanagnou - Sun, 12 Dec 2021 15:11 UTC

On 12.12.2021 13:18, hongy...@gmail.com wrote:
> On Sunday, December 12, 2021 at 7:57:26 PM UTC+8, Janis Papanagnou wrote:
>>
>> GNU Awk has a debugger that makes it unnecessary in practice to add own
>> [possibly or likely wrong] code.
>
> But this way it only supports running script from file, instead of the in-line ones from stdin:

And your problem with that is what?

You want to understand what the program does, don't you?
So put it in a file to use the debugger. What is your problem.

Janis

>
> $ awk --help |grep -i debug
> -D[file] --debug[=file]
>

Re: Try to understand complex sed command logic instructions.

<sp53r6$cjh$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4724&group=comp.unix.shell#4724

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Try to understand complex sed command logic instructions.
Date: Sun, 12 Dec 2021 16:17:57 +0100
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <sp53r6$cjh$1@dont-email.me>
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me>
<f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
<sp4not$5bs$1@dont-email.me>
<c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 12 Dec 2021 15:17:58 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="edd6c9f1e2953654c43794cc44b20122";
logging-data="12913"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/GSJgX/bc//5MYmC9QEu1q"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:cRFudCR/KZJrOpG1BjCdiL2R18s=
In-Reply-To: <c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sun, 12 Dec 2021 15:17 UTC

On 12.12.2021 13:15, hongy...@gmail.com wrote:
> On Sunday, December 12, 2021 at 7:52:02 PM UTC+8, Janis Papanagnou wrote:
>> On 12.12.2021 07:32, hongy...@gmail.com wrote:
>>> On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
>>>> I'd use Awk, for example, probably something like
>>>>
>>>> awk '!/^#/ && !f++ { print "..." } ; 1'
>>>
>>> The above code will add the stuff at the very beginning of the
>>> script if occasionally the shebang line is not the first line:
>> 1. The posted requirements had been
>> # the sed invocation inserts the lines at the start of the file
>> # after any initial comment lines
>> 2. What do you expect from a shebang line that is _ineffective_;
>> because it is *not* on the first line?
>
> But as you can see, even if shebang is not the first line, such as the empty line preceding it, the script still works as expected.

I have the impression that you still don't understand the mechanics.

Try this code (first line empty) and you see that the #! is ineffective.
---snip---

#!/usr/bin/awk -f
BEGIN { print "Hi" }
---snip---

Or if you run a standard shell script with #!/usr/bin/bash with
some non-standard shell environment you will also get errors.

There are a couple conditions that must be fulfilled for a shebang
script to get that line become active as an interpreter directive.
The one we have been talking about here is that it must be the first
line in the file (no empty lines or anything).

Janis

Re: Try to understand complex sed command logic instructions.

<j1n3pmFbq7oU1@mid.individual.net>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4725&group=comp.unix.shell#4725

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: jpstew...@personalprojects.net (John-Paul Stewart)
Newsgroups: comp.unix.shell
Subject: Re: Try to understand complex sed command logic instructions.
Date: Sun, 12 Dec 2021 15:19:02 -0500
Lines: 35
Message-ID: <j1n3pmFbq7oU1@mid.individual.net>
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me>
<9baba1db-8aff-418b-849f-edadb615939dn@googlegroups.com>
<6eb36997-6921-45a9-9d74-8668caaf79dan@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-Trace: individual.net xvSzhM61XdKwvqAN8OgA5ATdTbdjNZH3nECiksGDuSfCSB5LJv
Cancel-Lock: sha1:xqSdvs2QDQJSfIPSf77wCLPpmsU=
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
Content-Language: en-CA
In-Reply-To: <6eb36997-6921-45a9-9d74-8668caaf79dan@googlegroups.com>
 by: John-Paul Stewart - Sun, 12 Dec 2021 20:19 UTC

On 2021-12-12 01:36, hongy...@gmail.com wrote:
>> {
>>
>> Begin a block of commands
>> a \
>> export PYENV_ROOT="$HOME/.pyenv"
>> a \
>> export PATH="$PYENV_ROOT/bin:$PATH"
>> Append text, which has each embedded newline preceded by a backslash.
>> a \
>> ' -e ':a' -e '$!{n;ba};
>> Failed to figure out the above obscure code block.
>
> Especially, there are two '-e' constructs embedded in outer '-e'
> construct, which is very confusing for me.

No, there is no "embedded" -e construct. Re-read the original post:

> sed -Ei -e '/^([^#]|$)/ {a \
> export PYENV_ROOT="$HOME/.pyenv"
> a \
> export PATH="$PYENV_ROOT/bin:$PATH"
> a \
> ' -e ':a' -e '$!{n;ba};}' ~/.profile

The first -e is followed by an opening single quote ' so everything from
there to the following closing single quote ' on the last line is one
"script" to be executed. Then there is the second -e ':a' after that,
and finally the third -e script (also in single quotes). None are
embedded in the other. There are three consecutive script fragments.

Little details such as the placement of those single quotes are
crucially important in shell/sed/awk scripting. You must learn to
notice them and understand them!

Re: Try to understand complex sed command logic instructions.

<87bl1la37e.fsf@nosuchdomain.example.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4726&group=comp.unix.shell#4726

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.unix.shell
Subject: Re: Try to understand complex sed command logic instructions.
Date: Sun, 12 Dec 2021 14:05:57 -0800
Organization: None to speak of
Lines: 21
Message-ID: <87bl1la37e.fsf@nosuchdomain.example.com>
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me>
<f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
<sp4not$5bs$1@dont-email.me>
<c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="e677d882c77b218b154f35414bf85b42";
logging-data="9447"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Aq8ekKmYeVSQobzzMTd17"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:tqjUXjDJ6TzqbW1byP7e59dYGzA=
sha1:JlsUP3MjHlvFS5eiGbT0He5QpBQ=
 by: Keith Thompson - Sun, 12 Dec 2021 22:05 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com> writes:
> On Sunday, December 12, 2021 at 7:52:02 PM UTC+8, Janis Papanagnou wrote:
[...]
>> 2. What do you expect from a shebang line that is _ineffective_;
>> because it is *not* on the first line?
>
> But as you can see, even if shebang is not the first line, such as the
> empty line preceding it, the script still works as expected.

No, a shebang line must be on the first line, and the "#!" must be the
first two characters of the first line, or it will be ignored (which,
depending on the interpreter, usually means it's treated as a comment).

If a script has no shebang, the way it's executed depends on how you
invoke it. If you invoke it from bash, it executes it as a bash script.
If you invoke it by calling system(script_name), it uses /bin/sh.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: Try to understand complex sed command logic instructions.

<464b0be0-043a-4903-a286-b9dbda88c372n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4727&group=comp.unix.shell#4727

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:620a:4495:: with SMTP id x21mr31186625qkp.604.1639359729883;
Sun, 12 Dec 2021 17:42:09 -0800 (PST)
X-Received: by 2002:ac8:45d2:: with SMTP id e18mr42223390qto.112.1639359729706;
Sun, 12 Dec 2021 17:42:09 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 12 Dec 2021 17:42:09 -0800 (PST)
In-Reply-To: <sp53fj$5qt$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.45; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.45
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me> <f65b7516-a4dd-4532-bb19-e214f10db32fn@googlegroups.com>
<sp4o31$b1o$1@dont-email.me> <ba27321b-ab8a-43a8-bb3f-06408f9aefbfn@googlegroups.com>
<sp53fj$5qt$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <464b0be0-043a-4903-a286-b9dbda88c372n@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Mon, 13 Dec 2021 01:42:09 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 4
 by: hongy...@gmail.com - Mon, 13 Dec 2021 01:42 UTC

On Sunday, December 12, 2021 at 11:11:52 PM UTC+8, Janis Papanagnou wrote:
> You want to understand what the program does, don't you?
> So put it in a file to use the debugger. What is your problem.

I mean, it would be more convenient if I could debug it by running the content of the script from the terminal.

Re: Try to understand complex sed command logic instructions.

<9f1c5867-5377-49fa-9583-4faa15ce8361n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4728&group=comp.unix.shell#4728

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ad4:5cef:: with SMTP id iv15mr39661657qvb.82.1639360155309;
Sun, 12 Dec 2021 17:49:15 -0800 (PST)
X-Received: by 2002:a0c:f9cc:: with SMTP id j12mr39883006qvo.2.1639360155149;
Sun, 12 Dec 2021 17:49:15 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 12 Dec 2021 17:49:14 -0800 (PST)
In-Reply-To: <sp53r6$cjh$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=165.22.107.245; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 165.22.107.245
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me> <f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
<sp4not$5bs$1@dont-email.me> <c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
<sp53r6$cjh$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <9f1c5867-5377-49fa-9583-4faa15ce8361n@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Mon, 13 Dec 2021 01:49:15 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 53
 by: hongy...@gmail.com - Mon, 13 Dec 2021 01:49 UTC

On Sunday, December 12, 2021 at 11:18:02 PM UTC+8, Janis Papanagnou wrote:
> On 12.12.2021 13:15, hongy...@gmail.com wrote:
> > On Sunday, December 12, 2021 at 7:52:02 PM UTC+8, Janis Papanagnou wrote:
> >> On 12.12.2021 07:32, hongy...@gmail.com wrote:
> >>> On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
> >>>> I'd use Awk, for example, probably something like
> >>>>
> >>>> awk '!/^#/ && !f++ { print "..." } ; 1'
> >>>
> >>> The above code will add the stuff at the very beginning of the
> >>> script if occasionally the shebang line is not the first line:
> >> 1. The posted requirements had been
> >> # the sed invocation inserts the lines at the start of the file
> >> # after any initial comment lines
> >> 2. What do you expect from a shebang line that is _ineffective_;
> >> because it is *not* on the first line?
> >
> > But as you can see, even if shebang is not the first line, such as the empty line preceding it, the script still works as expected.
> I have the impression that you still don't understand the mechanics.
>
> Try this code (first line empty) and you see that the #! is ineffective.
> ---snip---
>
> #!/usr/bin/awk -f
> BEGIN { print "Hi" }
> ---snip---

Yes. The above code will fail as follows:

$ ./test.awk
../test.awk: line 3: BEGIN: command not found

But I still don't understand why the following example works:

$ cat test.sh

#!/bin/bash
echo 'Hello.'

$ ./test.sh
Hello.

> Or if you run a standard shell script with #!/usr/bin/bash with
> some non-standard shell environment you will also get errors.
>
> There are a couple conditions that must be fulfilled for a shebang
> script to get that line become active as an interpreter directive.
> The one we have been talking about here is that it must be the first
> line in the file (no empty lines or anything).
>
> Janis

Re: Try to understand complex sed command logic instructions.

<265a1af1-83cf-415d-9235-34d47ef41838n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4729&group=comp.unix.shell#4729

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:6214:1c41:: with SMTP id if1mr38330245qvb.0.1639360272391;
Sun, 12 Dec 2021 17:51:12 -0800 (PST)
X-Received: by 2002:a05:6214:23c8:: with SMTP id hr8mr40447525qvb.79.1639360272297;
Sun, 12 Dec 2021 17:51:12 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 12 Dec 2021 17:51:12 -0800 (PST)
In-Reply-To: <87bl1la37e.fsf@nosuchdomain.example.com>
Injection-Info: google-groups.googlegroups.com; posting-host=165.22.49.196; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 165.22.49.196
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me> <f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
<sp4not$5bs$1@dont-email.me> <c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
<87bl1la37e.fsf@nosuchdomain.example.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <265a1af1-83cf-415d-9235-34d47ef41838n@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Mon, 13 Dec 2021 01:51:12 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 19
 by: hongy...@gmail.com - Mon, 13 Dec 2021 01:51 UTC

On Monday, December 13, 2021 at 6:06:03 AM UTC+8, Keith Thompson wrote:
> "hongy...@gmail.com" <hongy...@gmail.com> writes:
> > On Sunday, December 12, 2021 at 7:52:02 PM UTC+8, Janis Papanagnou wrote:
> [...]
> >> 2. What do you expect from a shebang line that is _ineffective_;
> >> because it is *not* on the first line?
> >
> > But as you can see, even if shebang is not the first line, such as the
> > empty line preceding it, the script still works as expected.
> No, a shebang line must be on the first line, and the "#!" must be the
> first two characters of the first line, or it will be ignored (which,
> depending on the interpreter, usually means it's treated as a comment).

Where is this specification clearly stated?
> If a script has no shebang, the way it's executed depends on how you
> invoke it. If you invoke it from bash, it executes it as a bash script.
> If you invoke it by calling system(script_name), it uses /bin/sh.

Thank you for pointing this out.

Re: Try to understand complex sed command logic instructions.

<0dc47e12-afd3-4e5b-88e2-27ca3f5d8154n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4730&group=comp.unix.shell#4730

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ac8:5c45:: with SMTP id j5mr43418258qtj.58.1639361138886;
Sun, 12 Dec 2021 18:05:38 -0800 (PST)
X-Received: by 2002:ad4:5b8c:: with SMTP id 12mr39736099qvp.83.1639361138703;
Sun, 12 Dec 2021 18:05:38 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 12 Dec 2021 18:05:38 -0800 (PST)
In-Reply-To: <9f1c5867-5377-49fa-9583-4faa15ce8361n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=103.149.249.19; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 103.149.249.19
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me> <f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
<sp4not$5bs$1@dont-email.me> <c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
<sp53r6$cjh$1@dont-email.me> <9f1c5867-5377-49fa-9583-4faa15ce8361n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0dc47e12-afd3-4e5b-88e2-27ca3f5d8154n@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Mon, 13 Dec 2021 02:05:38 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 49
 by: hongy...@gmail.com - Mon, 13 Dec 2021 02:05 UTC

On Monday, December 13, 2021 at 9:49:17 AM UTC+8, hongy...@gmail.com wrote:
> On Sunday, December 12, 2021 at 11:18:02 PM UTC+8, Janis Papanagnou wrote:
> > On 12.12.2021 13:15, hongy...@gmail.com wrote:
> > > On Sunday, December 12, 2021 at 7:52:02 PM UTC+8, Janis Papanagnou wrote:
> > >> On 12.12.2021 07:32, hongy...@gmail.com wrote:
> > >>> On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
> > >>>> I'd use Awk, for example, probably something like
> > >>>>
> > >>>> awk '!/^#/ && !f++ { print "..." } ; 1'
> > >>>
> > >>> The above code will add the stuff at the very beginning of the
> > >>> script if occasionally the shebang line is not the first line:
> > >> 1. The posted requirements had been
> > >> # the sed invocation inserts the lines at the start of the file
> > >> # after any initial comment lines
> > >> 2. What do you expect from a shebang line that is _ineffective_;
> > >> because it is *not* on the first line?
> > >
> > > But as you can see, even if shebang is not the first line, such as the empty line preceding it, the script still works as expected.
> > I have the impression that you still don't understand the mechanics.
> >
> > Try this code (first line empty) and you see that the #! is ineffective.
> > ---snip---
> >
> > #!/usr/bin/awk -f
> > BEGIN { print "Hi" }
> > ---snip---
> Yes. The above code will fail as follows:
>
> $ ./test.awk
> ./test.awk: line 3: BEGIN: command not found
>
>
> But I still don't understand why the following example works:
>
> $ cat test.sh
>
> #!/bin/bash
> echo 'Hello.'
>
> $ ./test.sh
> Hello.

And strace can't trace the system calls and signals of this script:
$ strace ./test.sh
execve("./test.sh", ["./test.sh"], 0x7ffc7d8136c0 /* 102 vars */) = -1 ENOEXEC (Exec format error)
strace: exec: Exec format error
+++ exited with 1 +++

Re: Try to understand complex sed command logic instructions.

<op.1ebhu4xpa3w0dxdave@hodgins.homeip.net>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4731&group=comp.unix.shell#4731

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: dwhodg...@nomail.afraid.org (David W. Hodgins)
Newsgroups: comp.unix.shell
Subject: Re: Try to understand complex sed command logic instructions.
Date: Sun, 12 Dec 2021 21:14:54 -0500
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <op.1ebhu4xpa3w0dxdave@hodgins.homeip.net>
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me>
<f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
<sp4not$5bs$1@dont-email.me>
<c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
<87bl1la37e.fsf@nosuchdomain.example.com>
<265a1af1-83cf-415d-9235-34d47ef41838n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="9bf1082c2e9e23f1cad9d0a07eac035a";
logging-data="15525"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ZZXf6pfwLS5HakofRIYRHyORYE3NSXtg="
User-Agent: Opera Mail/12.16 (Linux)
Cancel-Lock: sha1:8UFlF/m3yYxuItVXp8typ3FiYVo=
 by: David W. Hodgins - Mon, 13 Dec 2021 02:14 UTC

On Sun, 12 Dec 2021 20:51:12 -0500, hongy...@gmail.com <hongyi.zhao@gmail.com> wrote:

> On Monday, December 13, 2021 at 6:06:03 AM UTC+8, Keith Thompson wrote:
>> "hongy...@gmail.com" <hongy...@gmail.com> writes:
>> > On Sunday, December 12, 2021 at 7:52:02 PM UTC+8, Janis Papanagnou wrote:
>> [...]
>> >> 2. What do you expect from a shebang line that is _ineffective_;
>> >> because it is *not* on the first line?
>> >
>> > But as you can see, even if shebang is not the first line, such as the
>> > empty line preceding it, the script still works as expected.
>> No, a shebang line must be on the first line, and the "#!" must be the
>> first two characters of the first line, or it will be ignored (which,
>> depending on the interpreter, usually means it's treated as a comment).
>
> Where is this specification clearly stated?

In the kernel source, specifically the program loader.

From "man execve" ...
Interpreter scripts
An interpreter script is a text file that has execute permission enabled and whose first line is of the form:
#!interpreter [optional-arg]

Regards, Dave Hodgins

Re: Try to understand complex sed command logic instructions.

<f8a4a5a0-3e82-425a-9a53-fafe4bb2005en@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4732&group=comp.unix.shell#4732

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ac8:5712:: with SMTP id 18mr42471283qtw.584.1639362306154;
Sun, 12 Dec 2021 18:25:06 -0800 (PST)
X-Received: by 2002:ae9:e918:: with SMTP id x24mr31333399qkf.264.1639362306013;
Sun, 12 Dec 2021 18:25:06 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 12 Dec 2021 18:25:05 -0800 (PST)
In-Reply-To: <j1n3pmFbq7oU1@mid.individual.net>
Injection-Info: google-groups.googlegroups.com; posting-host=172.104.65.59; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 172.104.65.59
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me> <9baba1db-8aff-418b-849f-edadb615939dn@googlegroups.com>
<6eb36997-6921-45a9-9d74-8668caaf79dan@googlegroups.com> <j1n3pmFbq7oU1@mid.individual.net>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f8a4a5a0-3e82-425a-9a53-fafe4bb2005en@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Mon, 13 Dec 2021 02:25:06 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 41
 by: hongy...@gmail.com - Mon, 13 Dec 2021 02:25 UTC

On Monday, December 13, 2021 at 4:19:08 AM UTC+8, John-Paul Stewart wrote:
> No, there is no "embedded" -e construct. Re-read the original post:
>
> > sed -Ei -e '/^([^#]|$)/ {a \
> > export PYENV_ROOT="$HOME/.pyenv"
> > a \
> > export PATH="$PYENV_ROOT/bin:$PATH"
> > a \
> > ' -e ':a' -e '$!{n;ba};}' ~/.profile
>
> The first -e is followed by an opening single quote ' so everything from
> there to the following closing single quote ' on the last line is one
> "script" to be executed. Then there is the second -e ':a' after that,
> and finally the third -e script (also in single quotes). None are
> embedded in the other. There are three consecutive script fragments.

Ok. Let me try to understand the last two script fragments:

-e ':a' -e '$!{n;ba};}'

:a Make label a for b command.
$ Match the last line.
! After the address (or address-range), and before the command, a ! may be inserted, which specifies
that the command shall only be executed if the address (or address-range) does not match.

{ Begin a block of commands (end with a }).

n N Read/append the next line of input into the pattern space.

b label
Branch to label; if label is omitted, branch to end of script.

; Is used to separate multiple sed commands, IIRC.

By putting all the above knowledge together, I still haven't figured out any clues
pertaining to the goal to be achieved here.
> Little details such as the placement of those single quotes are
> crucially important in shell/sed/awk scripting. You must learn to
> notice them and understand them!

Thank you for pointing this out.

Re: Try to understand complex sed command logic instructions.

<62ae255f-a2c0-4467-a27a-55bf49274d28n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4733&group=comp.unix.shell#4733

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:180c:: with SMTP id t12mr43571471qtc.507.1639362469474;
Sun, 12 Dec 2021 18:27:49 -0800 (PST)
X-Received: by 2002:ac8:5a0b:: with SMTP id n11mr42790096qta.372.1639362469338;
Sun, 12 Dec 2021 18:27:49 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 12 Dec 2021 18:27:49 -0800 (PST)
In-Reply-To: <op.1ebhu4xpa3w0dxdave@hodgins.homeip.net>
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.45; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.45
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me> <f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
<sp4not$5bs$1@dont-email.me> <c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
<87bl1la37e.fsf@nosuchdomain.example.com> <265a1af1-83cf-415d-9235-34d47ef41838n@googlegroups.com>
<op.1ebhu4xpa3w0dxdave@hodgins.homeip.net>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <62ae255f-a2c0-4467-a27a-55bf49274d28n@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Mon, 13 Dec 2021 02:27:49 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 6
 by: hongy...@gmail.com - Mon, 13 Dec 2021 02:27 UTC

On Monday, December 13, 2021 at 10:15:08 AM UTC+8, David W. Hodgins wrote:
> From "man execve" ...
> Interpreter scripts
> An interpreter script is a text file that has execute permission enabled and whose first line is of the form:
> #!interpreter [optional-arg]

Thank you for letting me know this.

Re: Try to understand complex sed command logic instructions.

<sp6ki2$r0r$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4734&group=comp.unix.shell#4734

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: unr...@invalid.ca (William Unruh)
Newsgroups: comp.unix.shell
Subject: Re: Try to understand complex sed command logic instructions.
Date: Mon, 13 Dec 2021 05:09:22 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 71
Message-ID: <sp6ki2$r0r$1@dont-email.me>
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me>
<f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
<sp4not$5bs$1@dont-email.me>
<c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
<sp53r6$cjh$1@dont-email.me>
<9f1c5867-5377-49fa-9583-4faa15ce8361n@googlegroups.com>
Injection-Date: Mon, 13 Dec 2021 05:09:22 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="595500a6c3fc05439296dd6436480bbb";
logging-data="27675"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//WG4yLcYwZ4sAg77dUQrN"
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:UCrJlAv37UqpLR54FTFF2TrhsDU=
 by: William Unruh - Mon, 13 Dec 2021 05:09 UTC

On 2021-12-13, hongy...@gmail.com <hongyi.zhao@gmail.com> wrote:
> On Sunday, December 12, 2021 at 11:18:02 PM UTC+8, Janis Papanagnou wrote:
>> On 12.12.2021 13:15, hongy...@gmail.com wrote:
>> > On Sunday, December 12, 2021 at 7:52:02 PM UTC+8, Janis Papanagnou wrote:
>> >> On 12.12.2021 07:32, hongy...@gmail.com wrote:
>> >>> On Saturday, December 11, 2021 at 8:23:09 PM UTC+8, Janis Papanagnou wrote:
>> >>>> I'd use Awk, for example, probably something like
>> >>>>
>> >>>> awk '!/^#/ && !f++ { print "..." } ; 1'
>> >>>
>> >>> The above code will add the stuff at the very beginning of the
>> >>> script if occasionally the shebang line is not the first line:
>> >> 1. The posted requirements had been
>> >> # the sed invocation inserts the lines at the start of the file
>> >> # after any initial comment lines
>> >> 2. What do you expect from a shebang line that is _ineffective_;
>> >> because it is *not* on the first line?
>> >
>> > But as you can see, even if shebang is not the first line, such as the empty line preceding it, the script still works as expected.
>> I have the impression that you still don't understand the mechanics.
>>
>> Try this code (first line empty) and you see that the #! is ineffective.
>> ---snip---
>>
>> #!/usr/bin/awk -f
>> BEGIN { print "Hi" }
>> ---snip---
>
> Yes. The above code will fail as follows:
>
> $ ./test.awk
> ./test.awk: line 3: BEGIN: command not found
>
>

> But I still don't understand why the following example works:
Becase the system runs this file with bash, with the shebang interpreted
as a comment, and the rest of the lines ar valid bash commands.

>
> $ cat test.sh
>
> #!/bin/bash
> echo 'Hello.'
>
> $ ./test.sh
> Hello.
>
>
>
>> Or if you run a standard shell script with #!/usr/bin/bash with
>> some non-standard shell environment you will also get errors.

You needed toread and understand this sentence.

>>
>> There are a couple conditions that must be fulfilled for a shebang
>> script to get that line become active as an interpreter directive.
>> The one we have been talking about here is that it must be the first
>> line in the file (no empty lines or anything).

It would really help if you did not treat your own prejudices as if they
were revelations from God, and tried to understand what others were
trying to say.

>>
>> Janis

Re: Try to understand complex sed command logic instructions.

<sp6oo1$86i$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4735&group=comp.unix.shell#4735

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Try to understand complex sed command logic instructions.
Date: Mon, 13 Dec 2021 07:20:48 +0100
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <sp6oo1$86i$1@dont-email.me>
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me>
<f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
<sp4not$5bs$1@dont-email.me>
<c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
<sp53r6$cjh$1@dont-email.me>
<9f1c5867-5377-49fa-9583-4faa15ce8361n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 13 Dec 2021 06:20:49 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b3c31537e7eb33caba0c844cef086ec9";
logging-data="8402"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vDX1a+yxz7hObTu9Hw0n3"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:MkEHZXems3Q5TIpSkODy+tsrVBs=
In-Reply-To: <9f1c5867-5377-49fa-9583-4faa15ce8361n@googlegroups.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Mon, 13 Dec 2021 06:20 UTC

On 13.12.2021 02:49, hongy...@gmail.com wrote:
> On Sunday, December 12, 2021 at 11:18:02 PM UTC+8, Janis Papanagnou wrote:
>>
>> Try this code (first line empty) and you see that the #! is ineffective.
>> ---snip---
>>
>> #!/usr/bin/awk -f
>> BEGIN { print "Hi" }
>> ---snip---
>
> Yes. The above code will fail as follows:
>
> $ ./test.awk
> ./test.awk: line 3: BEGIN: command not found
>
>
> But I still don't understand why the following example works:

For the same reason why that program also works without the #! line.

>
> $ cat test.sh
>
> #!/bin/bash
> echo 'Hello.'
>
> $ ./test.sh
> Hello.
>

You may want to inform yourself more thoroughly by reading articles
about #!. Wikipedia - https://en.wikipedia.org/wiki/Shebang_(Unix) -
is a good start and incidentally answers also your question. If you
have specialized questions or want to know about some gory details
have a look into https://www.in-ulm.de/~mascheck/various/shebang/ .

Janis

Re: Try to understand complex sed command logic instructions.

<a20e7722-18e9-460d-b3ec-50b98e029087n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4736&group=comp.unix.shell#4736

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ac8:7942:: with SMTP id r2mr1948497qtt.303.1639381527730;
Sun, 12 Dec 2021 23:45:27 -0800 (PST)
X-Received: by 2002:a05:622a:181d:: with SMTP id t29mr43045988qtc.338.1639381527547;
Sun, 12 Dec 2021 23:45:27 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 12 Dec 2021 23:45:27 -0800 (PST)
In-Reply-To: <sp6oo1$86i$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.39; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.39
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me> <f895cd0a-f2f4-4cd2-b10b-093b32527d33n@googlegroups.com>
<sp4not$5bs$1@dont-email.me> <c10f30db-b500-4124-81b2-d8fee54da0e6n@googlegroups.com>
<sp53r6$cjh$1@dont-email.me> <9f1c5867-5377-49fa-9583-4faa15ce8361n@googlegroups.com>
<sp6oo1$86i$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a20e7722-18e9-460d-b3ec-50b98e029087n@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Mon, 13 Dec 2021 07:45:27 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 46
 by: hongy...@gmail.com - Mon, 13 Dec 2021 07:45 UTC

On Monday, December 13, 2021 at 2:20:55 PM UTC+8, Janis Papanagnou wrote:
> On 13.12.2021 02:49, hongy...@gmail.com wrote:
> > On Sunday, December 12, 2021 at 11:18:02 PM UTC+8, Janis Papanagnou wrote:
> >>
> >> Try this code (first line empty) and you see that the #! is ineffective.
> >> ---snip---
> >>
> >> #!/usr/bin/awk -f
> >> BEGIN { print "Hi" }
> >> ---snip---
> >
> > Yes. The above code will fail as follows:
> >
> > $ ./test.awk
> > ./test.awk: line 3: BEGIN: command not found
> >
> >
> > But I still don't understand why the following example works:

> For the same reason why that program also works without the #! line.

Wonderful clue/hint.

> >
> > $ cat test.sh
> >
> > #!/bin/bash
> > echo 'Hello.'
> >
> > $ ./test.sh
> > Hello.
> >
> You may want to inform yourself more thoroughly by reading articles
> about #!. Wikipedia - https://en.wikipedia.org/wiki/Shebang_(Unix) -
> is a good start and incidentally answers also your question. If you
> have specialized questions or want to know about some gory details
> have a look into https://www.in-ulm.de/~mascheck/various/shebang/ .

I really don't know this website until now. It also has many other priceless notes, such as the following:

https://www.in-ulm.de/~mascheck/various/bourne_args
https://www.in-ulm.de/~mascheck/various/echo+printf/
https://www.in-ulm.de/~mascheck/various/find/#xargs

HZ

Re: Try to understand complex sed command logic instructions.

<a6bc88eb-b1ac-4afe-925e-bd6d5f20752fn@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4738&group=comp.unix.shell#4738

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:620a:2996:: with SMTP id r22mr2800177qkp.485.1639471030705;
Tue, 14 Dec 2021 00:37:10 -0800 (PST)
X-Received: by 2002:a05:6214:27cd:: with SMTP id ge13mr4171632qvb.40.1639471030537;
Tue, 14 Dec 2021 00:37:10 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Tue, 14 Dec 2021 00:37:10 -0800 (PST)
In-Reply-To: <f8a4a5a0-3e82-425a-9a53-fafe4bb2005en@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=143.198.81.109; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 143.198.81.109
References: <e3b2d713-b937-45c6-ab1a-6c67ee7abdacn@googlegroups.com>
<sp2577$6no$1@dont-email.me> <9baba1db-8aff-418b-849f-edadb615939dn@googlegroups.com>
<6eb36997-6921-45a9-9d74-8668caaf79dan@googlegroups.com> <j1n3pmFbq7oU1@mid.individual.net>
<f8a4a5a0-3e82-425a-9a53-fafe4bb2005en@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a6bc88eb-b1ac-4afe-925e-bd6d5f20752fn@googlegroups.com>
Subject: Re: Try to understand complex sed command logic instructions.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Tue, 14 Dec 2021 08:37:10 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 50
 by: hongy...@gmail.com - Tue, 14 Dec 2021 08:37 UTC

On Monday, December 13, 2021 at 10:25:08 AM UTC+8, hongy...@gmail.com wrote:
> On Monday, December 13, 2021 at 4:19:08 AM UTC+8, John-Paul Stewart wrote:
> > No, there is no "embedded" -e construct. Re-read the original post:
> >
> > > sed -Ei -e '/^([^#]|$)/ {a \
> > > export PYENV_ROOT="$HOME/.pyenv"
> > > a \
> > > export PATH="$PYENV_ROOT/bin:$PATH"
> > > a \
> > > ' -e ':a' -e '$!{n;ba};}' ~/.profile
> >
> > The first -e is followed by an opening single quote ' so everything from
> > there to the following closing single quote ' on the last line is one
> > "script" to be executed. Then there is the second -e ':a' after that,
> > and finally the third -e script (also in single quotes). None are
> > embedded in the other. There are three consecutive script fragments.
> Ok. Let me try to understand the last two script fragments:
>
> -e ':a' -e '$!{n;ba};}'

Here [1] I find the correlated example:

$ sed -e '/foo/ {s//bar/; ' -e ':a' -e '$!{n;ba' -e '};}' <<<$'1st foo\nUnrelated\n2nd foo\n3rd foo'
1st bar
Unrelated
2nd foo
3rd foo

[1] https://stackoverflow.com/a/33416489


> :a Make label a for b command.
> $ Match the last line.
> ! After the address (or address-range), and before the command, a ! may be inserted, which specifies
> that the command shall only be executed if the address (or address-range) does not match.
>
> { Begin a block of commands (end with a }).
>
> n N Read/append the next line of input into the pattern space.
>
> b label
> Branch to label; if label is omitted, branch to end of script.
>
> ; Is used to separate multiple sed commands, IIRC.
>
> By putting all the above knowledge together, I still haven't figured out any clues
> pertaining to the goal to be achieved here.
> > Little details such as the placement of those single quotes are
> > crucially important in shell/sed/awk scripting. You must learn to
> > notice them and understand them!
> Thank you for pointing this out.

Pages:12
server_pubkey.txt

rocksolid light 0.9.7
clearnet tor