[K12OSN] OT: bash help - copying a series of files
Nils Breunese
nils at breun.nl
Fri Dec 12 16:07:38 UTC 2008
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.
More information about the K12OSN
mailing list