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 ?
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 ...
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
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 ...
$ 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?
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.
And I just checked the posix standard. According to the standard, Solaris has it right and HP-UX has it wrong.