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

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




Nils Breunese wrote:
> 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.
> 
Thanks for the good suggestions (Petre, you too!).  I learn a lot on
this list.

-Rob
********************************************************

The information transmitted is intended only for the person or entity to
which it is addressed and may contain confidential and/or privileged
material. If you are not the addressee, any disclosure, reproduction,
copying, distribution, or other dissemination or use of this transmission in
error please notify the sender immediately and then delete this e-mail.
E-mail transmission cannot be guaranteed to be secure or error free as
information could be intercepted, corrupted lost, destroyed, arrive late or
incomplete, or contain viruses.
The sender therefore does not accept liability for any errors or omissions
in the contents of this message which arise as a result of e-mail
transmission. If verification is required please request a hard copy
version.

********************************************************



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