I have an if
in my bash script that has to check if EITHER of the 2 files exists, if they don't exist it should echo match.
Code I have:
if [[ ! -f /etc/benchmarking/code ]] && [[ ! -f /etc/benchmarking/code.class ]]; then
echo "match"
fi
But this doesn't not seem to work for some reason.
I am 110% sure that these 2 files do not exist. I don't get any errors, it just doesn't enter the if
.
I am new to bash scripting so I'm not sure what could be wrong.
[[ ! -f /etc/.../... ]]; echo $?
for each.[[ -f xxx ]]
returns true if xxx exists and is a regular file (or a symlink to a regular file). It will return false if it doesn't exist, or if it can't tell if it exists or not or if it exists but is not a regular file (directory, pipe, device, socket...) or if it's a symlink to a file that doesn't exist or it can't tell if it exists or not or is not a regular file...[[ -e $file ]] || [[ -L $file ]]
is closer to a test for existence.ls -d -- "$file" > /dev/null 2>&1
is even closer. And as a rule, this kind of test leads to race conditions. Maybe there's another way.[ -f ]
checks are just failing because they are under/etc
which you likely do not have the permissions for, and you are running this script as non-root.