Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-13-2012
Registered User
 
Join Date: Apr 2012
Location: Austria / AT
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
Redirecting errors of test command

Hello, Unix-Board!

Normally, I would hide error messages in a shell script with

Code:
command 2> path

but test also prints out errors if I do that.

The code of the script is (please don't tell me about bad coding ):


Code:
echo "Enter a number here"
read number
test $number -ge 0 && echo "Thank you" && sleep 1 && exit
test $number -le 0 && echo "Thank you" && sleep 1 && exit || echo "This is not a number"

With this, I test if somebody entered a letter. Test does that well but it also prints out "illegal number [ letter i typed ]"
and then displays the expected message.

Where do I have to place the 2> to supress this?

Thanks in advance,

intelinside

Last edited by fpmurphy; 04-13-2012 at 02:34 PM..
Sponsored Links
    #2  
Old 04-13-2012
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
Sorry, but you cannot redirect a syntax error. A numeric test expects a number. Also beware that you script had something after exit which was not going to execute.
First eliminate the non-numeric response. In this technique we try deleting all the numbers in the response and see if there is anything left (which would mean the answer was non-numeric).
( if -z is the same as test -z but modern convention).


Code:
echo "Enter a number here"
read number
if [ -z "`echo $number | tr -d [0-9]`" ]
then
            echo "Thank you"
            sleep 1
else
            echo "This is not a number"
            sleep 1
            exit
fi
# Continue processing $number here

# The next line is the last line
exit

Sponsored Links
    #3  
Old 04-14-2012
Registered User
 
Join Date: Apr 2012
Location: Austria / AT
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
Okay, I read some man pages about the

Code:
tr -d

command you use in your script and I think it is
is exactly what I need. Thank you!

Last edited by intelinside; 04-14-2012 at 07:22 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help redirecting output to a file including errors mbak Shell Programming and Scripting 10 10-09-2011 04:44 PM
Redirecting command output as well as commands AncientCoder Shell Programming and Scripting 4 07-29-2010 10:51 AM
redirecting to stdout in betwen command phoenix_nebula Shell Programming and Scripting 4 03-16-2010 01:09 AM
Redirecting output of a command to a file ankitgoel Shell Programming and Scripting 3 04-13-2009 04:45 PM
redirecting output, including errors kymberm UNIX for Dummies Questions & Answers 1 09-18-2002 12:59 PM



All times are GMT -4. The time now is 04:37 PM.