Handling standard error in condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling standard error in condition
# 1  
Old 12-23-2013
Handling standard error in condition

Testing this with KSH on RHEL

The bellow code works but i can't seem to handle the exit status of the unix command when it fails... i wanted to put something like >/dev/null 2>&1 to manage standard output and standard error but it changes my logic and the code doesn't work cause it doesn't return true when the ls works.

Mind that i know i could use the -f to test the file but in my production script it tests the return status of a service command. That is why i wanted to evaluate the $? of the unix command.

Maybe it is not the proper way to code this? I'm fully open to suggestions.

Its pretty simple. Test all the services and if you find one that is "true" then set the rcControlRule to 1 and if you find one exit the function with the rcControlRule to 1.

Code:
#!/bin/ksh

set -A services "toto" "titi" "tata" "/etc/redhat-release" "tete"

check_service () {
        i=0
        while [[ $i -le "${#services[@]}" ]] && [[ rcControlRule -ne 1 ]]; do
                if [ "$(ls "${services[$i]}")" ]; then
                        rcControlRule=1
                        echo "${services[$i]}"
                else
                        rcControlRule=0
                fi
        (( i+=1 ))
        done
        return ${rcControlRule}
}

echo "Start"

check_service

echo "${rcControlRule}"

echo Done

Code:
 ./test.ksh
Start
ls: cannot access toto: No such file or directory
ls: cannot access titi: No such file or directory
ls: cannot access tata: No such file or directory
/etc/redhat-release
1
Done

# 2  
Old 12-23-2013
One way to handle error try...

Code:
if ls *.type &>/dev/null; then 
    echo "yes files are there"
    ....................
    ....................
else 
    echo "no files are not there"
    ....................
    ....................
fi

# 3  
Old 12-23-2013
That is the logic i thought at first but doesn't work with the following code.

Code:
if ls ${services[$i]} &>/dev/null ; then

I get

Code:
 ls: cannot access toto: No such file or directory

And need to control-c to break.
# 4  
Old 12-23-2013
Quote:
Originally Posted by maverick72
That is the logic i thought at first but doesn't work with the following code.

Code:
if ls ${services[$i]} &>/dev/null ; then

I get

Code:
 ls: cannot access toto: No such file or directory

And need to control-c to break.
I tested like this see on Centos 6.5

Code:

# Working
$ if ls *.pdf &>/dev/null; then echo "yes"; else echo "no";fi
yes

$ if ls *.pdfs &>/dev/null; then echo "yes"; else echo "no";fi
no

# This does not work
$ if ls *.pdf >/dev/null; then echo "yes"; else echo "no";fi
yes

$ if ls *.pdfs >/dev/null; then echo "yes"; else echo "no";fi
ls: cannot access *.pdfs: No such file or directory
no

# 5  
Old 12-23-2013
Your code works on my system too (rhel 6.3) but doesn't seem to work with array elements. No clue why.

Code:
$ if ls /etc/redhat-release &>/dev/null; then echo "yes"; else echo "no";fi
yes
$ if ls toto  &>/dev/null; then echo "yes"; else echo "no";fi
no

EDIT: syntax
# 6  
Old 12-23-2013
Quote:
Originally Posted by maverick72
Your code works on my system too (rhel 6.3) but doesn't seem to work with array elements. No clue why.

Code:
$ if ls /etc/redhat-release &>/dev/null; then echo "yes"; else echo "no";fi
yes
$ if ls toto  &>/dev/null; then echo "yes"; else echo "no";fi
no

EDIT: syntax
One more test I did just now see

Code:
$ ls *.pdf -1
Customers of Old Prepaid Plans Migrated list_web.pdf
intercom hindi english web22.pdf
Introduce_prepaid_plan_Savinudi_Oct13_web7.pdf

