简体   繁体   中英

Run bash script as if I it was executed from a different directory

Maybe the title is a bit "stupid" but I do not know how to express my question and how to search for the question also, even if it is something very simple.

I have a set of scripts that produce a set of reports in the folder they are executed by. For example I have the script "my_script.sh" in the folder /a/folder/ and in this folder a set of output is stored. Since I have a lot of experiments that I want to let them run for the whole week I was thinking of creating a bash script that will call all the other scripts. But the output will be stored in the folder that the global script is present.

For example:

 /global/folder/global_script.sh

---> All the output is stored in this folder.

The global_script.sh may contain something like this:

/experiments/exp1/script1.sh >report1.txt
/experiments/exp1/script2.sh >report2.txt
/experiments/exp1/script2.sh >report3.txt

And I want the output of the bash scripts to be in their folder and not in the global folder.

Currently I am doing this manually navigating to the folder and executing the script.

(Ok I can change the code and use absolute paths! but is any better way to do that? )

you could change the working directory before you execute each script, or redirect the output to the directory you want:

cd /experiments/exp1/
sh /experiments/exp1/script1.sh >report1.txt

or

sh /experiments/exp1/script1.sh > /experiments/exp1/report1.txt

What's wrong with simply changing directory?

cd /experiments/exp1
./script1.sh >report1.txt
./script2.sh >report2.txt
./script2.sh >report3.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM