I am trying to run a script where inside I run another script, after it has run I want to retrieve the exit code and do something with it but I only get 0
every time.
I also tried different approaches with sourcing it or wrapping the command in a function and had also given both execution rights with chmod -x
.
a.sh
#!/usr/bin/env bash
mkdir data
mkdir -p data/test
b.sh
#!/usr/bin/env bash
set -x
(bash a.sh) &
pid=$!
wait $pid
exitCode=$?
echo $pid
echo $exitCode
Result with bash b.sh
:
+ pid=7399
+ wait 7399
+ bash a.sh
mkdir: data: File exists
+ exitCode=0
+ echo 7399
7399
+ echo 0
0
Result with bash a.sh
:
mkdir: data: File exists (no exit code)
Result with mkdir data
:
mkdir: data: File exists (exit code 1)
I know I can do mkdir -p data
but this is just a test to get any exit code that I can work with later in my script.
Bash version: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Solved it by using: set -e
Reference: https://ss64.com/bash/set.html
This enables immediately exiting when something went wrong. In my case the second command returned 0 and that's why I got 0 every time.
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.