• OT: Question to shell script

    From Hans@21:1/5 to All on Fri Sep 6 12:30:01 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andy Smith@21:1/5 to Hans on Fri Sep 6 13:20:01 2024
    Hi,

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

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Nicolas George@21:1/5 to All on Fri Sep 6 13:40:01 2024
    Andy Smith (12024-09-06):
    cd "$(dirname "$0")"

    … || exit

    Regards,

    --
    Nicolas George

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to Hans on Fri Sep 6 13:30:01 2024
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to Andy Smith on Fri Sep 6 13:40:01 2024
    On Fri, Sep 06, 2024 at 11:10:16 +0000, Andy Smith wrote:
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andy Smith@21:1/5 to Greg Wooledge on Fri Sep 6 13:50:02 2024
    Hi,

    On Fri, Sep 06, 2024 at 07:32:41AM -0400, Greg Wooledge wrote:
    On Fri, Sep 06, 2024 at 11:10:16 +0000, Andy Smith wrote:
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Hans@21:1/5 to All on Fri Sep 6 20:40:01 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anssi Saari@21:1/5 to Hans on Fri Sep 6 22:30:01 2024
    Hans <hans.ullrich@loop.de> writes:

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

    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.

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)