Regarding Redirecting a command's default error message in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regarding Redirecting a command's default error message in script
# 1  
Old 01-27-2011
Question Regarding Redirecting a command's default error message in script

Hi,

I just need a small help. I have this simple script to check a huge list of IDs if they exist or not on a server. Before running I have tested it with only 5-6 IDs. Script works fine.
But what I need it should not display the message of command's error message. That means if suppose an ID in the file "list" does not exist on server then it should display only "$i Does not exist". Can anyone please let me know how can I redirect the command's default message to /dev/null so that I can only see my error message?
Code:
for i in `cat list`
do
  id $i > /dev/null
  if [ `echo $?` -eq 0 ]; then
    echo "ID $i exists"
  elif [ `echo $?` -eq 1 ]; then
    echo "$i Does not exist" 
  else
    echo "Error"
  fi
done

I just want to know that. Suppose I have only 2 IDs on in the file "list" isctm91 & iscts30. When I ran the above script it displays messages like this,
Code:
$>sh chk.sh

User not found in /etc/passwd file    # <== Now this message is the default error message
                                      # through the command "id $i". I don't want to see this on terminal.
isctm91 Does not exist                # <== I just want to see this message.
User not found in /etc/passwd file    # <== this message don't want to see on terminal
iscts30 Does not exist                # <==I just wzant to see this message.

OS I am working is AIX.
Please let me know.

Last edited by Scott; 01-27-2011 at 02:00 PM.. Reason: Added code tags, indentation
# 2  
Old 01-27-2011
Some changes.
Redirect both STDOUT (1) and STDERR (2).
Change the "for" into a "while" because "for" can break with a large number of arguments.
Save the value of $? immediately after executing the command.
Simplify the if test.

Code:
cat list | while read i
do
    id $i 1>/dev/null 2>/dev/null ; ERROR=$?
    if [ ${ERROR} -eq 0 ]
    then
         echo "ID $i exists"
    else
         echo "$i Does not exist" 
    fi
done

This User Gave Thanks to methyl For This Post:
# 3  
Old 01-29-2011
MySQL Thank you Methyl. It worked well. This has really helped me. Thanks again.

Quote:
Originally Posted by methyl
Some changes.
Redirect both STDOUT (1) and STDERR (2).
Change the "for" into a "while" because "for" can break with a large number of arguments.
Save the value of $? immediately after executing the command.
Simplify the if test.

Code:
cat list | while read i
do
    id $i 1>/dev/null 2>/dev/null ; ERROR=$?
    if [ ${ERROR} -eq 0 ]
    then
         echo "ID $i exists"
    else
         echo "$i Does not exist" 
    fi
done

# 4  
Old 01-29-2011
You can also write it like this:
Code:
while read i
do
    if id $i >/dev/null 2>&1
    then
         echo "ID $i exists"
    else
         echo "$i Does not exist" 
    fi
done < list

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script error message

I have this UNIX script code with a query to export sql table in Oracle and export to csv file. The code gets the data correctly. However, when I run the script second time, I got the error message "not spooling currently" and shows the older data in csv file. When I delete the csv file and run... (5 Replies)
Discussion started by: Hope
5 Replies

2. UNIX for Beginners Questions & Answers

UNIX script error message

Greeting!! I wrote the below script to e-mail me only file names in a specific directory when a file is delayed for some time in a that directory. I am getting unexpected eof error message. I don't want any email if the folder is blank.(if the condition is not met) I am not getting the email at... (4 Replies)
Discussion started by: Hope
4 Replies

3. Shell Programming and Scripting

How to deliver an error message from script?

Hi I have a script that pick up a file on another server, and place it on a solaris server, but I came across the following error: mget HLR01_21092014? 200 Port command successful 150 Opening data channel for file transfer. HLR01_21092014: No space left on device 426 Connection closed;... (18 Replies)
Discussion started by: fretagi
18 Replies

4. Shell Programming and Scripting

Redirecting command output to a file in a shell script

Hello All, I have some unique requirement. I have written a very lengthy script which calls number of resource script to execute a particular task. What I want is output of each command(called from main script and resource scripts) should go to a... (3 Replies)
Discussion started by: anand.shah
3 Replies

5. UNIX for Dummies Questions & Answers

Error message with tail command

Hello, I wrote a script and part of the script, I have a validation to check if the file has <EOF> on the last line of the file. If it does not have a <EOF>, then a message has to be written to a log file. the code snippet shown below works fine, but it writes the below message if the... (1 Reply)
Discussion started by: Vijay81
1 Replies

6. Shell Programming and Scripting

Self referencing script error message

Hello again all. I have a user editable script that I'd like to have point out the user error to. Problem is I'm having troubles getting an echoed error message to give me the line. Here's what I'm trying to do. grep -n $loc /this/script.sh where '$loc' is the argument passed to the script.... (9 Replies)
Discussion started by: DC Slick
9 Replies

7. Shell Programming and Scripting

Is there a way to tee stderr from a command that's redirecting error to a file?

I'm not a complete novice at unix but I'm not all that advanced either. I'm hoping that someone with a little more knowledge than myself has the answer I'm looking for. I'm writing a wrapper script that will be passed user commands from the cron... Ex: ./mywrapper.sh "/usr/bin/ps -ef |... (1 Reply)
Discussion started by: sumgi
1 Replies

8. Shell Programming and Scripting

How to capture actual error message when a command fails to execute

I want to capture actual error message in case the commands I use in my shell script fails. For eg: ls -l abc.txt 2>>errorlog.txt In this case I understand the error message is written to the errorlog.txt and I assume its bacause the return code from the command ls -l abc might return 2 if... (3 Replies)
Discussion started by: prathima
3 Replies

9. Shell Programming and Scripting

Redirecting STDERR message to STDOUT & file at same time

Friends I have to redirect STDERR messages both to screen and also capture the same in a file. 2 > &1 | tee file works but it also displays the non error messages to file, while i only need error messages. Can anyone help?? (10 Replies)
Discussion started by: vikashtulsiyan
10 Replies

10. Shell Programming and Scripting

Help regarding Error message: A test command parameter is not valid

Hi I am getting few messages when trying to run my script from the following lines in the script if test then // SomeCode fi The messages are as follows: testing.sh: OBLIGOR_GROUP_ID: 0403-012 A test command parameter is not valid. testing.sh:... (5 Replies)
Discussion started by: skyineyes
5 Replies
Login or Register to Ask a Question