Discussion:
OT: Question to shell script
(too old to reply)
Hans
2024-09-06 10:30:01 UTC
Permalink
Dear list,

I am stuck with a little problem and know no one, whom I can ask. So I allow
me to ask here.

I have several directories, and in each directory there is a shell script,
which MUST be started within and from its path.

Now I want to edit a "master-shell-script", which I can start from anywhere
and which shall start each single shellscripts simultaniously, but within its
path.

The structure looks like this:

/directory-one/application_1/my-shell-1.sh
/directory-two/application_2/my-shell-2.sh
/directory-three/application_3/my-shell-3.sh

Of course, I could mae my master-shell-script so, that I first go into the
first one, execute, then cd to the second one, execute and last cd to the
third one, execute, but I suppose, there is an easier way.

Or does my idea not work at all?

Thanks fo a short advice.

Best

Hans
Andy Smith
2024-09-06 11:20:01 UTC
Permalink
Hi,
Post by Hans
I have several directories, and in each directory there is a shell script,
which MUST be started within and from its path.
Is there a reason not to just make these scripts cd to their own
directory so the caller doesn't have to care?

cd "$(dirname "$0")"

I mean, I don't know any other binary on my system that tells me to
run it from a specific directory, so introducing such a requirement
in my own tools would seems like poor user interface, as it is
surprising.

Thanks,
Andy
--
https://bitfolk.com/ -- No-nonsense VPS hosting
Nicolas George
2024-09-06 11:40:01 UTC
Permalink
Post by Andy Smith
cd "$(dirname "$0")"
… || exit

Regards,
--
Nicolas George
Greg Wooledge
2024-09-06 11:40:01 UTC
Permalink
Post by Andy Smith
Is there a reason not to just make these scripts cd to their own
directory so the caller doesn't have to care?
cd "$(dirname "$0")"
https://mywiki.wooledge.org/BashFAQ/028
Andy Smith
2024-09-06 11:50:02 UTC
Permalink
Hi,
Post by Greg Wooledge
Post by Andy Smith
Is there a reason not to just make these scripts cd to their own
directory so the caller doesn't have to care?
cd "$(dirname "$0")"
https://mywiki.wooledge.org/BashFAQ/028
Ah, got it - $0 is not reliably the location of the running script.

Thanks,
Andy
--
https://bitfolk.com/ -- No-nonsense VPS hosting
Hans
2024-09-06 18:40:01 UTC
Permalink
What I am exactly want to do:

I have 5 live-build directories. In each I am starting my own script, which is
setting variables and so on for the individual build and does some other
things (rennamin and copying the resulted ISO and so on).

As each build must be started within the live-build directory, I would have to
wait, if the first build is ready, then cd into the net live-buil and so on,
until all five are ready.

This is lasting much too long, so I would like, to run all 5 scripts in
parallel, and this should be started from a master script, which I can execute
from anywhere, i.e. from the home of root or /usr/sbin.

You certain have recognized: All these scripts must be run as root, but this
is another case and does not matter here.

If bash does not offer this option, I will find another way, but I hope,
someone more experienced with shellscripts or bash might have a quick
solution. If not, no problem, it does not harm anyone.

Best

Hans
Anssi Saari
2024-09-06 20:30:01 UTC
Permalink
Post by Hans
I have 5 live-build directories. In each I am starting my own script, which is
setting variables and so on for the individual build and does some other
things (rennamin and copying the resulted ISO and so on).
So you're asking how to do a script with 5 cd commands and 5 start
script commands? What exactly is the problem? If you append your script
starting command with a & symbol, it'll start in the background and your
script can proceed to the next cd command and next script startup
command and so on.
Post by Hans
If bash does not offer this option, I will find another way, but I hope,
someone more experienced with shellscripts or bash might have a quick
solution. If not, no problem, it does not harm anyone.
Perhaps it'd be easier for you to just open five terminals and manually
start one of the five scripts in each. Keep the terminals around and you
can easily rerun the scripts.

Perhaps a note here, if your scripts mostly copy files around, running
the scripts in parallel may not go much faster if your storage is
already saturated by one of them.

Greg Wooledge
2024-09-06 11:30:01 UTC
Permalink
Post by Hans
I have several directories, and in each directory there is a shell script,
which MUST be started within and from its path.
I'm not clear on what "within and from its path" means here, but let's
suppose you mean "I have to cd there first, and I also have to use the
full pathname of the script, rather than ./script."
Post by Hans
/directory-one/application_1/my-shell-1.sh
/directory-two/application_2/my-shell-2.sh
/directory-three/application_3/my-shell-3.sh
Of course, I could mae my master-shell-script so, that I first go into the
first one, execute, then cd to the second one, execute and last cd to the
third one, execute, but I suppose, there is an easier way.
You can populate an array with all of the script paths, then iterate
over it.


#!/bin/bash
scripts=(
/directory-one/application_1/my-shell-1.sh
/directory-two/application_2/my-shell-2.sh
/directory-three/application_3/my-shell-3.sh

# We can use blank lines and comments in here.
# /directory-four/application_4/my-shell-4.sh
)

for s in "${scripts[@]}"; do
dir=${s%/*}
cd "$dir" && "$s"
done


This particular script does *not* force each cd/execute to occur in a
subshell, because we don't actually have to return to our original
directory at any point, ever. It's totally fine to remain in the
first script directory until we cd to the second script directory.
If any of the cds fails, we simply skip that script and move on to the
next.
Loading...