getting variables from another script

I have 2 scripts.The first script greps something and prints it

say a.sh
#!/bin/sh
ccc="grep something and cut something"
echo $ccc

When you run this it gives a value say "100"

I have b.sh and I have
#!/bin/sh
status=`/home/a.sh`
status_code=$?
echo status_code

When I run b.sh I dont get 100 but get some other number...do you know why?

because you're echo'ing $?, which is the return code of the previous command, not the result of a.sh

It's easier if we see the real script. This construct should capture the output from the called script.

/home/a.sh | read status

In most shells, the value of $status will be lost because each element of a pipeline is executed in a subshell.

Use:

status=$(/home/a.sh)    ## What's a script doing in /home?????
printf "Return code: %s\n" "$?"
printf "     Output: %s\n" "$status"

I didn't explain myself very well. I'm not trying to pass $status to the calling shell. I'm reading the output from the echo in a.sh within b.sh. Very similar to cfajohnson POSIX shell method.

#!/bin/sh
# This is a.sh
echo "100"

#!/bin/sh
# This is b.sh
/home/a.sh | read status
echo "This came from b.sh: ${status}"


/home/a.sh

This came from b.sh: 100

That will only work in ksh (and not even in pdksh).

Hi cfajohnson.

I've tried both our scripts in HP-UX 11.11 /bin/sh (a hard link to /usr/bin/sh) and both scripts work. Please try my construct in your shell and let me know if there is an issue.

Neither script works in Bourne shell (mine misbehaves, yours gives syntax errors). We both have omitted to check what the o/p is using.

There's no need to try it. It doesn't work in anything except a KornShell.

Mine will work in a Bourne shell if you change $( ... ) to ` ... `.

Since every system for at least 10 years has a POSIX shell, I assume that as a minimum, and post solutions that will work in a POSIX shell.

On occasion, I use bash syntax (usually also works in ksh93), but I make it clear that that's what it is.