Mark
Mark

Reputation: 69

Shell Scripting - checking for file existence

I'm trying to write a shell script that checks if 3 specific files exist in the folder. I also want to store the results in an array. Once the file checking is done, I want to check the array to make sure at least one (1) file exists before the script can continue.

A successful output should look like this:

File file1.sh..........OK!
File file2.sh..........NOT FOUND!
File file3.sh..........OK!
File check completed successfully.

A failed output should look like this:

File file1.sh..........NOT FOUND!
File file2.sh..........NOT FOUND!
File file3.sh..........NOT FOUND!
At least one file is required to continue.

Right now I am using if/else statements like this:

#!/bin/bash
if [[ -f file1.sh ]]; then
  echo "File file1.sh........OK!";
  isFILE1=1
else 
  echo "File file1.sh........NOT FOUND!";
  isFILE1=0
fi

However, I'd like to do something like this instead. And also make printing the result on the same line as the File file1.sh.......:

#!/bin/bash
echo "File file1.sh.........";
if [[ -f file1.sh ]]; then
  echo "OK!";
  isFile[0]=1;
else 
  echo "NOT FOUND!";
  isFile[0]=0;
fi

I'm not sure how to check if at least 1 file in the array isFile exists.

Upvotes: 0

Views: 175

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201439

To keep the output on one line change this

echo "File file1.sh.........";

to

echo -n "File file1.sh.........";

The -n suppresses the new-line.

For checking the file existence from an array, use a singular flag and change it to 1 if you find a file.

Upvotes: 2

Related Questions