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 you


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 you