How to return the value of a specific os-release property?

#!/bin/bash

source /etc/os-release
export PRETTY_NAME
 
if
 ${PRETTY_NAME | cut -c 32}  = "(Tumbleweed) (x86_64)" ]]
then
echo "This is Tumbleweed"
else
echo "This is something else"
fi



Not sure if it’s progress, but I’ve also enclosed the entire operation in curley brackets as I described above.
Results in a “Bad substitution” error instead.
Googling that error isn’t helpful, results in solutions that say to explicitly declare bash shebang as you see I’ve already done.

TSU

This

${PRETTY_NAME | cut -c 32}

is pretty nonsense IMHO.

${...}

is parameter substitution where … is the parameter. Thus … must be the name of a parameter.
Now, PRETTY_NAME might be a parameter, but PRETTY_NAME | cut -c 32 is definitely not.

And it is even difficult to understand what

PRETTY_NAME | cut -c 32

should be.
PRETTY_NAME is a text string
cut -c 32 is a command
and | is a pipe.
Now a pipe can be put between two commands to pipe the standard output of the first command to the standard input of the next command. But there is no first command, only a string.

It could be that you want to have:

$(echo ${PRETTY_NAME} | cut -c 32)

That would output the value of PRETTY_NAME to standard out, which will be read by cut from it’s standard input. Then cut would do something with it (-c 32) and output something to it’s standard output. This string will then be put on the spot (because of the $(…) ).
Then you would have a left string in the comparrison, which will be compaired for equalness (=) with the right string.

But I have no idea if that is what you want. That is always the problem when people ask: is this command correct? Correct for doing what? When that is unknown, only the syntax can be checked.

Hi,

You can try

lsb-release -csd

or if you really want to use os-release

awk -F'"' '$1 ~ /^PRETTY_NAME/{print $2}' /etc/os-release

You also need to use $() and enclose the command to extract the output of the command and save it in a variable.


var1=$(awk -F'"' '$1 ~ /^PRETTY_NAME/{print $2}' /etc/os-release)
var2='(Tumbleweed) (x86_64)'

then the test


if  $var1 = *$var2* ]]; then
   foo
else
   bar
fi

Untested but you might want to play with it.

I’m not sure what all of the discussion is about.

I tried the obvious on my 13.2 system:


eval `grep VERSION_ID /etc/os-release`
echo $VERSION_ID

and got the output “13.2” (without the quotes).

When you want to know if there is anywhere in that file the word Tumbleweed:

grep -q Tumbleweed /etc/os-release && echo Yes

Hi,

Yes that is the quick and easy way to check if there is indeed a specific string in a file but the question just keep changing imo :slight_smile:

Yes, it depends on exactly what one is trying to achieve.

Ummm…
All really good stuff to understand why some of the test code didn’t work, thx. I realized more or less where the problems were but couldn’t identify them clearly enough to be able to come up with a solution.

Yes, didn’t actually state the main objective in the beginning which is to determine the openSUSE version programmatically.
More specifically, my specific situation needs to only differentiate whether an openSUSE is running TW or “not TW.”

At first, I assumed that would be possible by reading VERSION_ID so asked about that because it’s been a reliable variable in the past for all the stable openSUSE releases not realizing that with Tumbleweed the convention is significantly different… With TW every update defines a new complete version so any program that uses VERSION_ID expires approximately daily.

So, to satisfy the main objective as defined above and work for more than a day (actually the next “zypper up”) the essential static part of the string (exclude the id based on date) that identifies Tumbleweed in general must be extracted from one of the returned strings.

I suppose in retrospect it might also have been possible but not as elegant to test for every known stable openSUSE version since then VERSION_ID is a reliable indicator and then declare anything that doesn’t match would be guessed to be TW. But, aside from being inelegant it opens the door for some new fail in the future if some time another version number doesn’t match.

TSU

I’m pretty sure that the Tumbleweed VERSION_ID is always an 8-digit number, in the form YYYYMMDD. I don’t think any other opensuse version uses that format.

You can get the version number with facter, too. facter has got all data of the system and will be used for Puppet.

My version number in the shell:

sarah@linux-p095:~> echo $(facter operatingsystemrelease)
13.2


Use the command **facter **for seeing all available data and use it like above in the shell.

Hi,

That facter is new to me but i have one question why the echo ? and the use of $()

I guess because it shows how you can repeat those encapsulations endless

echo $(echo $(echo $(echo hurray)))

:wink:

What for a funny reason. rotfl!

No. I have used that, because I have used that so for assignments in scripting (without echo).
You are right. You don’t need $(command) in the shell. But it is very practical for longer commands in scripting.

There is definitely a lot of use for the $(…) construct, but using it inside an echo with no other text is a bit useless IMHO.