0

I have created this script in order to find exit codes for some arguments I need to test. Now I need to make a second script that calls this script to check for exit codes. However the 2nd script needs to read the exit code from this script and then assign text to it.

example:

passing /etc/resolve.conf then using echo $? returns 0

now i want something like found file to echo.

This is the first script to find exit codes

#/bin/bash
shopt -s -o nounset

if [ $# != 1 ]; then
    exit 2
fi

if [[ ${1:0:1} = /* ]]; then
    :
else
    exit 3
fi

if [ -f ${1} ]; then
    exit 0
else
    exit 1
fi

This is where I am at with my second script. After ./script1.sh is called, I don't understand how to set the exit code 0 to = text.

#/bin/bash
shopt -s -o nounset
./script1.sh

I know it would be easier to include echos in script1 like this example but in this case they need to be read from a second file.

if [ -f ${1} ]; then
    echo "file found"
    exit 0
else
    echo "file not found"
    exit 1
fi
0

You're calling script1.sh as:

./script1.sh

After the call, the exit code from the script will be in $?.

But, you're calling the script without any arguments, and the head of the script has the below test for the number of arguments, exiting with error code 2 unless there's exactly one. This matches the behaviour you mentioned seeing in the comments.

if [ $# != 1 ]; then
    exit 2
fi

You probably want to call script1.sh with some argument. If you want to pass the arguments the main script received, use either

./script1.sh "$@"   # pass all arguments

or

./script1.sh "$1"   # pass the first argument

(though the latter will pass an empty string even if there was no argument given to the main script.)


As minor points, you should quote the variable expansions, i.e. "$#" instead of $#, and "${1}" or "$1" instead of ${1}. Otherwise the script will fail with filenames containing whitespace, and even $# could go wrong if IFS is set to some insane value when the script starts.

Also, instead of

if something; then
    :
else
    exit 3
fi

you can write

if ! something; then
    exit 3
fi
4
  • So in order to get my correct exit codes I would need to pass the argument I'm typing into script2 into script1 then my $? results would match my exit codes in script1? – dylanw Oct 10 '20 at 17:11
  • @dylanw, yes, command line arguments don't get automatically "inherited" to other scripts, commands or functions – ilkkachu Oct 10 '20 at 17:15
  • @ikkachu, now I should be able to just write something like if [ {$result} = 0 ] then echo "found filename" fi to set the exit codes = to whatever text I want? – dylanw Oct 10 '20 at 17:34
  • @dylanw, yeah, res=$?; if [ "$res" = 0 ]; then echo 'return value was zero'; fi – ilkkachu Oct 10 '20 at 18:22
0

Assuming this is your second script:

#/bin/bash
shopt -s -o nounset
./script1.sh
result=$?
echo $result

the reason I assign it to a variable is that I store for future processing - if you called another program $? would be related to a different program

that $? is whatever that previous script returned

1
  • When I try running this as my second script it always returns a status code of 2 for every argument passed. but if I echo $? at the terminal it will then show the correct status code from ./script1 – dylanw Oct 10 '20 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.