M. Pollino
M. Pollino

Reputation: 45

Check if something is not a directory in Linux

I am trying to write an if statement where it will go to the next iteration of the for loop if i is the variable or if i is a directory. I am struggling with the directory part which would go after the -o

for i in *
    if [ "$i" = VARIABLE -o ]
    then
        continue
    fi

Upvotes: 0

Views: 188

Answers (2)

Toby Speight
Toby Speight

Reputation: 30831

You can use test -d:

if [ "$i" = VARIABLE ] || ! [ -d "$i" ]
then continue
fi

Or more succinctly:

[ "$i" != VARIABLE ] && [ -d "$i" ] || continue

Or as separate tests:

[ "$i" != VARIABLE ] || continue
[ -d "$i" ] || continue

Upvotes: 0

William Pursell
William Pursell

Reputation: 212248

I don't fully understand the question, but a common idiom is:

for i in *; do
  if test -d "$i"; then continue; fi
  ...
done

If you also want to compare against a particular value, you really shouldn't use -o. It's been deprecated for a long time. Instead, use:

if test -d "$i" || test "$i" = VARIABLE; then ...

Upvotes: 1

Related Questions