k12osn@redhat.com
[Top] [All Lists]

Re: [K12OSN] OT: bash help - copying a series of files

Subject: Re: [K12OSN] OT: bash help - copying a series of files
From: Nils Breunese
Date: Fri, 12 Dec 2008 17:07:38 +0100
Rob Owens wrote:

Nils Breunese wrote:
Rob Owens wrote:

You could first change the current working directory, so you don't need
to supply the path to every file.

   cd /usr/local/share/icons
   cp Drawings.desktop Obsolete\ Drawings.desktop Obsolete\ a\
Drawing.desktop /dest/dir


That would work, but how do I error check?  I'd like some verification
that the cd worked before cp'ing (because what if I was rm'ing instead?)

You could check if the current working directory is the directory you want it to be, using the pwd command.

        #!/bin/bash
        TARGET_DIR=/some/directory
        cd $HOME
        if [ `pwd` == $TARGET_DIR ]; then
                echo "OK"
        else
                echo "Not OK"
        fi

You could also use the $? variable to check the exit code of last command

        $ cd $HOME
        $ echo $?
        0
        $ cd non-existent-directory
        -bash: cd: non-existent-directory: No such file or directory
        $ echo $?
        1

Or in a script:

        #!/bin/bash
        TARGET_DIR=/some/directory
        cd $HOME
        if [ "$?" == "0" ]; then
                echo "OK"
                ... do some stuff in the $TARGET_DIR ...
        else
                echo "Not OK"
        fi

I haven't tested a line of code in this post, but the general idea is clear, I hope.

Nils Breunese.

_______________________________________________
K12OSN mailing list
K12OSN@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/k12osn
For more info see <http://www.k12os.org>

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