Article 5FPPX if/else and for loop in the same line

if/else and for loop in the same line

by
gabrielpasv
from LinuxQuestions.org on (#5FPPX)
Hi Guys,

I have one problem. I know how to use a bash for loop in a single line, it's easy. But I need to use for loop and if/else statement in the same line. For example:

let's say I have a file.txt with the following content:

dev:blablabla
prod:blablalba
stg:blablabla

What I want to do is iterate through these lines and check if a variable is equal to dev, prod or stg.
This is what I am doing:

Code: for i in `cat file.txt` ; do if [ $i =~ ^dev:* ] then echo $i fi ; doneBut it's not working.
Can anyone here help me ?
Thank youlatest?d=yIl2AUoC8zA latest?i=qkNLcGsLpdE:TUCcDuJPTrA:F7zBnMy latest?i=qkNLcGsLpdE:TUCcDuJPTrA:V_sGLiP latest?d=qj6IDK7rITs latest?i=qkNLcGsLpdE:TUCcDuJPTrA:gIN9vFwqkNLcGsLpdE
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments