Hello.
There is an script1.sh which executes other .sh scripts in this way:
./dir1/dir2/script2.sh
./dir1/dir3/script3.sh
This was for Ubuntu or such linux systems where you don't have to explicitely do: sh scrip.sh....
So, I tried with
sh ./dir1/dir2/script2.sh
sh ./dir1/dir3/script3.sh
but this doesn't work
I can go with
cd ./dir1/dir2/
script2.sh
cd ..
cd..
cd ./dir1/dir3/
sh script3.sh
but, obviously this is bad way to do it.
Is there a better way, in order not to go with cd?
executing .sh scripts inside another .sh script
the script files will run the interpreter in the SHELL= variable unless otherwise specified.
sh is not Puppy's default, bash is
the first line of the .sh file should look like this
#!/bin/sh
the file itself needs to have, or should have the executable bit set, typically I do it like this
chmod 755 shell.sh
If you put the file in a directory in the PATH= statement, all you need to is say its name to run it
echo $PATH shows the directories which are set. It might have this directory /root/bin, if so, that would be a good place to put your toys
Put script2 and script3 in the path and all script1 needs to say is:
If you don't have script1 and script2 in the path, then use full path to each, like this
You don't need the .sh extension. Using it will primarily make it easier for you to recognize which type of script it is.
I've never actually wrote a script using sh or tested any of my advice, but I wanted to make sure you got some kind of help, and I think my advice is accurate, if not it will make for critique from someone and I can learn more.
~
sh is not Puppy's default, bash is
the first line of the .sh file should look like this
#!/bin/sh
the file itself needs to have, or should have the executable bit set, typically I do it like this
chmod 755 shell.sh
If you put the file in a directory in the PATH= statement, all you need to is say its name to run it
echo $PATH shows the directories which are set. It might have this directory /root/bin, if so, that would be a good place to put your toys
Do not ./dir1, the ./ makes it a relative path and it will only run if the script1.sh is ran from the parent directorySo, I tried with
sh ./dir1/dir2/script2.sh
sh ./dir1/dir3/script3.sh
but this doesn't work
Put script2 and script3 in the path and all script1 needs to say is:
Code: Select all
#!/bin/sh
script2.sh
script3.sh
Code: Select all
#!/bin/sh
/truepath_to/script2.sh
/truepath_to/script3.sh
I've never actually wrote a script using sh or tested any of my advice, but I wanted to make sure you got some kind of help, and I think my advice is accurate, if not it will make for critique from someone and I can learn more.
~
- L18L
- Posts: 3479
- Joined: Sat 19 Jun 2010, 18:56
- Location: www.eussenheim.de/
Re: executing .sh scripts inside another .sh script
I have been trying too.tatamata wrote:...
So, I tried with
sh ./dir1/dir2/script2.sh
sh ./dir1/dir3/script3.sh
but this doesn't work...
... and it works
my console wrote:# pwd
/root/my-applications/bin
# ls dir1
dir2 dir3
# cat dir1/dir2/script2.sh
#!/bin/sh
echo hi this is $0
# cat script1.sh
#!/bin/sh
./dir1/dir2/script2.sh
./dir1/dir3/script3.sh
#
# script1.sh
hi this is ./dir1/dir2/script2.sh
hi this is ./dir1/dir3/script3.sh
#