tony@uqcspe.OZ (Tony O'Hagan) (10/30/87)
In the process of designing systems written (or prototyped) in bourne shell, I often have scripts that are required to return a useful exit status. The problem arises when using traps to clean up and reliably return the correct status when I'm finnished. My current solution looks something like this :- #!/bin/sh : : ts=2 # default trap exit status tdir=/tmp/myprog.$$ trap 'rm -fr $tdir; exit $ts' 0 1 2 3 15 mkdir $tdir || exit 2 input=$tdir/input output=$tdir/output : exit 2 # some sort of error : ts=1; exit 1 # return exit status 1 : ts=0; exit 0 # good-bye I hate having to assign the exit status to a variable before every exit. This doesn't work: trap 'ts=$?; rm -fr $tdir; exit $ts' 0 1 2 3 15 Anyone found a simpler, cleaner method for this problem ? Tony O'Hagan Computer Science Department, ACSnet: tony@uqcspe.oz University of Queensland, UUCP: ..!uunet!munnari!uqcspe.oz!tony St Lucia, Brisbane, 4067 JANET: uqcspe.oz!tony@ukc Australia. Internet: tony%uqcspe.oz@uunet.uu.net Phone: +61 7 377 4125 Smar
eggert@grand.sm.unisys.com (Paul Eggert) (11/03/87)
In article <1639@uqcspe.OZ> tony@uqcspe.oz (Tony O'Hagan) writes: |I hate having to assign the exit status to a variable before every exit. |This doesn't work: | trap 'ts=$?; rm -fr $tdir; exit $ts' 0 1 2 3 15 You are springing the trap twice. Use this instead: trap exit 1 2 3 15 trap 'ts=$?; rm -fr $tdir; exit $ts' 0
tony@uqcspe.OZ (Tony O'Hagan) (11/05/87)
In article <20@grand.sm.unisys.com> you write: >In article <1639@uqcspe.OZ> tony@uqcspe.oz (Tony O'Hagan) writes: >|I hate having to assign the exit status to a variable before every exit. >|This doesn't work: >| trap 'ts=$?; rm -fr $tdir; exit $ts' 0 1 2 3 15 > >You are springing the trap twice. Use this instead: > > trap exit 1 2 3 15 > trap 'ts=$?; rm -fr $tdir; exit $ts' 0 Thanks for you thoughts ... I realised that interupts cause the trap to be done twice, I guess I'm just lazy when it's only doing "rm -f ..." . Although when it does matter, your solution is cleaner than turning off traps within the trap !! What I'm really concerned about is that "exit 2" does not set $? for the trap. I often have something like :- trap 'rm -fr $tdir; exit $ts' 0 1 2 3 15 [ -s "$1" ] || { echo "$1 is empty" >&2; ts=2; exit 2; } One solution mailed to me by Robert Elz [kre@monet.berkeley.edu] was trap 'ts=$?; rm -fr $tdir; exit $ts' 0 1 2 3 15 [ -s "$1" ] || { echo "$1 is empty" >&2; (exit 2); exit; } which could also use the exit status of "mkdir" in something like :- mkdir $2 || exit I'd appeciate any further ideas, Tony O'Hagan. Computer Science Department, ACSnet: tony@uqcspe.oz University of Queensland, UUCP: ..!uunet!munnari!uqcspe.oz!tony St Lucia, Brisbane, 4067 JANET: uqcspe.oz!tony@ukc Australia. Internet: tony%uqcspe.oz@uunet.uu.net Phone: +61 7 377 4125 Smart: tony@uqcspe.oz.au
jack@cadre.dsl.PITTSBURGH.EDU (Jack Nelson) (06/20/88)
The following shell script prints out 0.75 on a line and ends, which seems to follow the sh(1) manual. #!/bin/sh bc <<End scale = 2 3 / 4 End What I would like to do is incorporate this calculation result into a shell variable using the here document: #!/bin/sh var=`bc <<End scale=2 3 / 4 End ` But this doesn't work; an error message "cannot open /tmp/sh01383" always is produced, both on 4.3 and 2.10 systems. Is there a way to do this? Can one deduce this behavior from the manual? I ended up using a one-line perl script; I suppose awk would work just as well. Expr(1) won't do floats. -- John P. Nelson, M.D., 3811 O'Hara St, Pittsburgh, PA 15213, t:412-624-1769 Dept. of Psychiatry, U. of Pittsburgh UUCP: { akgua | allegra | cmcl2 | idis | ihnp4 | mi-cec | pitt | psuvax1 | sun | sunrise | vax135 } ! cadre ! jack ARPA: jack@cadre.dsl.pittsburgh.edu
wu@spot.Colorado.EDU (WU SHI-KUEI) (06/22/88)
In article <1254@cadre.dsl.PITTSBURGH.EDU> jack@cadre.dsl.pittsburgh.edu.UUCP (Jack Nelson) writes: > . . . an explanation of what he wants to do >What I would like to do is incorporate this calculation result into a shell >variable using the here document: > #!/bin/sh > var=`bc <<End > scale=2 > 3 / 4 > End > ` > . . . error messages, why doen't it work etc. You don't need or want the here document. The following will do just fine: #!/bin/sh # not needed with REAL UNIX var=`bc ' scale = 2 3 / 4 quit'` The 'quit' is essential since bc keeps reading the standard input after the command have been read. Carl Brandauer ihnp4!nbires!bdaemon!carl 303-442-1731
davidsen@steinmetz.ge.com (William E. Davidsen Jr) (06/23/88)
In article <1254@cadre.dsl.PITTSBURGH.EDU> jack@cadre.dsl.pittsburgh.edu.UUCP (Jack Nelson) writes: | What I would like to do is incorporate this calculation result into a shell | variable using the here document: | #!/bin/sh | var=`bc <<End | scale=2 | 3 / 4 | End | ` | But this doesn't work; an error message "cannot open /tmp/sh01383" | always is produced, both on 4.3 and 2.10 systems. Is there a way | to do this? Can one deduce this behavior from the manual? This was fixed after V7. It works in SysIII sh, SysV sh, ksh, and sh on Sun (which is mostly sysV I believe). It fails on V7 and every BSD version I could try. Since Berkeley hasn't fixed it (and some similar problems) perhaps you can get ksh. -- bill davidsen (wedu@ge-crd.arpa) {uunet | philabs | seismo}!steinmetz!crdos1!davidsen "Stupidity, like virtue, is its own reward" -me
aida@porthos.csl.sri.com (Hitoshi Aida) (06/23/88)
In article <6774@sigi.Colorado.EDU> wu@spot.Colorado.EDU (WU SHI-KUEI) writes: >The following will do just fine: > > #!/bin/sh # not needed with REAL UNIX > var=`bc ' > scale = 2 > 3 / 4 > quit'` Not quite correct. The following will do just fine: #!/bin/sh # not needed with REAL UNIX var=`echo ' scale = 2 3 / 4 quit' | bc` "quit" can be omitted. Hitoshi AIDA (aida@csl.sri.com) Computer Science Lab, SRI International
ford@elgar.UUCP (Mike "Ford" Ditto) (06/24/88)
In article <1254@cadre.dsl.PITTSBURGH.EDU> jack@cadre.dsl.pittsburgh.edu.UUCP (Jack Nelson) writes: >What I would like to do is incorporate this calculation result into a shell >variable using the here document: > #!/bin/sh > var=`bc <<End > scale=2 > 3 / 4 > End > ` >But this doesn't work; an error message "cannot open /tmp/sh01383" >always is produced, both on 4.3 and 2.10 systems. Both command-substitution and here-files use a temporary file, and apparrently they use the same name. I'd call it a bug. But I'd do it like this anyway: #!/bin/sh var=`echo 'scale=2 3 / 4' | bc` Or better yet, var=`echo 2k 3 4 /p | dc` which is more efficient and also works in csh (if you put a 'set' at the beginning). -=] Ford [=- "Once there were parking lots, (In Real Life: Mike Ditto) now it's a peaceful oasis. ford@kenobi.cts.com This was a Pizza Hut, ...!sdcsvax!crash!kenobi!ford now it's all covered with daisies." -- Talking Heads
smileyf@ucscb.UCSC.EDU (Shutoku Shia) (06/26/88)
one other soln. is (instead of using here doc): #!/bin/sh float_var=`echo "scale = 4; 3/4" | bc"` Shutoku Shia ----------------------------------------------------------------------- | Bitnet: smileyf@ucscf.bitnet | formerly in | | Internet: smileyf@ucscf.UCSC.EDU | Dept. of Cmp. & Info. Sci. | | Arpanet: smileyf@ucscf.UCSC.EDU | Univ. of Calif., Santa Cruz | | Uucp: ...!ucbvax!ucscc!ucscf!smileyf | | -----------------------------------------------------------------------
smileyf@ucscb.UCSC.EDU (Shutoku Shia) (06/26/88)
In article <3920@saturn.ucsc.edu> smileyf@ucscb.UCSC.EDU (Shutoku Shia) writes: >one other soln. is (instead of using here doc): > >#!/bin/sh >float_var=`echo "scale = 4; 3/4" | bc"` - I made a mistake here .. there should be no double quote following bc. Shutoku Shia ----------------------------------------------------------------------- | Bitnet: smileyf@ucscf.bitnet | formerly in | | Internet: smileyf@ucscf.UCSC.EDU | Dept. of Cmp. & Info. Sci. | | Arpanet: smileyf@ucscf.UCSC.EDU | Univ. of Calif., Santa Cruz | | Uucp: ...!ucbvax!ucscc!ucscf!smileyf | | -----------------------------------------------------------------------
chris@mimsy.UUCP (Chris Torek) (06/28/88)
In article <1254@cadre.dsl.PITTSBURGH.EDU> jack@cadre.dsl.PITTSBURGH.EDU (Jack Nelson) writes: >... using [a] here document: > #!/bin/sh > var=`bc <<End > scale=2 > 3 / 4 > End > ` >But this doesn't work; an error message "cannot open /tmp/sh01383" >always is produced, both on 4.3 and 2.10 systems. Is there a way >to do this? Can one deduce this behavior from the manual? It is almost noted in the BUGS section: If << is used to provide standard input to an asynchronous process invoked by &, the shell gets mixed up about naming the input document. A garbage file /tmp/sh* is created, and the shell complains about not being able to find the file by another name. Given this, the error message above, and the fact that a garbage file is created, you can guess what is happening. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris