bash: check if file exists(without using if)

basically im trying to make this work in a bash shell script without using if statements

if [ -f testfile ]
 then
 echo testfile exists!
 fi

what it does is check if the file exists or not

i have this line but its not working, it checks if the testfile exists if it doesnt it 2> to the dev null pathname and i have an echo statement which says it doesnt exist

Perhaps you could have posted the line that didn't work, then.

If you don't want to use 'if', your options are limited.

[ -f testfile ] || exit 1

im trying the

[ -f testfile ] || exit 1

with an echo statement "Exits since testfile doesnt exist"

i have another one

[ -f testfile2 ] || exit 1

with an echo statement "testfile 2" exist

can't figure out how to show testfile echo statement or the testfile2 echo statement

Usually, you don't.

You could do something like [ -f file ] || echo "doesn't exist" && exit 1 but what if the echo fails for some reason? Then you won't exit.

You could combine statements like [ -f file ] || ( statement1 ; statement2 ) except that, because it's in a subshell, exit won't quit your main shell, just the part in brackets!

Since you're in BASH though, you can do this:

function die
{
        echo "$@" >&2
        exit 1
}

[ -f file ] || die "File does not exist"

---------- Post updated at 09:32 AM ---------- Previous update was at 09:29 AM ----------

If you have a bunch of them, you can do

function die
{
        echo "$@" >&2
        exit 1
}

for FILE in "file1" "file2" "file3" "file4" "file5" "file6"
do
        [ -f "$FILE" ] || die "$FILE does not exist"
done