0

I want to display a message when the variable fl is not a valid filename or directory. Seems as though there is a problem with the construct below.

if [[ ! -f "$fl" || ! -d "$fl" ]]; then
  printf '%s\n' "$fl: File or Directory does not exist"
fi
Dilna
  • 405
  • 1
  • 6
  • `||` should be `&&` – Barmar Nov 12 '21 at 20:28
  • You can just use `-e`, it tests any kind of directory entry. – Barmar Nov 12 '21 at 20:28
  • You want `&&`, not `||` (or better yet, use `-e` to see if there's *something* by that name, instead of separately testing `-f` and `-d`). See ["Bash multiple ors don't work with negatives"](https://stackoverflow.com/questions/59858793/bash-multiple-ors-dont-work-with-negatives), ["Why non-equality check of one variable against many values always returns true?"](https://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true), and more generally [De Morgan's laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws). – Gordon Davisson Nov 12 '21 at 20:30
  • You may want this: `if ! [[ -f $fl || -d $fl ]]; then`... – M. Nejat Aydin Nov 12 '21 at 21:19
  • @Poati `-a` can be ambiguous (because it also means "and"); see [this question](https://stackoverflow.com/questions/24434441/why-does-negated-file-existence-check-in-bash-return-weird-results-in-case-of-ex). Use `-e` to check for existence of filesystem objects. – Gordon Davisson Nov 13 '21 at 11:17

1 Answers1

0
if [ ! -e $fl ]; then
  printf '%s\n' "$fl: File or Directory does not exist"
fi

If you give it a filename remember to include the extension

martindale
  • 20
  • 5