How do I capture a variable but ignore standard error?

daworley
Contributor II

Hey all,

I'm working on a script to rename permissions on accounts moved from Open Directory to Active Directory.

As part of the checks (while loop logic) to see if the account exists I am querying AD for the GUID of the account:

/usr/bin/dscl /Active Directory/$domain/All Domains/ -read /Users/$user GeneratedUID | /usr/bin/awk '{print $NF;}')"

This works great for accounts that do exist in that domain, but if they don't the command fails. (I might have mistyped this from my script, but the logic works ;) )

How can I capture this information into a variable, and have sterr ignore the cases where the command populating the variable fails?

1 ACCEPTED SOLUTION

eyemyth
New Contributor III

Add ```
2> /dev/null
``` to redirect stderr to /dev/null

View solution in original post

2 REPLIES 2

eyemyth
New Contributor III

Add ```
2> /dev/null
``` to redirect stderr to /dev/null

bentoms
Release Candidate Programs Tester

Here's one i made earlier.

accountUniqueID=`dscl . -read /Users/$1 2>dev/null | ?grep UniqueID | cut -c 11-`