flash
flash

Reputation: 1519

assign a value to a variable basis on grep response

I need to check for a substring in a string. If present then assign some value to FILE variable and if not present then assign some other value to same FILE variable.

Below is what I have tried:

FILE="$(hostname -f | grep ".dev." &&  echo "data.txt.pp" || echo "data.txt")"; echo $FILE

But in the output of $FILE variable:

hostname.corp.com data.txt.pp

I see hostname is also being printed out. I just want either data.txt.pp or data.txt. If hostname has .dev. in it then I want FILE variable to be data.txt.pp otherwise it should have data.txt. What is wrong I am doing?

Upvotes: 0

Views: 32

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185274

Add -q :

hostname -f | grep -q "\.dev\." && echo "data.txt.pp" || echo "data.txt"

Avoid using UPPER CASE variables, they are reserved for system use

Upvotes: 1

Related Questions