0

I have a bash file where I am currently using an if statement to check for two files existing before continuing with the rest.

if [[ -e $flchck ]] && [[ -e $flchck2 ]]; then

--do stuff here

else

--sends me an email telling me the files were not found.

My question is, is this the most efficient way to do this? If I need it to check more files, would I just keep adding && to include more.

What if I want it to tell me which file(s) was missing when it did this check.

Any guidance/direction on how to handle this would be greatly appreciated.

3
  • Don't tag sh if you're going to use bash-only syntax (like [[) Commented Oct 17, 2022 at 21:40
  • Anyhow -- you certainly can have an array of files and iterate through that array... if you're targeting bash; sh doesn't support arrays. Commented Oct 17, 2022 at 21:41
  • sorry I didnt mean to tag sh, I am using bash for this. I will update acordingly Commented Oct 17, 2022 at 21:44

1 Answer 1

1

Typically one would use an array for this purpose. Assuming that your error-report-mailing code is encapsulated in a command named send_error_email, this may look like:

#!/usr/bin/env bash
files=( /path/to/file1 /path/to/file2 /path/to/file3 )
for file in "${files[@]}"; do
  if ! [[ -e $file ]]; then
    send_error_email "$file does not exist"
    exit 1
  fi
done
# do stuff here
Sign up to request clarification or add additional context in comments.

2 Comments

If I am understanding this correctly, it is going to cycle through all the files, and if it finds one that doesn't exist it exits out and sends me the email. if it gets through all without exiting it continues on?
The above summary is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.