0

I need a single line bash command that takes a piped input and returns true or false depending if it match a string or not..

This seems to work for the true or false part:

test `expr match "Release:11.04" "Release:11.04"` = 13 && echo true || echo false

But I cannot get it to work properly with the other command's output..

What I need would be something like this:

test `expr match "`lsb_release -r`" "Release:11.04"` = 13 && echo true || echo false

So I tried with xargs, but I can't get it to work:

lsb_release -r | xargs -I {} test `expr {} "Release: 11.04"` = 13 && echo True || echo False

Also, if any of you happens to know a shorter way to achieve this, it would be greatly welcome.

3 Answers 3

3

You could use the $(cmd) syntax.

test $(expr match "$(lsb_release -r)" "Release:11.04") = 13 && echo true || echo false

Though on my system there is a tab in the output of lsb_release after ':', so you may want to doublecheck your checks.

And actually after fiddling a bit, I think this would be a nicer way with bash (tested on Debian system):

[[ "$(lsb_release -r)" =~ $'Release:\t6.0.2' ]] && echo true || echo false
Sign up to request clarification or add additional context in comments.

1 Comment

Or more portably [ "$(lsb_release -r | cut -f2)" = 6.0.2 ] && echo true || echo false
1

Why not this way:

[ `lsb_release -r | awk '{print $2}'` = "11.04" ] && echo true || echo false

BTW, if you need to use nested `` you can escape inner one with \ like that:

test `expr match "\`lsb_release -r\`" "Release:11.04"`

But it looks ugly. Using $() is more readable, but bit less portable.

Comments

0

This would be another simpler approach -

lsb_release -r | grep -q "11.04" && echo true || echo false

Basically, this greps the output of lsb_release -r for 11.04. -q in grep will return exit status zero if a match is found, which will echo true. false otherwise.

1 Comment

You don't need to look at grep's output, just use its exit status: lsb_release -r | grep -q "11.04" && echo true || echo false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.