Waleed93
Waleed93

Reputation: 1300

How to Write For Loop and If Statement In One Line in Bash

I have a following case where I want to execute if/elif/else bash conditional statement in one line over each loop.

for file in *; do
   if[ ! -d "$file" ]; then 
      continue;
   elif[ -f "$file" ]; then 
     echo "$file";
   else 
      exit 1
   fi
done

I tried to write it in one line:

for file in *; do if[ ! -d "$file" ]; then continue; elif [ -f "$file" ]; then echo "$file"; else exit 1; fi; done

but it is giving unexpected syntax error: 'then' error

Where am I getting it wrong?

Upvotes: 0

Views: 44

Answers (0)

Related Questions