OR'ing condition in script

Hi,

I have the following requirement. I am building a product on linux. The final build executables and libraries are all in different directories. I am writing a release script to collect all these final build items into a directory in /home/$USER/release.

I have the following condition:

if [ -f $DEVMGR/$FILE -o -f $DEVMGRCONSOLE/$FILE ] ; then

Now I need to copy which ever above file is present to /home/$USER/release. Both conditions will never be true at any time. Only 1 will be. How do I incorporate that ?

Your inputs please.

Vino

if you're absolutely certain that both conditions can never be true at the same time, your "if" condition should suffice ... otherwise, you can add another check to make sure that only 1 of the conditions is true at any time ...

try this one ...

if [ -f $DEVMGR/$FILE -o -f $DEVMGRCONSOLE/$FILE ]
then
    if [ -f $DEVMGR/$FILE ]
    then
        cp -p $DEVMGR/$FILE /home/$USER/release
    else
        cp -p $DEVMGRCONSOLE/$FILE /home/$USER/release
    fi
fi

I want to avoid that second if..else block. Is there anyway ?

I feel not. What say ?

Vino

if [ -f $DEVMGR/$FILE -o -f $DEVMGRCONSOLE/$FILE ]
then
    cp -p $DEVMGR/$FILE /home/$USER/release 2> /dev/null
    cp -p $DEVMGRCONSOLE/$FILE /home/$USER/release 2> /dev/null
fi

Ice,

Thanks for the solutions. Works good.

One question though. Why the -p ? Wouldn't -f work well ?

Yes the man pages do tell that permissions will be preserved. What is the significance of -p in this case ? Am I missing something ?

Vino

per the cp man page ...

however, i've never really used the "-f" option --- like most other esoteric options of other commands that are good to know but not required for basic stuff --- so i don't actually understand the unlink statement ... maybe one of the hardcore c programmers here can explain it better ...

do look at this test here and you can decide what it is you need ...

root_mybox:/tmp # cp -p /etc/profile testprofile
root_mybox:/tmp # ls -l /etc/profile testprofile
-rw-r--r--   1 root     sys          920 Dec  3 19:04 /etc/profile
-rw-r--r--   1 root     sys          920 Dec  3 19:04 testprofile
root_mybox:/tmp # cp -f /etc/profile testprofile1
root_mybox:/tmp # ls -l /etc/profile testprofile1
-rw-r--r--   1 root     sys          920 Dec  3 19:04 /etc/profile
-rw-r--r--   1 root     other        920 Apr 20 17:51 testprofile1
root_mybox:/tmp #

Consider this:

$ date > one
$ touch two
$ chmod 0 two
$ cp one two
cp: cannot open 'two': Permission denied
$ cp -f one two

With the first cp, I could not open "two" for writing (could not obtain a file descriptor). By unlinking "two", the second cp command created a new "two" from scratch. However, the language you quoted implied that cp would first try to open the destination file and if that worked, bypass the unlink. More normal language is:

And that could cause a different result. The above language came from HP-UX. What OS supplied your language?

Solaris 8 ...

Oh my...

You're right. Solaris does say that! I just tried the sequence
touch three
ln -s three two
date > one
cp -f one two
on both Solaris and HP-UX. On HP-UX, I get a brand new file called "two" and "three" is untouched. On Solaris, three is overwritten. :eek:

And I just checked the posix standard. According to the standard, Solaris has it right and HP-UX has it wrong.