Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

7 May, 2024: fms is rebuilding. Don't expect much in that section for quite a few days, maybe longer.


devel / comp.unix.shell / Re: find my_script in multiple directories and execute it

SubjectAuthor
* find my_script in multiple directories and execute itHarry
+* Re: find my_script in multiple directories and execute itJosef Möllers
|`* Re: find my_script in multiple directories and execute itHarry
| +* Re: find my_script in multiple directories and execute itJanis Papanagnou
| |`- Re: find my_script in multiple directories and execute itHarry
| `* Re: find my_script in multiple directories and execute itJosef Moellers
|  `- Re: find my_script in multiple directories and execute itHarry
+* Re: find my_script in multiple directories and execute itLew Pitcher
|`* Re: find my_script in multiple directories and execute itHarry
| `- Re: find my_script in multiple directories and execute itLew Pitcher
`* Re: find my_script in multiple directories and execute itHelmut Waitzmann
 +* Re: find my_script in multiple directories and execute itKenny McCormack
 |`* Re: find my_script in multiple directories and execute itHelmut Waitzmann
 | +- Re: find my_script in multiple directories and execute itJanis Papanagnou
 | `- Re: find my_script in multiple directories and execute itKenny McCormack
 +* Re: find my_script in multiple directories and execute itOğuz
 |`* Re: find my_script in multiple directories and execute itKeith Thompson
 | `- Re: find my_script in multiple directories and execute itChristian Weisgerber
 +- Re: find my_script in multiple directories and execute itHelmut Waitzmann
 `* Re: find my_script in multiple directories and execute ithongy...@gmail.com
  +* Re: find my_script in multiple directories and execute ithongy...@gmail.com
  |+- Re: find my_script in multiple directories and execute itBen Bacarisse
  |`* Re: find my_script in multiple directories and execute itHelmut Waitzmann
  | `- Re: find my_script in multiple directories and execute itHelmut Waitzmann
  `- Re: find my_script in multiple directories and execute itHelmut Waitzmann

1
find my_script in multiple directories and execute it

<a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:1349:b0:3b9:b7f6:7cb0 with SMTP id w9-20020a05622a134900b003b9b7f67cb0mr1241821qtk.277.1675961399110;
Thu, 09 Feb 2023 08:49:59 -0800 (PST)
X-Received: by 2002:a0d:e692:0:b0:52e:c6d9:d760 with SMTP id
p140-20020a0de692000000b0052ec6d9d760mr45422ywe.243.1675961398856; Thu, 09
Feb 2023 08:49:58 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.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: Thu, 9 Feb 2023 08:49:58 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=24.84.202.140; posting-account=0cwH1wkAAACyHC8RuqgGpRum9kOwb46T
NNTP-Posting-Host: 24.84.202.140
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
Subject: find my_script in multiple directories and execute it
From: harryooopotter@hotmail.com (Harry)
Injection-Date: Thu, 09 Feb 2023 16:49:59 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1445
 by: Harry - Thu, 9 Feb 2023 16:49 UTC

I have my_script.sh in multiple directories.
How could I find it, then change directory there, and execute it, one by one?

../a/b c/d/my_script.sh
../e/f g/h/my_script.sh
....

find . -name 'my_script.sh' -exec (cd dirname of {} ; sh my_script.sh ) \;

I tried just echoing the dirname with no success ...
$ find . -name update_files_b0783.sh -exec echo ${{}%/*} \;
-bash: ${{}%/*}: bad substitution

Any help appreciated.
TIA

Re: find my_script in multiple directories and execute it

<k4knsoF45pgU1@mid.individual.net>

  copy mid

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

  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: josef@invalid.invalid (Josef Möllers)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Thu, 9 Feb 2023 18:12:56 +0100
Lines: 24
Message-ID: <k4knsoF45pgU1@mid.individual.net>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net Cb1okFjvTvZL0R95VHslTgiXmkue/t9EF4V8tY47x1XlNiYKBc
Cancel-Lock: sha1:mTJ2Dqg3dnHD+ft/4rFCCNKwlNs=
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Content-Language: en-US
In-Reply-To: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
 by: Josef Möllers - Thu, 9 Feb 2023 17:12 UTC

On 09.02.23 17:49, Harry wrote:
> I have my_script.sh in multiple directories.
> How could I find it, then change directory there, and execute it, one by one?
>
> ./a/b c/d/my_script.sh
> ./e/f g/h/my_script.sh
> ...
>
> find . -name 'my_script.sh' -exec (cd dirname of {} ; sh my_script.sh ) \;
>
> I tried just echoing the dirname with no success ...
> $ find . -name update_files_b0783.sh -exec echo ${{}%/*} \;
> -bash: ${{}%/*}: bad substitution

My pragmatic solution: Whip up a script:
#! /bin/bash
dir="${1%/*}"
script="${1##*/}"
cd "$dir" && sh "$script"

Then use the script(name) as the argument to "-exec"

Josef

Re: find my_script in multiple directories and execute it

<ts3ak1$mogt$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Thu, 9 Feb 2023 17:30:41 -0000 (UTC)
Organization: The Pitcher Digital Freehold
Lines: 17
Message-ID: <ts3ak1$mogt$2@dont-email.me>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 9 Feb 2023 17:30:41 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="a0ad817ff2e5c6854ed4a5ce0f8d4755";
logging-data="746013"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FkkigxLSVXrwat1j3ZGSJW5Y8i0rrhB0="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:6VSgkpQ80nUdwYvRT5ktX9c6BhQ=
 by: Lew Pitcher - Thu, 9 Feb 2023 17:30 UTC

On Thu, 09 Feb 2023 08:49:58 -0800, Harry wrote:

> I have my_script.sh in multiple directories.
> How could I find it, then change directory there, and execute it, one by
> one?
>
> ./a/b c/d/my_script.sh ./e/f g/h/my_script.sh ...
>
> find . -name 'my_script.sh' -exec (cd dirname of {} ; sh my_script.sh )
> \;

How about
find . -name 'my_script.sh' -print | while read SP ; do "$SP" ; done

--
Lew Pitcher
"In Skills We Trust"

Re: find my_script in multiple directories and execute it

<ed3ca6ff-b884-4559-b4f1-212f1aa7ef3an@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ac8:570b:0:b0:3b9:bbba:f444 with SMTP id 11-20020ac8570b000000b003b9bbbaf444mr1796609qtw.164.1675966512511;
Thu, 09 Feb 2023 10:15:12 -0800 (PST)
X-Received: by 2002:a81:8042:0:b0:527:a383:964d with SMTP id
q63-20020a818042000000b00527a383964dmr1321595ywf.99.1675966512294; Thu, 09
Feb 2023 10:15:12 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.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: Thu, 9 Feb 2023 10:15:12 -0800 (PST)
In-Reply-To: <k4knsoF45pgU1@mid.individual.net>
Injection-Info: google-groups.googlegroups.com; posting-host=24.84.202.140; posting-account=0cwH1wkAAACyHC8RuqgGpRum9kOwb46T
NNTP-Posting-Host: 24.84.202.140
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com> <k4knsoF45pgU1@mid.individual.net>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ed3ca6ff-b884-4559-b4f1-212f1aa7ef3an@googlegroups.com>
Subject: Re: find my_script in multiple directories and execute it
From: harryooopotter@hotmail.com (Harry)
Injection-Date: Thu, 09 Feb 2023 18:15:12 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 1557
 by: Harry - Thu, 9 Feb 2023 18:15 UTC

On Thursday, February 9, 2023 at 9:13:03 AM UTC-8, Josef Möllers wrote:

> My pragmatic solution: Whip up a script:
> #! /bin/bash
> dir="${1%/*}"
> script="${1##*/}"
> cd "$dir" && sh "$script"
>
> Then use the script(name) as the argument to "-exec"

Josef,
It only went through one of the many directories.

Re: find my_script in multiple directories and execute it

<1a67d531-c879-48b7-8aa4-4ef768e0bf00n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:153:b0:3bb:760d:934b with SMTP id v19-20020a05622a015300b003bb760d934bmr1821498qtw.11.1675966799876;
Thu, 09 Feb 2023 10:19:59 -0800 (PST)
X-Received: by 2002:a25:dcc3:0:b0:8e6:15c7:e455 with SMTP id
y186-20020a25dcc3000000b008e615c7e455mr99146ybe.66.1675966799671; Thu, 09 Feb
2023 10:19:59 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.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: Thu, 9 Feb 2023 10:19:59 -0800 (PST)
In-Reply-To: <ts3ak1$mogt$2@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=24.84.202.140; posting-account=0cwH1wkAAACyHC8RuqgGpRum9kOwb46T
NNTP-Posting-Host: 24.84.202.140
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com> <ts3ak1$mogt$2@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1a67d531-c879-48b7-8aa4-4ef768e0bf00n@googlegroups.com>
Subject: Re: find my_script in multiple directories and execute it
From: harryooopotter@hotmail.com (Harry)
Injection-Date: Thu, 09 Feb 2023 18:19:59 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1530
 by: Harry - Thu, 9 Feb 2023 18:19 UTC

On Thursday, February 9, 2023 at 9:30:51 AM UTC-8, Lew Pitcher wrote:

> How about
> find . -name 'my_script.sh' -print | while read SP ; do "$SP" ; done

Lew
I haven't tried your suggestion yet ...
It has to "cd <the destination directory>" first, because my_script.sh would try to copy files from some_directory to those already existing in <the destination directory>.

Re: find my_script in multiple directories and execute it

<ts3dk0$od4r$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Thu, 9 Feb 2023 19:21:52 +0100
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <ts3dk0$od4r$1@dont-email.me>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<k4knsoF45pgU1@mid.individual.net>
<ed3ca6ff-b884-4559-b4f1-212f1aa7ef3an@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 9 Feb 2023 18:21:52 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="ed9d34eb8d2592a8a438ab030447fd00";
logging-data="799899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19HrOBJ+h0blQqVUEWYBm32"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:Eq/q+xUnh8sTX+//jfPMmOxBl6o=
In-Reply-To: <ed3ca6ff-b884-4559-b4f1-212f1aa7ef3an@googlegroups.com>
 by: Janis Papanagnou - Thu, 9 Feb 2023 18:21 UTC

On 09.02.2023 19:15, Harry wrote:
> On Thursday, February 9, 2023 at 9:13:03 AM UTC-8, Josef Möllers wrote:
>
>> My pragmatic solution: Whip up a script:
>> #! /bin/bash
>> dir="${1%/*}"
>> script="${1##*/}"
>> cd "$dir" && sh "$script"
>>
>> Then use the script(name) as the argument to "-exec"
>
> Josef,
> It only went through one of the many directories.
>

What if you expand the loop in shell...?

find -name "my_script.sh" |
while read f ; do ( cd "${f%/*}" && sh "${f##*/}" ) ; done

But note that you need adjustments for pathological filenames.

Also it might be worth a thought in case it's possible that
malicious files in the directory hierarchy might be executed.

Janis

Re: find my_script in multiple directories and execute it

<ts3f9p$o7ii$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Thu, 9 Feb 2023 18:50:33 -0000 (UTC)
Organization: The Pitcher Digital Freehold
Lines: 17
Message-ID: <ts3f9p$o7ii$1@dont-email.me>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<ts3ak1$mogt$2@dont-email.me>
<1a67d531-c879-48b7-8aa4-4ef768e0bf00n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 9 Feb 2023 18:50:33 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="a0ad817ff2e5c6854ed4a5ce0f8d4755";
logging-data="794194"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/knHKYyYqAhiu4HuxPM39f3Tf4NkazDoo="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:qNrNVjlxbZfE2TT4RH0dpTR/A5E=
 by: Lew Pitcher - Thu, 9 Feb 2023 18:50 UTC

On Thu, 09 Feb 2023 10:19:59 -0800, Harry wrote:

> On Thursday, February 9, 2023 at 9:30:51 AM UTC-8, Lew Pitcher wrote:
>
>> How about find . -name 'my_script.sh' -print | while read SP ; do "$SP"
>> ; done
>
> Lew I haven't tried your suggestion yet ...
> It has to "cd <the destination directory>" first, because my_script.sh
> would try to copy files from some_directory to those already existing in
> <the destination directory>.

That's fair. My suggestion doesn't do that.

--
Lew Pitcher
"In Skills We Trust"

Re: find my_script in multiple directories and execute it

<be5aa04d-6968-4d10-adb3-4c91689f0596n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:84:b0:3b9:bfac:8327 with SMTP id o4-20020a05622a008400b003b9bfac8327mr1784920qtw.315.1675970760003;
Thu, 09 Feb 2023 11:26:00 -0800 (PST)
X-Received: by 2002:a5b:388:0:b0:8c6:51c5:4896 with SMTP id
k8-20020a5b0388000000b008c651c54896mr519581ybp.272.1675970759764; Thu, 09 Feb
2023 11:25:59 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.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: Thu, 9 Feb 2023 11:25:59 -0800 (PST)
In-Reply-To: <ts3dk0$od4r$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=24.84.202.140; posting-account=0cwH1wkAAACyHC8RuqgGpRum9kOwb46T
NNTP-Posting-Host: 24.84.202.140
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<k4knsoF45pgU1@mid.individual.net> <ed3ca6ff-b884-4559-b4f1-212f1aa7ef3an@googlegroups.com>
<ts3dk0$od4r$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <be5aa04d-6968-4d10-adb3-4c91689f0596n@googlegroups.com>
Subject: Re: find my_script in multiple directories and execute it
From: harryooopotter@hotmail.com (Harry)
Injection-Date: Thu, 09 Feb 2023 19:25:59 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1828
 by: Harry - Thu, 9 Feb 2023 19:25 UTC

On Thursday, February 9, 2023 at 10:21:57 AM UTC-8, Janis Papanagnou wrote:

> What if you expand the loop in shell...?
>
> find -name "my_script.sh" |
> while read f ; do ( cd "${f%/*}" && sh "${f##*/}" ) ; done
>
> But note that you need adjustments for pathological filenames.
>
> Also it might be worth a thought in case it's possible that
> malicious files in the directory hierarchy might be executed.
>
> Janis

Janis

It works.
Thanks a lot for your help.

H.

Re: find my_script in multiple directories and execute it

<k4mp1jFedj8U1@mid.individual.net>

  copy mid

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

  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: josef.moellers@invalid.invalid (Josef Moellers)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Fri, 10 Feb 2023 12:44:51 +0100
Lines: 23
Message-ID: <k4mp1jFedj8U1@mid.individual.net>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<k4knsoF45pgU1@mid.individual.net>
<ed3ca6ff-b884-4559-b4f1-212f1aa7ef3an@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net m7SYHEntONQwub9TCNk2OAE56LS1gM8xNx01uzE960KX5qhYE5
Cancel-Lock: sha1:seKJAK5+rlrucu+st215kt25OHA=
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Content-Language: en-US
In-Reply-To: <ed3ca6ff-b884-4559-b4f1-212f1aa7ef3an@googlegroups.com>
 by: Josef Moellers - Fri, 10 Feb 2023 11:44 UTC

On 09.02.23 19:15, Harry wrote:
> On Thursday, February 9, 2023 at 9:13:03 AM UTC-8, Josef Möllers wrote:
>
>> My pragmatic solution: Whip up a script:
>> #! /bin/bash
>> dir="${1%/*}"
>> script="${1##*/}"
>> cd "$dir" && sh "$script"
>>
>> Then use the script(name) as the argument to "-exec"
>
> Josef,
> It only went through one of the many directories.

Although you seem to have found a solution:
find . -name 'my_script.sh' -exec ./josefs_script {} \;
Is that how you called it?

$ find . -name my_script.sh -exec ./josef_script {} \;
my_script.sh called in /tmp/test/a/b/c/d
my_script.sh called in /tmp/test/e/f/g/h

Josef

Re: find my_script in multiple directories and execute it

<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Fri, 10 Feb 2023 21:50:19 +0100
Organization: A noiseless patient Spider
Lines: 15
Sender: Helmut Waitzmann <12f7e638@mail.de>
Message-ID: <837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
MIME-Version: 1.0
Content-Type: text/plain; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: reader01.eternal-september.org; posting-host="f10ea34a4b83ea0750b50b7820522991";
logging-data="1222126"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/T50otk7elQhU6X39BDF5hMl02u99KixE="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Cancel-Lock: sha1:GHpPHJTF2pDXViRue4medaeJNEo=
sha1:ZfaxYFVwUYnMPTJjMMrYq5AboYA=
Mail-Copies-To: nobody
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
 by: Helmut Waitzmann - Fri, 10 Feb 2023 20:50 UTC

Harry <harryooopotter@hotmail.com>:
> I have my_script.sh in multiple directories.
> How could I find it, then change directory there, and execute
> it, one by one?
>
> ./a/b c/d/my_script.sh
> ./e/f g/h/my_script.sh
> ...
>
> find . -name 'my_script.sh' -exec (cd dirname of {} ; sh my_script.sh ) \;
>

find . -name 'my_script.sh' -exec sh -c -- \
'name="${1##*/}" && dir="${1%"$name"} &&
CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;

Re: find my_script in multiple directories and execute it

<ts6dki$3nnam$1@news.xmission.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Fri, 10 Feb 2023 21:40:34 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <ts6dki$3nnam$1@news.xmission.com>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com> <837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
Injection-Date: Fri, 10 Feb 2023 21:40:34 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="3923286"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Fri, 10 Feb 2023 21:40 UTC

In article <837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>,
Helmut Waitzmann <oe.throttle@xoxy.net> wrote:
> Harry <harryooopotter@hotmail.com>:
>> I have my_script.sh in multiple directories.
>> How could I find it, then change directory there, and execute
>> it, one by one?
>>
>> ./a/b c/d/my_script.sh
>> ./e/f g/h/my_script.sh
>> ...
>>
>> find . -name 'my_script.sh' -exec (cd dirname of {} ; sh my_script.sh ) \;
>>
>
> find . -name 'my_script.sh' -exec sh -c -- \
> 'name="${1##*/}" && dir="${1%"$name"} &&
> CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;

That's totally unsuitable for the OP. Nobody is going to be able to use
that unless/until they understand it, and, to be frank, *I* don't
understand it (if I took enough time, I'm sure I could work it out, but why
bother?) and if *I* don't understand it, no way is either this OP or any
general OP going to get it.

Bottom line: You really can't post something like that w/o a detailed
explanation of what it is and why you wrote it that way.

--
Alice was something of a handful to her father, Theodore Roosevelt. He was
once asked by a visiting dignitary about parenting his spitfire of a daughter
and he replied, "I can be President of the United States, or I can control
Alice. I cannot possibly do both."

Re: find my_script in multiple directories and execute it

<6d26e3d1-f7d2-4385-ac52-fbc09f1dd68bn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a37:88c7:0:b0:731:fea3:8180 with SMTP id k190-20020a3788c7000000b00731fea38180mr1229114qkd.214.1676066650500;
Fri, 10 Feb 2023 14:04:10 -0800 (PST)
X-Received: by 2002:a5b:388:0:b0:8c6:51c5:4896 with SMTP id
k8-20020a5b0388000000b008c651c54896mr899856ybp.272.1676066650287; Fri, 10 Feb
2023 14:04:10 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.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: Fri, 10 Feb 2023 14:04:09 -0800 (PST)
In-Reply-To: <k4mp1jFedj8U1@mid.individual.net>
Injection-Info: google-groups.googlegroups.com; posting-host=24.84.202.140; posting-account=0cwH1wkAAACyHC8RuqgGpRum9kOwb46T
NNTP-Posting-Host: 24.84.202.140
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<k4knsoF45pgU1@mid.individual.net> <ed3ca6ff-b884-4559-b4f1-212f1aa7ef3an@googlegroups.com>
<k4mp1jFedj8U1@mid.individual.net>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6d26e3d1-f7d2-4385-ac52-fbc09f1dd68bn@googlegroups.com>
Subject: Re: find my_script in multiple directories and execute it
From: harryooopotter@hotmail.com (Harry)
Injection-Date: Fri, 10 Feb 2023 22:04:10 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2366
 by: Harry - Fri, 10 Feb 2023 22:04 UTC

On Friday, February 10, 2023 at 3:44:58 AM UTC-8, Josef Moellers wrote:
> On 09.02.23 19:15, Harry wrote:
> > On Thursday, February 9, 2023 at 9:13:03 AM UTC-8, Josef Möllers wrote:
> >
> >> My pragmatic solution: Whip up a script:
> >> #! /bin/bash
> >> dir="${1%/*}"
> >> script="${1##*/}"
> >> cd "$dir" && sh "$script"
> >>
> >> Then use the script(name) as the argument to "-exec"
> >
> > Josef,
> > It only went through one of the many directories.
> Although you seem to have found a solution:
> find . -name 'my_script.sh' -exec ./josefs_script {} \;
> Is that how you called it?
>
> $ find . -name my_script.sh -exec ./josef_script {} \;
> my_script.sh called in /tmp/test/a/b/c/d
> my_script.sh called in /tmp/test/e/f/g/h
>
> Josef

Josef

My apology, I must have done it wrong the first time.
After repeating the test, it works.
Yes, that's the way I called your script, not ./josef_script, but /some/path/josef_script; but it shouldn't matter.

H.

Re: find my_script in multiple directories and execute it

<ts7an1$1ba5i$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: oguzismailuysal@gmail.com (Oğuz)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Sat, 11 Feb 2023 08:56:49 +0300
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <ts7an1$1ba5i$1@dont-email.me>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 11 Feb 2023 05:56:49 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="6c07b6378b4d63b65bebbe9ec04c7124";
logging-data="1419442"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/WZAWQIwOWxwyLVB+gmIwrbmgJcHE1tV8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:ds5sh22dtb8+vAthhr9l1HvQyb8=
In-Reply-To: <837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
Content-Language: en-US
 by: Oğuz - Sat, 11 Feb 2023 05:56 UTC

On 2/10/23 11:50 PM, Helmut Waitzmann wrote:
>   find . -name 'my_script.sh' -exec sh -c -- \
>     'name="${1##*/}" && dir="${1%"$name"} &&
>      CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;

That's

find . -name my_script.sh -execdir sh {} \;

with GNU find. Is -execdir not standard yet?

Re: find my_script in multiple directories and execute it

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

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Fri, 10 Feb 2023 22:53:48 -0800
Organization: None to speak of
Lines: 20
Message-ID: <87sffcd75v.fsf@nosuchdomain.example.com>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
<ts7an1$1ba5i$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader01.eternal-september.org; posting-host="90bf916fd25db4ec2ca7ac7a7e3306a3";
logging-data="1430902"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19yw97WXr1ebJ3QzeMPnAXy"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:x9fVbU5BnmqfxhffoOOfcccMaDA=
sha1:GWVwcr9XbNDqMDq1PW63Za9qOS8=
 by: Keith Thompson - Sat, 11 Feb 2023 06:53 UTC

Oğuz <oguzismailuysal@gmail.com> writes:
> On 2/10/23 11:50 PM, Helmut Waitzmann wrote:
>>   find . -name 'my_script.sh' -exec sh -c -- \
>>     'name="${1##*/}" && dir="${1%"$name"} &&
>>      CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;
>
> That's
>
> find . -name my_script.sh -execdir sh {} \;
>
> with GNU find. Is -execdir not standard yet?

It's not specified by POSIX, but the GNU find manual says it "was
introduced by the BSD family of operating systems". (It doesn't
say when.)

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

Re: find my_script in multiple directories and execute it

<slrntuf7bc.2aop.naddy@lorvorc.mips.inka.de>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: naddy@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Sat, 11 Feb 2023 13:51:40 -0000 (UTC)
Message-ID: <slrntuf7bc.2aop.naddy@lorvorc.mips.inka.de>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
<ts7an1$1ba5i$1@dont-email.me> <87sffcd75v.fsf@nosuchdomain.example.com>
Injection-Date: Sat, 11 Feb 2023 13:51:40 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="76570"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Sat, 11 Feb 2023 13:51 UTC

On 2023-02-11, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:

>> with GNU find. Is -execdir not standard yet?
>
> It's not specified by POSIX, but the GNU find manual says it "was
> introduced by the BSD family of operating systems". (It doesn't
> say when.)

It wasn't in 4.4BSD. Todd Miller added it on OpenBSD in 1996 and
from there is was copied to FreeBSD and eventually NetBSD.

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: find my_script in multiple directories and execute it

<838rh39u4r.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Sun, 12 Feb 2023 09:15:48 +0100
Organization: A noiseless patient Spider
Lines: 104
Sender: Helmut Waitzmann <12f7e638@mail.de>
Message-ID: <838rh39u4r.fsf@helmutwaitzmann.news.arcor.de>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
<ts6dki$3nnam$1@news.xmission.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: reader01.eternal-september.org; posting-host="2312441a3779777582ef705ffe9625ed";
logging-data="1824649"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18xUc2NnpuSRxlBD/7pz4ZdX/r5dhZcxGU="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Cancel-Lock: sha1:AFn7W5hrWS36n/mLdKT+wodzBcY=
sha1:QN54TWPaviMJMyPKmAwXqznsljo=
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mail-Copies-To: nobody
 by: Helmut Waitzmann - Sun, 12 Feb 2023 08:15 UTC

gazelle@shell.xmission.com (Kenny McCormack):
> In article <837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>,
> Helmut Waitzmann <oe.throttle@xoxy.net> wrote:
>>
>> find . -name 'my_script.sh' -exec sh -c -- \
>> 'name="${1##*/}" && dir="${1%"$name"} &&
>> CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;
>
> That's totally unsuitable for the OP.
>

It will solve the OP's problem.  Your post will not.

Josef Möllers solution presented a shell script, which needs be
stored somewhere in a file of its own, that is, the command line
asked by Harry won't be self‐contained.  This might or might not
be a problem.

Janis' solution (as he already stated) won't work with
pathological filenames, but he doesn't show how the adjustments
to correct this would be.

> Nobody is going to be able to use that unless/until they
> understand it, and, to be frank, *I* don't understand it (if I
> took enough time, I'm sure I could work it out, but why bother?)

You are free to refrain from working it out.

> and if *I* don't understand it, no way is either this OP or any
> general OP going to get it.

Do you conclude from what is incomprehensible for you will be
incomprehensible for everybody else?

Do you understand, why Janis' solution fails at "pathological"
filenames?  Do you know how to solve that problem?  Do you know
that on the system API level there are no such pathological
filenames, that is, the pathological effect is introduced alone
by a solution which neglects the existence of such pathological
filenames?

> Bottom line: You really can't post something like that w/o a
> detailed explanation of what it is and why you wrote it that
> way.

I don't know what parts of that solution need to be explained, as
that depends of the knowledge of the reader.  Feel free to ask.

In the bottom line, my solution works like Josef's, but avoids
having a separate shell script:  Instead of using a separate
script file it makes use of the

sh -c -- command_line sh parameters...

invocation mechanism of the shell.  To understand what that is,
consult the explanation of the "-c" invocation option of the
shell in the shell's manual page.  It boils down to invoke the
shell with the option "-c" and a shell script's content supplied
as the first non‐option operand: "command_line".

Compare the contents of the command_line parameter with the
contents of Josef's shell script:  The command_line parameter
resembles the following shell script:

name="${1##*/}" &&
dir="${1%"$name"}" &&
CDPATH= cd -- "${dir:=./}" &&
sh ./"$name"

Compare with Josef's shell script:  Do you see the relationship?

dir="${1%/*}"
script="${1##*/}"
cd "$dir" && sh "$script"

Now, for the differences between the two:  Answer the following
questions:

What will the contents of the "dir" variable be, when Harry
happens to invoke his "find" command with the root directory ("/")
as the starting point?  To what directory will the "cd" command
change when the "find" command finds the "/my_script.sh" file?

What will the "cd" command do, if Harry happens to invoke his
"find" command with a directory the name of starts with a "-"?

What will the "cd" command do, if the CDPATH environment variable
happens to be defined?

For the answers, look into the description of the ${var%pattern}
variable expansion and into the description of the "cd" shell
builtin command in the shell manual page.

You are free of course to present a solution which is easier to
understand.

Re: find my_script in multiple directories and execute it

<tsadn6$1o876$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Sun, 12 Feb 2023 11:06:30 +0100
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <tsadn6$1o876$1@dont-email.me>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
<ts6dki$3nnam$1@news.xmission.com>
<838rh39u4r.fsf@helmutwaitzmann.news.arcor.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 12 Feb 2023 10:06:30 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="5be43b48baeec2670aaba1e5d60a0794";
logging-data="1843430"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+SDdvzsn6V81f+u9F6JqNR"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:/ztE3TyaLdG9PWrtYa9b+YZ2f54=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <838rh39u4r.fsf@helmutwaitzmann.news.arcor.de>
 by: Janis Papanagnou - Sun, 12 Feb 2023 10:06 UTC

On 12.02.2023 09:15, Helmut Waitzmann wrote:
> gazelle@shell.xmission.com (Kenny McCormack):
>> [...]

Since my post was referenced I just want to reply on this:

> Janis' solution (as he already stated) won't work with pathological
> filenames, but he doesn't show how the adjustments to correct this
> would be.

My observation is, indeed, that for a specific task a 99% solution
usually helps the asking poster in most cases to 100% fulfill his
task.

Another observation is that when we need a tool for some task that
shall be used over years by many users reliably then any less than
100% coverage may appear to be insufficient, either because at some
time we need to add functionality to be usable for "pathological"
cases, or it may be even a security issue associated with it.

The question is (as far as I am concerned) what effort to spend for
a concrete question. In case of the scripts I use I typically start
with a line or few lines of code that "does the job"; once the state
of usage changes and I want a reliable tool I start adding comments
to the code, use getopts, sometimes signal handling, rework the user
interface to be more flexible or alleviate usage, and whatnot before
that tool goes into my local 'bin' directory. A few operational lines
of code evolves towards a bulletproof, maintainable software tool of
sometimes even many pages of code. (If you think I'm exaggerating,
think e.g. about ^C aborted processes and inconsistencies you leave
e.g. by only partly executed scripts on parts of the directory tree.)

Some improvements are more trivial than others (like using 'cd --'
instead of 'cd') others may be less trivial (line '\n' in filenames).
All sorts of changes for improvements may be added [on demand].

Here I dare to draw the relation to Kenny's quite terse (and probably
rough appearing) comment; simple solutions are easier to grasp than
complex ones, and these 'find' specific syntactical details (together
with all the quoting and escaping orgy) may be considered being too
opaque - personally I don't mind (although I would avoid such in my
own scripts if possible).

To come to an end; if the tools work for the application domain (or
according to specifications, that we rarely see here in Usenet) then
there's nothing to "correct" in a solution. If requirements change
then you have to adjust or change the solution.

In our case I would be more concerned about "script-injections" that
I mentioned in my post. But here as well; it may not be an issue in
the OPs application domain. So a hint suffices and changes can later
be made if the application domain requires it (now or at some time).

Even though the OP already stated "It works." - You're welcome, Harry,
BTW -, I also appreciate to see the completely 'find' based solution
that you additionally provided afterwards. It's certainly good we can
choose (or learn from).

Janis

Re: find my_script in multiple directories and execute it

<tsaq98$3pugf$1@news.xmission.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Sun, 12 Feb 2023 13:40:56 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tsaq98$3pugf$1@news.xmission.com>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com> <837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de> <ts6dki$3nnam$1@news.xmission.com> <838rh39u4r.fsf@helmutwaitzmann.news.arcor.de>
Injection-Date: Sun, 12 Feb 2023 13:40:56 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="3996175"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sun, 12 Feb 2023 13:40 UTC

In article <838rh39u4r.fsf@helmutwaitzmann.news.arcor.de>,
Helmut Waitzmann <oe.throttle@xoxy.net> wrote:
> gazelle@shell.xmission.com (Kenny McCormack):
>> In article <837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>,
>> Helmut Waitzmann <oe.throttle@xoxy.net> wrote:
>>>
>>> find . -name 'my_script.sh' -exec sh -c -- \
>>> 'name="${1##*/}" && dir="${1%"$name"} &&
>>> CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;
>>
>> That's totally unsuitable for the OP.
>>
>
> It will solve the OP's problem.

No, it won't. Not really.

>Your post will not.

I did not post any code, so this comment of yours is noise.

--
The difference between communism and capitalism?
In capitalism, man exploits man. In communism, it's the other way around.

- Daniel Bell, The End of Ideology (1960) -

Re: find my_script in multiple directories and execute it

<837cwm8yea.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Sun, 12 Feb 2023 20:41:17 +0100
Organization: A noiseless patient Spider
Lines: 9
Sender: Helmut Waitzmann <12f7e638@mail.de>
Message-ID: <837cwm8yea.fsf@helmutwaitzmann.news.arcor.de>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: reader01.eternal-september.org; posting-host="2312441a3779777582ef705ffe9625ed";
logging-data="1963400"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18HXHXu5s5pIBF94pmoJiInElo1tW0O3B0="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Cancel-Lock: sha1:58elrWgvj70pWwDdaDzAzTMW3wY=
sha1:qo0qUQI1+VwBUwhAuNg2RZi8vAU=
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mail-Copies-To: nobody
 by: Helmut Waitzmann - Sun, 12 Feb 2023 19:41 UTC

Helmut Waitzmann <nn.throttle@xoxy.net>:
> find . -name 'my_script.sh' -exec sh -c -- \
> 'name="${1##*/}" && dir="${1%"$name"} &&
^
There is a quotation mark missing:  The correct line should be

'name="${1##*/}" && dir="${1%"$name"}" &&

> CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;

Re: find my_script in multiple directories and execute it

<6c2bb8d2-47bf-4cbe-b120-10d2806651e0n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a37:aa03:0:b0:730:f188:8ccd with SMTP id t3-20020a37aa03000000b00730f1888ccdmr1296410qke.75.1676244531862;
Sun, 12 Feb 2023 15:28:51 -0800 (PST)
X-Received: by 2002:a05:6902:241:b0:92a:dc60:1074 with SMTP id
k1-20020a056902024100b0092adc601074mr50677ybs.652.1676244531582; Sun, 12 Feb
2023 15:28:51 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.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: Sun, 12 Feb 2023 15:28:51 -0800 (PST)
In-Reply-To: <837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
Injection-Info: google-groups.googlegroups.com; posting-host=60.249.28.127; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 60.249.28.127
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com> <837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6c2bb8d2-47bf-4cbe-b120-10d2806651e0n@googlegroups.com>
Subject: Re: find my_script in multiple directories and execute it
From: hongyi.zhao@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 12 Feb 2023 23:28:51 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1868
 by: hongy...@gmail.com - Sun, 12 Feb 2023 23:28 UTC

On Saturday, February 11, 2023 at 5:00:27 AM UTC+8, Helmut Waitzmann wrote:
> Harry <harryoo...@hotmail.com>:
> > I have my_script.sh in multiple directories.
> > How could I find it, then change directory there, and execute
> > it, one by one?
> >
> > ./a/b c/d/my_script.sh
> > ./e/f g/h/my_script.sh
> > ...
> >
> > find . -name 'my_script.sh' -exec (cd dirname of {} ; sh my_script.sh ) \;
> >
> find . -name 'my_script.sh' -exec sh -c -- \
> 'name="${1##*/}" && dir="${1%"$name"} &&
> CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;

Will this subshell-based technique suffer performance bottlenecks under heavy usage?

Regards,
Zhao

Re: find my_script in multiple directories and execute it

<3951690f-ec29-4bea-9805-9400cb2205ean@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:1822:b0:3b8:671e:9a9c with SMTP id t34-20020a05622a182200b003b8671e9a9cmr2598068qtc.75.1676244703213;
Sun, 12 Feb 2023 15:31:43 -0800 (PST)
X-Received: by 2002:a5b:508:0:b0:893:b1db:97fa with SMTP id
o8-20020a5b0508000000b00893b1db97famr1994437ybp.603.1676244702920; Sun, 12
Feb 2023 15:31:42 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.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: Sun, 12 Feb 2023 15:31:42 -0800 (PST)
In-Reply-To: <6c2bb8d2-47bf-4cbe-b120-10d2806651e0n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=60.249.28.127; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 60.249.28.127
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de> <6c2bb8d2-47bf-4cbe-b120-10d2806651e0n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3951690f-ec29-4bea-9805-9400cb2205ean@googlegroups.com>
Subject: Re: find my_script in multiple directories and execute it
From: hongyi.zhao@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 12 Feb 2023 23:31:43 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2191
 by: hongy...@gmail.com - Sun, 12 Feb 2023 23:31 UTC

On Monday, February 13, 2023 at 7:28:54 AM UTC+8, hongy...@gmail.com wrote:
> On Saturday, February 11, 2023 at 5:00:27 AM UTC+8, Helmut Waitzmann wrote:
> > Harry <harryoo...@hotmail.com>:
> > > I have my_script.sh in multiple directories.
> > > How could I find it, then change directory there, and execute
> > > it, one by one?
> > >
> > > ./a/b c/d/my_script.sh
> > > ./e/f g/h/my_script.sh
> > > ...
> > >
> > > find . -name 'my_script.sh' -exec (cd dirname of {} ; sh my_script.sh ) \;
> > >
> > find . -name 'my_script.sh' -exec sh -c -- \
> > 'name="${1##*/}" && dir="${1%"$name"} &&
> > CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;
> Will this subshell-based technique suffer performance bottlenecks under heavy usage?

Another cumbersome is: This approach requires a variety of nesting of single and double quotes and requires great care in writing.
> Regards,
> Zhao

Re: find my_script in multiple directories and execute it

<87o7pyux4x.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.usenet@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Mon, 13 Feb 2023 02:17:34 +0000
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <87o7pyux4x.fsf@bsb.me.uk>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
<6c2bb8d2-47bf-4cbe-b120-10d2806651e0n@googlegroups.com>
<3951690f-ec29-4bea-9805-9400cb2205ean@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="e37c0bf26f0db513a5726eff3573b828";
logging-data="2040210"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18MpecZybDggC4bLG/dxPIlgzlHXcP05bc="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:yDxKZGwOLACDWM4MhUyWGMzQkhA=
sha1:yWs9FeqnspYtIXpxmAbG+oxyT7o=
X-BSB-Auth: 1.899714a839f92967a2aa.20230213021734GMT.87o7pyux4x.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 13 Feb 2023 02:17 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com> writes:

> On Monday, February 13, 2023 at 7:28:54 AM UTC+8, hongy...@gmail.com wrote:
>> On Saturday, February 11, 2023 at 5:00:27 AM UTC+8, Helmut Waitzmann wrote:
>> > Harry <harryoo...@hotmail.com>:
>> > > I have my_script.sh in multiple directories.
>> > > How could I find it, then change directory there, and execute
>> > > it, one by one?
>> > >
>> > > ./a/b c/d/my_script.sh
>> > > ./e/f g/h/my_script.sh
>> > > ...
>> > >
>> > > find . -name 'my_script.sh' -exec (cd dirname of {} ; sh my_script.sh ) \;
>> > >
>> > find . -name 'my_script.sh' -exec sh -c -- \
>> > 'name="${1##*/}" && dir="${1%"$name"} &&
>> > CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;
>>
>> Will this subshell-based technique suffer performance bottlenecks
>> under heavy usage?
>
> Another cumbersome is: This approach requires a variety of nesting of
> single and double quotes and requires great care in writing.

I would probably do something like this:

dir="$PWD"
find . -name my_script.sh | while read script; do
cd "$(dirname "$script")"
./"$(basename "$script")"
cd "$dir"
done

but for some uses this could be simplified to something like

find "$PWD" -name my_script.sh | while read script; do
cd "$(dirname "$script")"
"$script"
done

which relies on doing the find with an absolute path.

--
Ben.

Re: find my_script in multiple directories and execute it

<83h6vp739j.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Mon, 13 Feb 2023 20:51:20 +0100
Organization: A noiseless patient Spider
Lines: 97
Sender: Helmut Waitzmann <12f7e638@mail.de>
Message-ID: <83h6vp739j.fsf@helmutwaitzmann.news.arcor.de>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
<6c2bb8d2-47bf-4cbe-b120-10d2806651e0n@googlegroups.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: reader01.eternal-september.org; posting-host="404fc4b7df1f2fcd302bc1bc56633a01";
logging-data="2355481"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/j1RwlGsgpkr4hZGh8Aum/4SkBXl3Vty8="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Cancel-Lock: sha1:lRWYIJdfLnBTiQfHLvi3wEyx+EM=
sha1:GvAGHrQDqJgE2LFU61a/XSMrwNE=
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mail-Copies-To: nobody
 by: Helmut Waitzmann - Mon, 13 Feb 2023 19:51 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com>:
> On Saturday, February 11, 2023 at 5:00:27 AM UTC+8,
> Helmut Waitzmann wrote:
>> Harry <harryoo...@hotmail.com>:
>>> I have my_script.sh in multiple directories.
>>> How could I find it, then change directory there, and execute
>>> it, one by one?
>>>
>>> ./a/b c/d/my_script.sh
>>> ./e/f g/h/my_script.sh
>>> ...
>>>
>>> find . -name 'my_script.sh' -exec (cd dirname of {} ; sh my_script.sh ) \;
>>>
>> find . -name 'my_script.sh' -exec sh -c -- \
>> 'name="${1##*/}" && dir="${1%"$name"} &&
>> CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;
>
> Will this subshell-based technique suffer performance
> bottlenecks under heavy usage?

According to the OP's requirements, for each found
"my_script.sh", the working directory shall be changed to the
directory where the found script lies, then the script executed.

As POSIX‐"find" hasn't got a predicate that changes the working
directory[1], one has to use a program that first changes its
working directory and then invokes the "execve" system call to
start a shell interpreting the script.  Of course one may write
such a program (in a manner like the programs "nohup" or
"nice").  But this goal is easier achieved by using a shell with
the option "-c" like above.

[1] GNU "find" does have such a predicate, though: "-execdir". 
Oğuz already noted that.

A variant would be

find . -name 'my_script.sh' -exec sh -c -- '
for pathname
do
(
name="${pathname##*/}" &&
dir="${pathname%"$name"}" &&
CDPATH= cd -- "${dir:=./}" &&
exec sh ./"$name"
)
done' sh \{\} +

which will cause "find" to invoke the directory changing shell
("sh -c -- ...") on multiple shell scripts found, thus invoke
fewer "sh -c -- ..." shells.

But this advantage implicates, that the shell has to fork
itself – see the parentheses in the "for" loop – before it
changes the working directory and "exec"s the "sh" interpreting
the script.

=> There are fewer "exec"s as before but not fewer "fork"s. 
Also, there is one process more than before running, which might
be a disadvantage when there is few free memory.

Also, there might be another problem, when "find" gathers
multiple found scripts.  If one invocation ot the script removes
or creates any other script that shall resp. should be invoked
later (Harry didn't write anything about that), then the shell
running the "for" loop will try to invoke a non‐existing shell
script resp. omit to invoke a just created shell script.

I won't bother using the "for" loop variant.

(Quoting again:)

> Will this subshell-based technique suffer performance
> bottlenecks under heavy usage?

Maybe.  But if one does

find . -name my_script.sh -print |
while IFS= read -r pathname
do
(
name="${pathname##*/}" &&
dir="${pathname%"$name"}" &&
CDPATH= cd -- "${dir:=./}" &&
exec sh ./"$name"
)
done

one will have almost the same implications as with the "for" loop
above plus the problems with pathological directory names.

Re: find my_script in multiple directories and execute it

<83edqt72gm.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Mon, 13 Feb 2023 21:08:41 +0100
Organization: A noiseless patient Spider
Lines: 39
Sender: Helmut Waitzmann <12f7e638@mail.de>
Message-ID: <83edqt72gm.fsf@helmutwaitzmann.news.arcor.de>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
<6c2bb8d2-47bf-4cbe-b120-10d2806651e0n@googlegroups.com>
<3951690f-ec29-4bea-9805-9400cb2205ean@googlegroups.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: reader01.eternal-september.org; posting-host="404fc4b7df1f2fcd302bc1bc56633a01";
logging-data="2358679"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19GCIXBwxFiyfK2ZN1DJIiZ9/qmf1yExOo="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Cancel-Lock: sha1:+147bQYitEfBsR0tsuFypjV0nvk=
sha1:JI+CrQAyorKBa2j9GqOvqXo1nyw=
Mail-Copies-To: nobody
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
 by: Helmut Waitzmann - Mon, 13 Feb 2023 20:08 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com>:
> On Monday, February 13, 2023 at 7:28:54 AM UTC+8,
> hongy...@gmail.com wrote:
>> On Saturday, February 11, 2023 at 5:00:27 AM UTC+8,
>> Helmut Waitzmann wrote:
>>> Harry <harryoo...@hotmail.com>:
>>>> I have my_script.sh in multiple directories.
>>>>
>>>> How could I find it, then change directory there, and execute
>>>> it, one by one?
>>>>
>>>> find . -name 'my_script.sh' -exec (cd dirname of {} ; sh my_script.sh ) \;
>>>>
>>> find . -name 'my_script.sh' -exec sh -c -- \
>>> 'name="${1##*/}" && dir="${1%"$name"} &&
>>> CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;

[…]

> Another cumbersome is: This approach requires a variety of
> nesting of single and double quotes and requires great care in
> writing.

Yes.  The shell language always requires great care in writing. 
That's true.  A part of that language are the quoting rules.  If
one writes a shell command line which invokes a shell giving it a
shell command line as a parameter (which is the case with the
"-c" shell invocation option), then one has to put a command
line, written as one word, into a command line, which may cause
nested quoting and requires great care in writing.

An alternative approach would be, what Josef Möllers did: store
that "inner" command line into a script rather than supply it
quoted with the shell invocation option "-c".  This saves you
from nested quoting but you need to store the script in a file of
its own.

Re: find my_script in multiple directories and execute it

<835yc46j82.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: find my_script in multiple directories and execute it
Date: Tue, 14 Feb 2023 22:16:29 +0100
Organization: A noiseless patient Spider
Lines: 58
Sender: Helmut Waitzmann <12f7e638@mail.de>
Message-ID: <835yc46j82.fsf@helmutwaitzmann.news.arcor.de>
References: <a7fb9eaf-d5a6-41f7-9f71-e2afe05a7b33n@googlegroups.com>
<837cwpb5ys.fsf@helmutwaitzmann.news.arcor.de>
<6c2bb8d2-47bf-4cbe-b120-10d2806651e0n@googlegroups.com>
<3951690f-ec29-4bea-9805-9400cb2205ean@googlegroups.com>
<83edqt72gm.fsf@helmutwaitzmann.news.arcor.de>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Injection-Info: reader01.eternal-september.org; posting-host="ce93d4d6ad7bd717f552eaacf750cf79";
logging-data="2779440"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KizNkgWMYOmpCWyvWTi9lRWLZ/Zzjdag="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Cancel-Lock: sha1:slfgftQOyY1krH10TbQEcXiSB8U=
sha1:CKTjq0URz+bv2gA0e3dc3+DCB/A=
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mail-Copies-To: nobody
 by: Helmut Waitzmann - Tue, 14 Feb 2023 21:16 UTC

Helmut Waitzmann <nn.throttle@xoxy.net>:
> "hongy...@gmail.com" <hongyi.zhao@gmail.com>:
>> On Monday, February 13, 2023 at 7:28:54 AM UTC+8,
>> hongy...@gmail.com wrote:
>>> On Saturday, February 11, 2023 at 5:00:27 AM UTC+8,
>>> Helmut Waitzmann wrote:
>>>> Harry <harryoo...@hotmail.com>:
>>>>> I have my_script.sh in multiple directories.
>>>>>
>>>>> How could I find it, then change directory there, and execute
>>>>> it, one by one?
>>>>>
>>>> find . -name 'my_script.sh' -exec sh -c -- \
>>>> 'name="${1##*/}" && dir="${1%"$name"} &&
>>>> CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;
>
> […]
>
> The shell language always requires great care in writing. 
> That's true.  A part of that language are the quoting rules.  If
> one writes a shell command line which invokes a shell giving it a
> shell command line as a parameter (which is the case with the
> "-c" shell invocation option), then one has to put a command
> line, written as one word, into a command line, which may cause
> nested quoting and requires great care in writing.

In order to make the command line be one word, one needs to quote
that command line.  This can be achieved as follows:

First, write the command line in an unquoted, "normal" form, like
one would do it when writing a shell script in a file (like Josef
Möllers did).

Then, using the find‐and‐replace capabilities of the text editor,
replace every apostrophe ("'") in the command line by the
sequence of the four characters apostrophe, backslash,
apostrophe, apostrophe ("'\''").

Finally prepend and append an apostrophe to the command line.

In the example above, there are no apostrophes in the unquoted
command line

name="${1##*/}" && dir="${1%"$name"}" &&
CDPATH= cd -- "${dir:=./}" && sh ./"$name"

so there is nothing to be replaced.  Just prepend and append an
apostrophe:

'name="${1##*/}" && dir="${1%"$name"}" &&
CDPATH= cd -- "${dir:=./}" && sh ./"$name"'

That's it.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor