954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Calling a shell script from another script in a loop

Hi Guys,

how to call one shell script from another script inside a loop ?

any suggestion most welcome.

Prakash

Prakash_8111
Newbie Poster
21 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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 $?
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

It is bash shell .
The loop will run as the number of subdirs in the current running dirs.

Prakash_8111
Newbie Poster
21 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

what I've given should solve the problem..
If you have more probs, post your code

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

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


main1.sh:

#!/bin/bash
x="abc123"
echo "[Main1] Value of x: ${x}"
echo "[Main1] Calling sub"
./sub.sh
echo "[Main1] Value of x: ${x}"


main2.sh:

#!/bin/bash
x="abc123"
echo "[Main2] Value of x: ${x}"
echo "[Main2] Calling sub"
. sub.sh
echo "[Main2] Value of x: ${x}"


main3.sh:

#!/bin/bash
x="abc123"
echo "[Main2] Value of x: ${x}"
echo "[Main2] Calling sub"
source sub.sh
echo "[Main2] Value of x: ${x}"


sub.sh:

#!/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.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

Thanks a lot all, its working now

Prakash_8111
Newbie Poster
21 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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!

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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.

nikita.
Light Poster
35 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You