$ arr=(pdf pdfs)
$ i=0; while [ ${#arr[*]} -gt $i ];do if ls *.${arr[$i]} &>/dev/null; then echo "yes"; else echo "no";fi;i=$((i+1));done
yes
no

it seems its not working with ksh I guess not sure.

--edit---

This will work for you

Code:
set -A services "toto" "titi" "tata" "/etc/redhat-release" "tete"
i=0
while [ ${#services[*]} -gt $i ];do
if [ $(ls "${services[$i]}" 2>/dev/null | wc -l) -gt 0 ]  ; then
    echo "mycode goes here"
else 
    echo "no...some command"
fi;
((i+=1))
done


Last edited by Akshay Hegde; 12-23-2013 at 07:35 AM..
# 7  
Old 12-23-2013
cmd line works on my system too .... in bash Smilie

when i put your line in a ksh shell script it doesn't work.

Code:
#!/bin/ksh

arg=(ksh bash)
i=0
while [ ${#arr[*]} -gt $i ]; do
        if ls "*.${arr[$i]}" &>/dev/null; then
                echo "yes"
        else
                echo "no"
        fi
        i=$((i+1))
done

See if that script works on your system (with pdf and pdfs).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error handling

Hello fellow UNIX gurus :) I have a problem regarding the script below: # Variables used in this shell. power=0 # Stores squared integer total=0 # Sum of all squared integers num=0 # Stores command line arguements # Provides error handling if command line... (5 Replies)
Discussion started by: Learn4Life
5 Replies

2. Shell Programming and Scripting

Error Handling

Below code works for different databases i.e. MYSQL and ORACLE The problem is for MYSQL in Block: if ; $? taking value accordingly but in case of ORACLE $? is always taking this value as zero (0). That is the reason in Oracle it always going in else Block in any case.. :( and in case of ... (4 Replies)
Discussion started by: ambarginni
4 Replies

3. Shell Programming and Scripting

Standard out and standard error

I need to run a cronjob and in the cronjob I execute a script that if there is an error produces standard error so I do /RUNMYSCRIPT 2> mylogfile.log However, if it runs correctly, I don't get a standard error output, I get a standard out output. How do I redirect both standard error and... (2 Replies)
Discussion started by: guessingo
2 Replies

4. Shell Programming and Scripting

Redirecting standard error issues.

Hello Friends, Good Day. I am trying to redirect a standard error to the bit bucket(/dev/null) but it is not working. Though, it is working fine in redirecting the standard output. Below is the output of my script without any redirection: $ ./CheckVSSLocks.sh... (16 Replies)
Discussion started by: singh.chandan18
16 Replies

5. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

6. Shell Programming and Scripting

Error Handling

Helo Experts, I need a help in handling errors in shell script, wants my errors displayed in text file instead of command window.. My shell script is here; cd /cygdrive/s/Files for FILES in ./*.* do temp=`basename $FILES` if cp $FILES /cygdrive/r/CopyFile1/$FILES; then echo "copy... (5 Replies)
Discussion started by: CelvinSaran
5 Replies

7. Shell Programming and Scripting

Writing to standard error

Hi, I want to redirect the standard output to standard error whenever an error occurs for ex if then echo right else echo wrong fi I want to redirect the wrong to stderror .Adding a line 1>&2 will do that or is additional code to be added.How can i verify whether the output... (2 Replies)
Discussion started by: padmisri
2 Replies

8. Shell Programming and Scripting

standard error to standard out question

Hi there how can i get the result of a command to not give me its error. For example, on certain systems the 'zfs' command below is not available, but this is fine becaues I am testing against $? so i dont want to see the message " command not found" Ive tried outputting to /dev/null 2>&1 to no... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

9. UNIX for Dummies Questions & Answers

Writing to Standard Error

Hi. I'm working on a project for a class, and there's one part of the project that is confusing me. It's a compression and decompression project, and after we write our code for compression, we need to write to standard error. (1) Size of original file (number of characters read... (1 Reply)
Discussion started by: sjung10
1 Replies

10. UNIX for Dummies Questions & Answers

Error: Internal system error: Unable to initialize standard output file

Hey guys, need some help. Running AIX Version 5.2 and one of our cron jobs is writing errors to a log file. Any ideas on the following error message. Error: Internal system error: Unable to initialize standard output file I'm guessing more info might be needed, so let me know. Thanks (2 Replies)
Discussion started by: firkus
2 Replies
Login or Register to Ask a Question