Subject: Re: cd success?
To: None <netbsd-users@NetBSD.org>
From: Michael Parson <mparson@bl.org>
List: netbsd-users
Date: 01/26/2007 10:21:13
On Fri, Jan 26, 2007 at 08:58:35AM +0100, Jan Danielsson wrote:
> Hello all,
> 
>    I have a script which contains the line:
> 
>    rm -Rf *
> 
>    As you might imagine, I'm somewhat afraid that script might run amok.
> It shouldn't, obviously, but Just In Case... Is there any way to
> determine if a "cd" was successful or unsuccessful?
> 
>    The script does this:
> 
>    cd ~/backup/foo
>    rm -Rf *

Someone else made a more complicated version of this, but the basic
piece you're missing is that you can use && to seperate commands and
it will only execute what follows that if the first one exits successful,
so, you can do:

cd ~/backup/foo && rm -Rf *

If the cd fails, the rm won't happen.

You could add another check after that for the exit value and stop the script:

    cd ~/backup/foo && rm -Rf *
    
    if [[ $? -eq 0 ]]; then
        echo "cd failed! aborting!"
        exit 1
    fi


>    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?
> 
>    Or should I use some:
> 
>    if [ `pwd` == "~/backup/foo" ] ; then
>       echo "Phew!...
>    fi
> 
>    ..instead?
> 
> -- 
> Kind regards,
> Jan Danielsson
> 
> 



-- 
Michael Parson
mparson@bl.org