This is my scenario, I have a root folder called A, which contains folders B,C,D with n possibilities. Each one of those folders have a bash script. How can I get those bash scripts and run them using bash script.
A/B/run.sh
A/C/run.sh
A/D/run.sh
This is my scenario, I have a root folder called A, which contains folders B,C,D with n possibilities. Each one of those folders have a bash script. How can I get those bash scripts and run them using bash script.
A/B/run.sh
A/C/run.sh
A/D/run.sh
With find :
find . -name *.sh -type f -exec bash -c '[[ -x {} ]] || chmod u+x {}; {}' \;
For each *.sh found :
easy :)
eval "$(ls A/*/run.sh)"
ls will return a list of file with path to your scripts. (you could use find too)
the " " around the $() will make sure the result keeps new lines
eval will execute the returned lines as a script.
Mind you if there are spaces in the names of your script and stuff this can be a brittle solution. But if it looks like what you have shown, that should work fine.
this is output for an example:
~/ eval "$(ls */run.sh)"
I am running and my name is: one/run.sh
I am running and my name is: two/run.sh
the run.sh scripts are:
echo "I am running and my name is: $0"
for loop would be the most obvious way yo do it. This solution appears to be an implementation of the very first warning on the bash pitfalls list.