6

Can someone help explain the following:

If I type:

a=`ls -l`

Then the output of the ls command is saved in the variable a

but if I try:

a=`sh ./somefile`

The result is outputed to the shell (stdout) rather than the variable a

What I expected was the result operation of the shell trying to execute a scrip 'somefile' to be stored in the variable.

Please point out what is wrong with my understanding and a possible way to do this.

Thanks.

EDIT:

Just to clarify, the script 'somefile' may or may not exist. If it exsists then I want the output of the script to be stored in 'a'. If not, I want the error message "no such file or dir" stored in 'a'

3 Answers 3

15

I think because the shell probably attaches itself to /dev/tty but I may be wrong. Why wouldn't you just set execute permissions on the script and use:

a=`./somefile`

If you want to capture stderr and stdout to a, just use:

a=`./somefile 2>&1`

To check file is executable first:

if [[ -x ./somefile ]] ; then
    a=$(./somefile 2>&1)
else
    a="Couldn't find the darned thing."
fi

and you'll notice I'm switching to the $() method instead of backticks. I prefer $() since you can nest them (e.g., "a=$(expr 1 + $(expr 2 + 3))").

5
  • its not about the permissions... i want the either error messages or return values to be stored in the variable.
    – RomanM
    Commented Feb 6, 2009 at 7:37
  • What I'm saying is that you don't need to run sh to run the script (and sh will impose its own firewall on the output if you do). Just run the script directly and, for that, you need to 'chmod +x' it.
    – paxdiablo
    Commented Feb 6, 2009 at 7:41
  • Try the following: "chmod u+x somefile; a=./somefile" and see what a gets set to then.
    – paxdiablo
    Commented Feb 6, 2009 at 7:44
  • but for my case somefile my not exsists, how to avoid seeing the message "no such file or dir" in the shell but rather have it stored in the variable
    – RomanM
    Commented Feb 6, 2009 at 8:11
  • "if [[ -x ./somefile ]]" is what you're looking for.
    – paxdiablo
    Commented Feb 6, 2009 at 8:21
5

You can try the new and improved way of doing command substitution, use $() instead of backticks.

a=$(sh ./somefile)

If it still doesn't work, check if somefile is not actually stderr'ing.

1
  • probably it can't actually find somefile ;)
    – Bogdan
    Commented Feb 6, 2009 at 17:53
2

You are correct, the stdout of ./somefile is stored in the variable a. However, I assume somefile outputs to stderr. You can redirect that with 2>&1 directly after ./somefile.

2
  • oh i think i see, so the output in the case 'somefile' doesnt exsists stored in a is nothing but the script by default redirects to stderr which is why i see the error message on the screen as opposed to stored in the variable ?
    – RomanM
    Commented Feb 6, 2009 at 7:35
  • 1
    No the stdout of sh is stored in a, sh is not passing the stdout of ./somefile back to it's caller.
    – paxdiablo
    Commented Feb 6, 2009 at 7:42

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.