OR'ing condition in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting OR'ing condition in script
# 1  
Old 04-19-2005
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:

Code:
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
# 2  
Old 04-19-2005
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 ...
# 3  
Old 04-19-2005
try this one ...
Code:
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

# 4  
Old 04-19-2005
I want to avoid that second if..else block. Is there anyway ?

I feel not. What say ?

Vino
# 5  
Old 04-19-2005
Code:
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

# 6  
Old 04-19-2005
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
# 7  
Old 04-20-2005
per the cp man page ...
Quote:
-f Unlink. If a file descriptor for a destination file
cannot be obtained, attempt to unlink the destination
file and proceed.
...

/usr/bin/cp
The following option is supported for /usr/bin/cp only:

-p Preserve. cp duplicates not only the contents of
source_file, but also preserves the owner and group
id, permissions modes, modification and access time,
and ACLs if applicable. Note that the command may fail
if ACLs are copied to a file system that does not sup-
port ACLs. The command will not fail if unable to
preserve modification and access time or permission
modes. If unable to preserve owner and group id, cp
will not fail, and it will clear S_ISUID and S_ISGID
bits in the target. cp will print a diagnostic message
to stderr and return a non-zero exit status if unable
to clear these bits.

In order to preserve the owner and group id, permis-
sion modes, and modification and access times, users
must have the appropriate file access permissions;
this includes being superuser or the same owner id as
the destination file.
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 ...
Code:
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 #

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Condition in bash script

I want get from user and pass these parameters to bash script. script should copy files in user home directory. FYI: each file might be exist or not, might be one of them exist or four of them. Here is my script, it always copy file1 and seems only one of them execute! #!/bin/bash for... (6 Replies)
Discussion started by: indeed_1
6 Replies

2. Shell Programming and Scripting

Creating a condition on a bash script

I wrote a code to find codons in a DNA string. The only problem I have is how do I make the code only work for a file with DNA. This means the file only has the characters a,c,g,t and no white space characters. (3 Replies)
Discussion started by: germany1517
3 Replies

3. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. UNIX for Dummies Questions & Answers

While condition in shell script

while do if ;then read driverName else driverName="" fi done can anyone please explain what exactly is happening on 1st line...is it like the conditions being ORed...I have no clue about this. (4 Replies)
Discussion started by: rtagarra
4 Replies

5. Shell Programming and Scripting

Script with variable and condition

Hello newbies question... I just need a script able to launch a command when a condition is matched : #!/bin/ksh SIZ = 'cat /nurp/control.lst|wc -l' if test "$SIZ" -gt 0 then echo 1 else echo 2 fi but I receive errors messages ./t2: SIZ: not found 2 whats wrong ? (5 Replies)
Discussion started by: vdurieu
5 Replies

6. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. Shell Programming and Scripting

Script printing else condition

Please help what is wrong in below script. It is printing else also, else echo "DATABASE NOT RUNNING :${i} ================================================= #!/usr/bin/ksh set -A DBS $(cat /etc/oratab | grep ":Y" | awk -F : '{print $1}') set -A RDBS $(ps -ef | grep -i pmon | grep... (3 Replies)
Discussion started by: allwin
3 Replies

8. Shell Programming and Scripting

Condition in script

Hi all, I am writing a script (.sh) which takes backup of database, zip it ftp to remote server and finally mail me to notify. So far this all has been done, however, now I am looking to add notification over each step. Means if any step is failed, then I should get an email and if all goes... (2 Replies)
Discussion started by: viki250
2 Replies

9. Shell Programming and Scripting

grep'ing and sed'ing chunks in bash... need help on speeding up a log parser.

I have a file that is 20 - 80+ MB in size that is a certain type of log file. It logs one of our processes and this process is multi-threaded. Therefore the log file is kind of a mess. Here's an example: The logfile looks like: "DATE TIME - THREAD ID - Details", and a new file is created... (4 Replies)
Discussion started by: elinenbe
4 Replies

10. Shell Programming and Scripting

IF condition failing in a SSH script

Hi, I'm ssh-in to a remote machine (ubuntu) and trying to execute a little script in there.The script looks like this: ssh user@ubuntu <<EOF cd ~/test ls -l echo "Continue counting files starting with a`s ?" read answer if then ls -l a* else exit fi EOF Now everything works... (9 Replies)
Discussion started by: rubionis
9 Replies
Login or Register to Ask a Question