Skip to main content
Answer

How do I capture a variable but ignore standard error?

  • October 11, 2012
  • 2 replies
  • 12 views

Forum|alt.badge.img+13

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?

Best answer by eyemyth

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

2 replies

Forum|alt.badge.img+6
  • Contributor
  • Answer
  • October 11, 2012

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


bentoms
Forum|alt.badge.img+35
  • Hall of Fame
  • October 11, 2012

Here's one i made earlier.

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