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"
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 )
\;
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.
How about
find . -name 'my_script.sh' -print | while read SP ; do "$SP" ; done
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>.
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
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.
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 ) \;
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,Although you seem to have found a solution:
It only went through one of the many directories.
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
Harry <[email protected]>:
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 \{\} \;
find . -name 'my_script.sh' -exec sh -c -- \
'name="${1##*/}" && dir="${1%"$name"} &&
CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;
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?
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.)
In article <[email protected]>,
Helmut Waitzmann <[email protected]> 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.
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.
[email protected] (Kenny McCormack):
[...]
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.
[email protected] (Kenny McCormack):
In article <[email protected]>,
Helmut Waitzmann <[email protected]> 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.
find . -name 'my_script.sh' -exec sh -c -- \^
'name="${1##*/}" && dir="${1%"$name"} &&
CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;
On Saturday, February 11, 2023 at 5:00:27 AM UTC+8, Helmut Waitzmann wrote:
Harry <[email protected]>:
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 -- \Will this subshell-based technique suffer performance bottlenecks under heavy usage?
'name="${1##*/}" && dir="${1%"$name"} &&
CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;
Regards,
Zhao
Harry <[email protected]>:
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 \{\} \;
On Monday, February 13, 2023 at 7:28:54 AM UTC+8, [email protected] wrote:
On Saturday, February 11, 2023 at 5:00:27 AM UTC+8, Helmut Waitzmann wrote: >> > Harry <[email protected]>:
I have my_script.sh in multiple directories.find . -name 'my_script.sh' -exec sh -c -- \
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 ) \;
'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.
On Saturday, February 11, 2023 at 5:00:27 AM UTC+8,
Helmut Waitzmann wrote:
Harry <[email protected]>:
I have my_script.sh in multiple directories.find . -name 'my_script.sh' -exec sh -c -- \
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 ) \; >>>
'name="${1##*/}" && dir="${1%"$name"} &&
CDPATH= cd -- "${dir:=./}" && sh ./"$name"' sh \{\} \;
Will this subshell-based technique suffer performance
bottlenecks under heavy usage?
There are fewer "exec"s as before but not fewer "fork"s.Also, there is one process more than before running, which might
Will this subshell-based technique suffer performance
bottlenecks under heavy usage?
On Monday, February 13, 2023 at 7:28:54 AM UTC+8,
[email protected] wrote:
On Saturday, February 11, 2023 at 5:00:27 AM UTC+8,
Helmut Waitzmann wrote:
Harry <[email protected]>:
I have my_script.sh in multiple directories.find . -name 'my_script.sh' -exec sh -c -- \
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 ) \; >>>>
'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.
"[email protected]" <[email protected]>:
On Monday, February 13, 2023 at 7:28:54 AM UTC+8,
[email protected] wrote:
On Saturday, February 11, 2023 at 5:00:27 AM UTC+8,
Helmut Waitzmann wrote:
Harry <[email protected]>:
I have my_script.sh in multiple directories.find . -name 'my_script.sh' -exec sh -c -- \
How could I find it, then change directory there, and execute
it, one by one?
'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.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 157:17:32 |
| Calls: | 12,093 |
| Calls today: | 1 |
| Files: | 15,000 |
| Messages: | 6,517,750 |