1

I am trying to return a value from one script to another. However, in the child script there are multiple echos, so am not sure how to retrieve a specific one in the parent scrip as if I try to do return_val = $(./script.sh) then return_val will have multiple arguments. Any solution here?

script 1:

status=$(script2.sh)
if [ $status == "hi" ]; then
echo "success"
fi

script 2:

echo "blah"
status="hi"
echo $status
4
  • Please add your or pseudo scrips reflecting your case. Commented Sep 16, 2022 at 16:55
  • Just uploaded sudo code, as there are 2+ echo values in second script i am unable to capture the status value properly. Commented Sep 16, 2022 at 17:03
  • 1
    Should some of the excess output from script2.sh be written to standard error instead of standard output? Would it be possible to use the exit status of script2.sh instead of reading its standard output at all? Commented Sep 16, 2022 at 17:28
  • 1
    ./script2 | grep -Fq 'hi' && echo success , grep has more options/flag you might want to check it out. Commented Sep 16, 2022 at 17:42

3 Answers 3

1

Solution 1) for this specific case, you could get the last line that was printed by the script 2, using the tail -1 command. Like this:

script1.sh

#!/bin/bash

status=$( ./script2.sh | tail -1 )
if [ $status == "hi" ]; then
  echo "success"
fi

script2.sh

#!/bin/bash

echo "blah"
status="hi"
echo $status

The restriction is that it will only work for the cases where you need to check the last string printed by the called script.

Solution 2) If the previous solution doesn't apply for your case, you could also use an identifier and prefix the specific string that you want to check with that. Like you can see below:

script1.sh

#!/bin/bash

status=$( ./script2.sh | grep "^IDENTIFIER: " | cut -d':' -f 2 )
if [ $status == "hi" ]; then
  echo "success"
fi

script2.sh

#!/bin/bash

echo "blah"
status="hi"
echo "IDENTIFIER: $status"

The grep "^IDENTIFIER: " command will filter the strings from the called script, and the cut -d':' -f 2 will split the "IDENTIFIER: hi" string and get the second field, separated by the ':' character.

Sign up to request clarification or add additional context in comments.

Comments

0

You might capture the output of script2 into a bash array and access the element in the array you are interested in. Contents of script2:

#!/bin/bash

echo "blah" 
status="hi" 
echo $status

Contents of script1:

#!/bin/bash

output_arr=( $(./script2) ) 
if [[ "${output_arr[1]}" == "hi" ]]; then 
    echo "success" 
fi

Output:

$ ./script1 
success

Comments

0

Script1:-

#!/bin/sh

cat > ed1 <<EOF
1p
q
EOF

next () {
[[ -z $(ed -s status < ed1 | grep "hi") ]] && main
[[ -n $(ed -s status < ed1 | grep "hi") ]] && end
}

main () {
sleep 1
next
}

end () {
echo $(ed -s status < ed1)
exit 0
} 

Script2:-

#!/bin/sh 

echo "blah"
echo "hi" >> status

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.