If file1 and file2 exist then


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If file1 and file2 exist then
# 1  
Old 06-27-2013
If file1 and file2 exist then

HI,

I would like a little help on writing a if statement.

What i have so far is:
Code:
#!/bin/bash

FILE1=path/to/file1
FILE2=path/to/file2

echo ${FILE1} ${FILE2}

if [[ ! ( -f $FILE1 && -f $FILE2 ) ]]
then
echo file1 and file2 not found
else
echo FILE ok
fi

but as for prep work I'd like to modify it to output which file is found.

so more along the line of this:

if file1 is found then echo file1 found



But i would need the script to look for both 1 and 2 if 1 doesn't exist.

Not at all sure how to write this.
# 2  
Old 06-27-2013
Code:
#!/bin/bash

FILE1="path/to/file1"
FILE2="path/to/file2"

[ -f "$FILE1" ] && printf "%s found\n" "$FILE1"
[ -f "$FILE2" ] && printf "%s found\n" "$FILE2"

if [ ! -f "$FILE1" ] && [ ! -f "$FILE2" ]
then
        printf "%s and %s missing\n" "$FILE1" "$FILE2"
fi

This User Gave Thanks to Yoda For This Post:
# 3  
Old 06-27-2013
Try:
Code:
if [[ ! -f "$file1" && ! -f "$file2" ]]; then

or reverse the condition
Code:
if [[ -f "$file1" || -f "$file2" ]]; then
  echo FILE ok
else
  ...

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 06-27-2013
Quote:
Originally Posted by Yoda
Code:
#!/bin/bash

FILE1="path/to/file1"
FILE2="path/to/file2"

[ -f "$FILE1" ] && printf "%s found\n" "$FILE1"
[ -f "$FILE2" ] && printf "%s found\n" "$FILE2"

if [ ! -f "$FILE1" ] && [ ! -f "$FILE2" ]
then
        printf "%s and %s missing\n" "$FILE1" "$FILE2"
fi

Thank you! This is exactly what I'm looking for!

Quote:
Originally Posted by Scrutinizer
Try:
Code:
if [[ ! -f "$file1" && ! -f "$file2" ]]; then

or reverse the condition
Code:
if [[ -f "$file1" || -f "$file2" ]]; then
  echo FILE ok
else
  ...


Thank you I was just about to update this as i forgot i'm using "or" not "&"

Thanks for your help guys!

---------- Post updated at 03:45 PM ---------- Previous update was at 03:41 PM ----------

I do have one more question for you guys....

If it finds file1 or file2 can you make it into a variable to use for the rest of the script? which ever one it finds. in the script i'm making there should only be 1 of the two that exist.
# 5  
Old 06-27-2013
What happens when both of them exist?
# 6  
Old 06-27-2013
Quote:
Originally Posted by Scrutinizer
What happens when both of them exist?

The first part that I posted will be used to to check the control of the script and exit if both files exist before the the other part checks to see which file is actually valid.

something along the line of this:

Code:
if ([ -f $FILE1 ] && [ -f$FILE2 ])

then

echo both files exist script is exiting

else

echo one or more files missing

fi

# 7  
Old 06-27-2013
I mean which of the file names should the variable (post #4) contain if both files exist.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

3. Shell Programming and Scripting

look for line from FILE1 at FILE2

Hi guys! I'm trying to write something to find each line of file1 into file2, if line is found return YES, if not found return NO. The result can be written to a new file. Can you please help me out? FILE1 INPUT: WATER CAR SNAKE (in reality this file has about 600 lines each with a... (2 Replies)
Discussion started by: demmel
2 Replies

4. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

Remove words from file2 that don't exist in file1

Hi I have to list of words file1 and file2, I want to compare both lists and remove from file2 all the words that don't exist in file1. How can I do this? Many thanks (4 Replies)
Discussion started by: noliveira
4 Replies

6. Shell Programming and Scripting

grep -f file1 file2

Hi I started to learn bash a week ago. I need filter the strings from the last column of a "file2" that match with a column from an other "file1" file1: chr10100036394-100038350AK077761 chr10100041065-100046547AK032226 chr10100041065-100046547AK016270 chr10100041065-100046547AK078231 ...... (6 Replies)
Discussion started by: geparada88
6 Replies

7. Shell Programming and Scripting

file1 newer then file2

Hello, I am new to shell scripting and i need to create a script with the following directions and I can not figure it out. Create a shell script called newest.bash that takes two filenames as input arguments ($1 and $2) and prints out the name of the newest file (i.e. the file with the... (1 Reply)
Discussion started by: mandylynn78
1 Replies

8. Shell Programming and Scripting

grep -f file1 file2

Wat does this command do? fileA is a subset of fileB..now, i need to find the lines in fileB that are not in fileA...i.e fileA - fileB. diff fileA fileB gives the ouput but the format looks no good.... I just need the contents alone not the line num etc. (7 Replies)
Discussion started by: vijay_0209
7 Replies

9. Shell Programming and Scripting

I want records in file2 those are not exist in file1

I have two files - file1 and file2. Now I want records in file2 those are not exist in file1. How to grep this ? eg: file1 08941 08944 08945 08946 08947 file2 08942 08944 5 08942 08945 5 08942 08946 4 08942 08947 6 08942 08952 4 08942 08963 5 08942 ... (3 Replies)
Discussion started by: suresh3566
3 Replies

10. Shell Programming and Scripting

match value from file1 in file2

Hi, i've two files (file1, file2) i want to take value (in column1) and search in file2 if the they match print the value from file2. this is what i have so far. awk 'FILENAME=="file1"{ arr=$1 } FILENAME=="file2" {print $0} ' file1 file2 (2 Replies)
Discussion started by: myguess21
2 Replies
Login or Register to Ask a Question