Hi Guys,
how to call one shell script from another script inside a loop ?
any suggestion most welcome.
Prakash
Hi Guys,
how to call one shell script from another script inside a loop ?
any suggestion most welcome.
Prakash
Which shell?
What's the loop break condition?
How to loop: http://www.freeos.com/guides/lsst/ch03sec06.html
How to call a script: It's just a command give full path.
E.g. a.sh wants to call b.sh then
#!/bin/bash
#a.sh
PATH_TO_B=/tmp/b.sh
echo Calling b.sh
$PATH_TO_B
echo $PATH_TO_B exited with $?
#if b.sh is not executable then:
/bin/bash $PATH_TO_B
echo $PATH_TO_B exited with $?
It is bash shell .
The loop will run as the number of subdirs in the current running dirs.
what I've given should solve the problem..
If you have more probs, post your code
Well it depends on how you want the scripts to interact with eachother. If you want to share environment variables that have not been exported then you would call the file with . /path/to/file.sh
or source /path/to/file.sh
. If you don't want to share the environment of the script you would simple /path/to/file.sh
Test environment
sk@svn2:/tmp/dani$ ls -al
total 24
drwxr-xr-x 2 sk sk 4096 2010-01-07 00:10 .
drwxrwxrwt 14 root root 4096 2010-01-07 00:02 ..
-rwxr-xr-x 1 sk sk 124 2010-01-07 00:09 main1.sh
-rwxr-xr-x 1 sk sk 124 2010-01-07 00:10 main2.sh
-rwxr-xr-x 1 sk sk 129 2010-01-07 00:10 main3.sh
-rwxr-xr-x 1 sk sk 97 2010-01-07 00:08 sub.sh
#!/bin/bash
x="abc123"
echo "[Main1] Value of x: ${x}"
echo "[Main1] Calling sub"
./sub.sh
echo "[Main1] Value of x: ${x}"
#!/bin/bash
x="abc123"
echo "[Main2] Value of x: ${x}"
echo "[Main2] Calling sub"
. sub.sh
echo "[Main2] Value of x: ${x}"
#!/bin/bash
x="abc123"
echo "[Main2] Value of x: ${x}"
echo "[Main2] Calling sub"
source sub.sh
echo "[Main2] Value of x: ${x}"
#!/bin/bash
echo "[Sub] Value of x: '${x}'"
x="sub value"
echo "[Sub] Setting Value to: '${x}'"
Output:
sk@svn2:/tmp/dani$ ./main1.sh
[Main1] Value of x: abc123
[Main1] Calling sub
[Sub] Value of x: ''
[Sub] Setting Value to: 'sub value'
[Main1] Value of x: abc123
sk@svn2:/tmp/dani$ ./main2.sh
[Main2] Value of x: abc123
[Main2] Calling sub
[Sub] Value of x: 'abc123'
[Sub] Setting Value to: 'sub value'
[Main2] Value of x: sub value
sk@svn2:/tmp/dani$ ./main3.sh
[Main2] Value of x: abc123
[Main2] Calling sub
[Sub] Value of x: 'abc123'
[Sub] Setting Value to: 'sub value'
[Main2] Value of x: sub value
Notice how using . or source passed the value set locally in the calling script, where as ./sub.sh
did not? Also the value of $x was modified in sub.sh and when you source'd the scripts then the modified value was available in the calling script.
Thanks a lot all, its working now
I'm glad you got it working
Please mark this thread as solved if you have found an answer to your question and good luck!
I'm glad you got it working
Please mark this thread as solved if you have found an answer to your question and good luck!
Can you please tell me how to store the result of the script in a variable.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.