Supress grep output but capture it in a variable
I'm trying to get the following line to work
WERRORS=`echo $VALPG | grep -q -s -o -m 1 '\<[0-9]* Errors'`
What I want is that the r开发者_JAVA百科esult of grep go into WERRORS variable but not echo in the terminal. So i use -q, but then WERRORS is empty
If grep sends any error messages, they go to the error output, which is not captured by the backticks. If you need this output in a variable (which is somewhat problematic, because it's often localized), redirect it using 2>&1:
WERRORS=`echo $VALPG | grep -s -o -m 1 '\<[0-9]* Errors' 2>&1`
WERRORS=`echo $VALPG | grep -s -o -m 1 '\<[0-9]* Errors'`
kent$ val=abcpc
kent$ a=$(echo $val|grep -o -m 1 -s 'pc')
kent$ echo $a
pc
精彩评论