0

Hello I am trying to store an OS X command in a variable and I am having problems doing so. Here is my code:

#! /bin/bash
Output=$(dscl . -read /Users/root AuthenticationAuthority)
Check="No such key: AuthenticationAuthority"  

if [ "$Output" = "$Check" ]
    then
       echo "OK"
      else
    echo "FALSE"
fi

I have done this before with commands such as "defaults read...." and it works fine but the dscl . -read will not store the output in the variable. Any ideas?

3
  • Checked whether its executing? Commented Mar 12, 2015 at 17:19
  • When you echo $Output, what happens? Commented Mar 12, 2015 at 17:19
  • When you run that script do you see the output on your screen? (Is it returning that error to standard error instead of standard output?) Commented Mar 12, 2015 at 17:19

2 Answers 2

7

On failure, the dscl command, as well as all standard shell commands, outputs the error message on stderr, whereas $(...) only captures stdout.

You have to merge the two streams first:

Output=$(dscl . -read /Users/root AuthenticationAuthority 2>&1)
Sign up to request clarification or add additional context in comments.

Comments

0

When the dscl command succeeds, its output goes to stdout, which is captured by the command substitution.

When there is an error, the message is printed to stderr instead.

To capture either stdout or stderr, you can redirect stderr in the command to go to stdout:

Output=$(dscl . -read /Users/root AuthenticationAuthority 2>&1)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.