The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem with if, while, for conditions kittusri9 Shell Programming and Scripting 3 04-24-2008 09:15 AM
reduce the or conditions hitmansilentass Shell Programming and Scripting 8 05-03-2007 05:27 PM
multiple conditions in if/then grandtheftander UNIX for Dummies Questions & Answers 4 07-21-2006 01:58 PM
if statement with two conditions cin2000 Shell Programming and Scripting 1 01-23-2006 03:21 PM
if statement with two conditions -e, && yongho Shell Programming and Scripting 16 06-14-2005 04:46 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-25-2008
RRVARMA's Avatar
RRVARMA RRVARMA is offline
Registered User
  
 

Join Date: Feb 2008
Location: Bangalore, INDIA
Posts: 34
Exclamation How to giv two conditions in IF statement..??

Actually i need to satisfy both the condition..
lik i'm lookin for two different files.. and if BOTH the files r not present then it should run the followin script.. For example

inputfile1=data/in/inputfile1.txt
inputfile2=data/in/inputfile2.txt

if [ ! -f ${inputfile1} && ! -f ${inputfile2} ]
then

echo " "
echo "ERROR: Files not found."
echo " "
exit 1

fi


echo " "
echo "RUN SUCCESSFUL: Both Files found. "
echo " "

exit 1

Here even if the second file is not present its showing RUN SUCCESSFUL.. Please Help...
  #2 (permalink)  
Old 04-25-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
Replace && by -a
Code:
inputfile1=data/in/inputfile1.txt
inputfile2=data/in/inputfile2.txt

if [  ! -f ${inputfile1} -a ! -f ${inputfile2} ]
then
   echo " "
   echo "ERROR: Both files not found."
   echo " "
   exit 1
fi

if [  ! -f ${inputfile1} -o ! -f ${inputfile2} ]
then
   echo " "
   echo "ERROR: File(s) not found."
   echo " "
   exit 1
fi

echo " "
echo "RUN SUCCESSFUL: Both Files found. "
echo " "

exit 0
jean-Pierre.

Last edited by aigles; 04-25-2008 at 09:02 AM.. Reason: 'exit 0' is a better choice for successful execution
  #3 (permalink)  
Old 04-25-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,419
Just shorter..
Code:
#!/bin/sh
inputfile1=data/in/inputfile1.txt
inputfile2=data/in/inputfile2.txt
[ -f ${inputfile1} -a -f ${inputfile2} ] && echo OK || echo NOK

Last edited by danmero; 04-25-2008 at 09:09 AM.. Reason: remove not
  #4 (permalink)  
Old 04-25-2008
push push is offline
Registered User
  
 

Join Date: Nov 2007
Location: Pune
Posts: 22
Simple Mistake :-)
You can use && if u use like
Quote:

if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]
in your code

Quote:
if [ ! -f ${inputfile1} -a ! -f ${inputfile2} ]

Last edited by push; 04-25-2008 at 09:10 AM.. Reason: wrong code
  #5 (permalink)  
Old 04-25-2008
push push is offline
Registered User
  
 

Join Date: Nov 2007
Location: Pune
Posts: 22
Simple Mistake :-)
You can use && if u use like
Quote:

if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]
in your code

Quote:
if [ ! -f ${inputfile1} -a ! -f ${inputfile2} ]
  #6 (permalink)  
Old 04-25-2008
push push is offline
Registered User
  
 

Join Date: Nov 2007
Location: Pune
Posts: 22
sorry didnt see the replies
  #7 (permalink)  
Old 04-25-2008
penchal_boddu penchal_boddu is offline
Registered User
  
 

Join Date: Apr 2008
Location: Bangalore
Posts: 127
shorter form :

inputfile1=data/in/inputfile1.txt
inputfile2=data/in/inputfile2.txt

[ ! -f ${inputfile1} -a ! -f ${inputfile2} ] && { echo "\n ERROR: Both files not found. \n" ; exit 1 ;}

[ ! -f ${inputfile1} -o ! -f ${inputfile2} ] && { echo "\n ERROR: File(s) not found. \n" ; exit 1 ;}

echo "\n RUN SUCCESSFUL: Both Files found. \n"
exit 1
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0