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
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