KSH - Trapping errors from within Piped Commands

KSH - Trapping errors from within Piped Commands

Post by Rudy Garz » Thu, 07 Oct 1999 04:00:00



Does anyone know how to trap errors from within piped commands in KSH???

example:

ls -l /non-existent-dir | ls -l /existing-dir
echo $?

I want to find out if the first command failed or if the second one failed.

I tried:

ls -l /non-existent-dir 2> err.file | ls -l /existing-dir 2>> err.file
if [ -s err.file ]
then
  echo "ERROR: `cat err.file`"
  exit 1
fi

I think this is rather ugly.

Thanks,

Rudy

 
 
 

KSH - Trapping errors from within Piped Commands

Post by Ken Pizzi » Sat, 09 Oct 1999 04:00:00



>Does anyone know how to trap errors from within piped commands in KSH???

Yes.  A search through the archives of this group can show you
several possibilites.

Quote:>example:

>ls -l /non-existent-dir | ls -l /existing-dir
>echo $?

>I want to find out if the first command failed or if the second one failed.

Well, it's kinda weird trying to pipe input to an "ls", but
we'll ignore that detail for now...

One solution is of the form:
  if ! ls -l /non-existent-dir; then
    echo "process 1 failed" 1>&2
  fi | if ! ls -l /existing-dir; then
    echo "process 2 failed" 1>&2
  fi

Quote:>ls -l /non-existent-dir 2> err.file | ls -l /existing-dir 2>> err.file
>if [ -s err.file ]
>then
>  echo "ERROR: `cat err.file`"
>  exit 1
>fi

>I think this is rather ugly.

It also doesn't solve the stated problem.

                --Ken Pizzini

 
 
 

1. trap command piped into another command

Hello all ksh lovers,

I would like to know why apparently I cannot process the output of
the trap command :

for instance, I have :

# trap
trap -- /etc/local/startups/Korn/K_shell.logout EXIT

# trap | awk '{printf("%s %s\n", $3, $4)}'
#

I get nothing from the trap piped into the awk.

PS : I want to do this in order to propagate the trap signals into
     ksh subshells. Has anybody tried to do this ?

Thanks in advance.
JM Ch.

2. Permissions

3. trap command question (ksh)

4. Gnome/KDE switching in RH 6.0 ?

5. ksh: trap '...' exit int ... or just trap '...' exit?

6. kfm/kio files in /tmp

7. ksh and trap...(how to release trap?).

8. comp.unix.admin archive

9. trap command question (ksh)

10. ksh bug with $(command) within here document?

11. How do I use su command within ksh script

12. sh equivalent of ksh coprocess (can you pipe commands to a back ground process)

13. simple pipe command breaking in ksh