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
since you are using bash and iterating over subdirectories, treat the second script just like any other command and pass the subdirectory path as an argument. As noted, use the full path so you do not depend on the current directory; anchoring to the caller script directory is a reliable pattern. If you need the callee to modify variables in the caller, source it (as explained). Otherwise, execute it as a separate process and check its exit status per iteration.
#!/usr/bin/env bash
set -euo pipefail
script_dir="$(cd -- "$(dirname -- "$0")" && pwd)"
callee="$script_dir/other.sh" # script to call
# Loop over immediate subdirectories (robust to spaces/newlines)
while IFS= read -r -d '' d; do
printf 'Processing: %s\n' "$d"
if out=$("$callee" "$d" 2>&1); then
printf 'OK: %s\n' "$out"
else
rc=$?
printf 'FAILED (exit %d): %s\n' "$rc" "$out"
# decide: continue, break, or collect failures
fi
done < <(find . -mindepth 1 -maxdepth 1 -type d -print0) to store the result of a script in a variable, capture stdout with command substitution and also record the exit code if you care about success/failure:
result="$(/path/to/script.sh arg1 arg2)" # stdout only
status=$? # exit code of script.sh
# For stdout+stderr together: result="$(/path/to/script.sh 2>&1)" Tips:
source only when you intentionally want the callee to affect the caller (e.g., set functions, aliases, or variables).cd, it will change the caller’s working directory; executed scripts will not.Jump to Post— thekashyap 193Which 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
Jump to Post— sknake 1,622Well 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.shorsource /path/to/file.sh. If you don't want to share the environment of the …
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.