If file1 and file2 exist then

HI,

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

What i have so far is:

#!/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.

#!/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
1 Like

Try:

if [[ ! -f "$file1" && ! -f "$file2" ]]; then

or reverse the condition

if [[ -f "$file1" || -f "$file2" ]]; then
  echo FILE ok
else
  ...
1 Like

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

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.

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:

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

then

echo both files exist script is exiting

else

echo one or more files missing

fi

I mean which of the file names should the variable (post #4) contain if both files exist.