[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to grep error messages



In <[🔎] 20090628094255.GA12904@freenet.de>, Michelle Konzack wrote:
>Am 2009-06-28 11:39:55, schrieb Soren Orel:
>> I can /dev/null the error messages like:
>>
>> cd "$1" 2> /dev/null
>>
>> e.g.: I get error If "$1" has spaces in it
>>
>> Ok, but how can I grep the error message? I tried:
>>
>> if cd "$1" 2> grep -i "No such file or directory"; then echo
>> "badbadbad"; exit; fi
>>
>> But it doesn't work :S
>
>You need to redirect it to STDOUT with:
>
>    if [ $(cd "$1" 2>&1 |grep -i "No such file or directory" ]
>        then echo "badbadbad" ; exit
>    fi
>
>But I would prefer something like:
>
>    if [ $(cd "$1") -ne 0 ]

This compares the output of the "cd" command against "0", numerically.  
That's probably not what you want.

You probably want to compare the exit code of the "cd" command against "0", 
numerically.  Something like:
cd "$1"
if [ $? -ne 0 ]; then ... fi

or simply:
if ! cd "$1"; then ... fi
-- 
Boyd Stephen Smith Jr.           	 ,= ,-_-. =.
bss@iguanasuicide.net            	((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy 	 `-'(. .)`-'
http://iguanasuicide.net/        	     \_/

Attachment: signature.asc
Description: This is a digitally signed message part.


Reply to: