[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: question about sound



On Fri, Aug 19, 2022 at 11:55:33PM -0500, David Wright wrote:
> On Fri 19 Aug 2022 at 08:46:29 (-0400), Greg Wooledge wrote:
> > On Thu, Aug 18, 2022 at 11:13:11PM -0500, David Wright wrote:
> > > The attraction of a one-liner is partly because of screens
> > > being around four times wider than high (characterwise).
> > > Wouldn't it be nice if bash had Perl's die ….
> > 
> > Some people put a die() function in their scripts, and then use it.
> > 
> > die() { printf >&2 '%s\n' "$*"; exit 1; }
> > 
> > Or variants thereof.  There are almost as many variations as there are
> > shell programmers.
> 
> But if I have that, and:
> 
> soxy is a function
> soxy () 
> { 
>     [ -z "$1" ] && die "Usage:	${FUNCNAME[0]} path-to/sound-file-of-any-type [trim 20 2]
> 	runs sox to play the file with any arguments given.
> 	The example above reminds you to put the full argument.";
>     local From="$1";
>     shift;
>     sox -q "$From" -t alsa default "$@"
> }
> 
> then typing just:
> 
> $ soxy
> 
> into an xterm will have the same effect as:
> 
> $ ^D
> 
> killing bash and the xterm, whereas what I would want from die is a
> "double-return", quitting both die and soxy, and leaving me at:
> 
> $ 
> 
> just as Perl's die would do (if soxy was a Perl program).

The problem is... the function is running in your current shell's
process. This is totally different from the Perl case, where your
Perl programm gets its own process, so exit is going to do the
right thing (in both cases, the current process is terminated).

(Actually, Perl is more complicated, since you can catch a die
with an eval, so it is basically a full-fledged exception machinery).

I see two ways forward: either use return in your shell function;
you'd have to build your exception handling level-by-level, by
hand. Or do what everyone does and build your soxy as a command,
which will be executed in its own shell and not as a function.

You can set traps in the shell for exit and return events, so
you might try to build exception handling on that, but I don't
know whether it is a good idea. But possibly a nice exercise :)

Cheers
-- 
t

Attachment: signature.asc
Description: PGP signature


Reply to: