Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!laidbak!daveb From: daveb@laidbak.UUCP (Dave Burton) Newsgroups: comp.unix.questions Subject: sh feature: redirection of flow control bug Message-ID: <1316@laidbak.UUCP> Date: 23 Jan 88 17:33:46 GMT Reply-To: daveb@laidbak.UUCP (Dave Burton) Organization: is pretty bad/My method of Lines: 57 This is an ancient (but still present) bug, so apologies if I'm reviving a dead subject. I just had occasion to use a feature of the Bourne/Korn shells, I/O redirection of the flow control statements. The following script demonstrates a bug present in sh and absent in ksh: #!/bin/sh # replace with $WHEREEVER/bin/ksh if [ ! -d "$1" ] then echo "'$1' not a dir" exit 1 fi >&2 cd $1 echo "hello from `pwd`" exit 0 This fails with sh and works with ksh. Output from the sh version: Script started on Sat Jan 23 11:12:34 1988 laidbak:1% x . hello from /u3/daveb laidbak:2% x x 'x' not a dir x: x: bad directory laidbak:3% exit script done on Sat Jan 23 11:12:52 1988 Output from the ksh version: Script started on Sat Jan 23 11:14:23 1988 laidbak:1% x . hello from /u3/daveb laidbak:2% x x 'x' not a dir laidbak:3% exit script done on Sat Jan 23 11:14:58 1988 I haven't tested, but I'm sure {for,while}do/done and case/esac exhibit the same behavior as the if/fi. The workaround is easy (if inelegant): redirect output of each individual command; or set a flag and test it upon completion of the if/fi. Is there a better/preferred workaround? The encapsulated command list is small in this example; adding more command to the list makes individual redirection tiresome. Hacking the shell is out - several of the systems this code will execute on have no source available. And please, no sh/csh/ksh flames/merit discussions. -- --------------------"Well, it looked good when I wrote it"--------------------- Verbal: Dave Burton Net: ...!ihnp4!laidbak!daveb V-MAIL: (312) 505-9100 x325 USSnail: 1901 N. Naper Blvd. #include Naperville, IL 60540