Subject: Re: cd success?
To: None <netbsd-users@NetBSD.org>
From: Andrew Smallshaw <andrews@sdf.lonestar.org>
List: netbsd-users
Date: 01/28/2007 19:26:01
On Fri, Jan 26, 2007 at 08:12:56AM +0000, Chavdar Ivanov wrote:
> On 1/26/07, Jan Danielsson <jan.m.danielsson@gmail.com> wrote:
> >
> >   The script does this:
> >
> >   cd ~/backup/foo
> >   rm -Rf *
> >
> >   Currently, if the cd-command fails, I will have to kill myself. I
> >don't want to do that. If I remember correctly, there's a variable,
> >something like $? which gets set to the return code. Is that set for
> >internal shell commands as well?
> 
> Just
> $ cd /nonexistent && echo BAH
> ksh: cd: /nonexistent - No such file or directory
> $ cd /tmp && echo BAH
> BAH

As well as testing for the return code explicitly there is also
the option to exit a non-interactive shell if a command that is
not tested fails.  Original sh used to do this as standard but that
is one useful thing that went out of the window with POSIX.  You
can still get the behaviour with the set -e option though:

#! /bin/sh

set -e

cd nowhere
rm *

The script will exit before rm gets called, assuming there is no
directory called "nowhere".

-- 
Andrew Smallshaw
andrews@sdf.lonestar.org