On Fri, Sep 06, 2024 at 12:25:11 +0200, Hans wrote:
> 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."

> 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. 

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.

Reply via email to