scsh-users
[Top] [All Lists]

idiomatic use of &&

To: scsh-news@zurich.ai.mit.edu
Subject: idiomatic use of &&
From: clements@ccs.neu.edu (John B. Clements)
Date: Thu, 12 Jun 2003 17:14:49 +0000 (UTC)
Organization: Northeastern University, College of Computer Science
I'm trying to write a shell script using &&, and I run into
the following issue: Let's say I have a sequence of commands,
strung together with &&, and I want to set the cwd for some of
them.  Easy enough, I just stick a (begin (with-cwd ...)) into
the &&.  But now, suppose I have more than one process that
gets run inside the with-cwd.  I want the entire sequence of
commands to have the && behavior; that is, if any one of them
fails, the whole thing halts.

If I use a nested &&, I don't by default get this behavior:

> (&& (begin (&& (rm nonexistentfile) (echo aaaa))) (echo bbbb))
rm: nonexistentfile: No such file or directory
bbbb
#t

That makes sense to me, given the stated semantics of &&.  
How do I change it so that any error halts the whole string?  
Here's my solution, but it seems like there must be a more
idiomatic way of doing this:

> (define (propagate-error x) (if x #t (error 'sub-error)))
> (&& (begin (propagate-error (&& (rm nonexistentfile) 
                                  (echo aaaa)))) 
      (echo bbb))
rm: nonexistentfile: No such file or directory

Error: sub-error
#f
> 

This would seem like a common problem.  Also, it's a pain to
abstract over &&, because it's a macro.

Thanks,

john clements
 

<Prev in Thread] Current Thread [Next in Thread>