Redirecting errors of test command


 
Thread Tools Search this Thread
# 1  
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 Smilie):

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 03:34 PM..
# 2  
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

# 3  
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 08:22 AM..
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Computers
Difficulty: Easy
Linus Torvalds created Linux and Git.
True or False?

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting stdion, stdout within an AT command

Hello, I'm strugling with some redirecting and all help is apreciated. The following program is working as expected, but the result of the AT command doesn't go to any file. Thanks in advance for the help. #!/bin/bash modem=/dev/ttyUSB1 file=/root/imsi.txt # print error to stderr and exit... (4 Replies)
Discussion started by: cleitao
4 Replies

2. Shell Programming and Scripting

Need help redirecting output to a file including errors

Need help redirecting output to a file including errors if any,I have 2 script namely push.ksh and run.ksh, I'm scp'ing push.ksh to another server and executing remotely via run.ksh, the script run.ksh runs locally but does not capture any errors in "servername.out" file (I tried testing various... (10 Replies)
Discussion started by: mbak
10 Replies

3. Shell Programming and Scripting

Redirecting command output as well as commands

I have a Bourne Shell script that is normally run as a background job and redirects it's output to a file internally (using exec >>); I use "set -x" to capture each command which provides me with a nice shell execution log if it all goes to pieces. I now also need to be able to also run this as... (4 Replies)
Discussion started by: AncientCoder
4 Replies

4. Shell Programming and Scripting

redirecting to stdout in betwen command

can anyone help me in making singleline command for Capital Letters are folders ,small letter are files X,Y,Z are subfolders of A as shown below A - X,Y,Z Folder X has three files a.txt,b.txt,c.txt similarly Y,Z. as shown below X- a.txt,b.txt,c.txt Y- a.txt,b.txt,c.txt Z-... (4 Replies)
Discussion started by: phoenix_nebula
4 Replies

5. Shell Programming and Scripting

Redirecting stdin/stdout to/from command from/to string

Hi, I am working on a project where I have to generate and execute nasm code on-the-fly. I generate the code in a file program.asm and then execute it.This output is to stdout which i redirect to an output file which i read back to compare results: system("nasm -f elf program.asm >... (5 Replies)
Discussion started by: doc_cypher
5 Replies

6. Shell Programming and Scripting

Redirecting output of a command to a file

Hi We are having a requirement where one shell script, say a.sh (which uses Java and connects to Oracle database using JDBC) keeps on running everytime. I created a wrapper (to check whether a.sh is running and if not then to start it) and scheduled it in the crontab. Now all the output from... (3 Replies)
Discussion started by: ankitgoel
3 Replies

7. Shell Programming and Scripting

problem redirecting output of command to variable

Hi. I'm a newbie in scripting and i have this problem: i want to use the 'fuser' command on a file to tell if it's being accessed (for my purposes: still being written). I want to save the output of the command and later compare with the 'not being used' result. the script: #!/bin/bash... (2 Replies)
Discussion started by: nunovc
2 Replies

8. UNIX for Dummies Questions & Answers

redirecting output, including errors

what's the proper syntax to redirect output, including all errors? ls -la > direct.list makes out put file direct.list but if i'm running a script and i want to include the errors, would i type something like: myscript.scr 2> out_list.txt or will that get the errors only? (1 Reply)
Discussion started by: kymberm
1 Replies

Featured Tech Videos