Getting OS name and version from Linx os-release file - linux

I'm attempting to get the OS name (pretty_name)and version (version_ID) on one line via the /etc/os-release file, in a simple command.
Currently, I have the following:
cat /etc/*-release | egrep "PRETTY_NAME|VERSION_ID" | cut -d = -f 2 | tr -d '"'
Which gives me the following output:
7.9
Red Hat Enterprise Linux
I'm looking to get my output looking like this:
Red Hat Enterprise Linux 7.9
What can I do to get the output the way I want it to be?

Since os-release is in shell format (and provided by your OS vendor, whom you're obliged to trust), you can just source it (which treats the quotes as syntax rather than data, so they don't need to be manually removed).
Amending to follow the advice from #Cyrus to prefer Red Hat's extended metadata over the generic variable names when available (but falling back to the generic names otherwise):
. /etc/os-release
printf '%s %s\n' \
"${REDHAT_SUPPORT_PRODUCT:-$PRETTY_NAME}" \
"${REDHAT_SUPPORT_PRODUCT_VERSION:-$VERSION_ID}"

Note; the order of the contents of /etc/*-release can't be guaranteed. This answer focus on OP's case;
Use tac to reverse the output, then use tr '\n' ' ' to concatenate the lines;
cat /etc/*-release | egrep "PRETTY_NAME|VERSION_ID" | cut -d = -f 2 | tr -d '"' | tac | tr '\n' ' '
# 10 Debian GNU/Linux 10 (buster)
Another option would be to read the second and third line of lsb_release witch has a more consistent output;
lsb_release -as 2>/dev/null | sed -n 2,3p | tac | tr '\n' ' '
# 10 Debian GNU/Linux 10 (buster)

Related

How can I query the number of the virtual desktop on which the bash script is running in Linux Mint via bash?

Environment:
Linux Mint, Cinnamon desktop manager, with multiple workspaces=virtual desktops, e.g. 4.
Bash script
What is known:
How to determine the number of workspaces:
wmctrl -d | wc -l
What I need:
Get the number of virtual desktops the bash script is running on with a pure bash as var (like with grep, not awk or similar) and echo the var.
With awk (imho still the most appropriate choice for the task at hand):
nr_of_active_workspace=$(wmctrl -d | awk '/\*/{print $NF}')
echo $nr_of_active_workspace
Or a pure bash solution:
nr_of_active_workspace=$(wmctrl -d | while read -r line; do [[ $line =~ '*' ]] && echo ${line: -1} ; done)
echo $nr_of_active_workspace
You can use POSIX shell features and the xprop(1) command to get both details with no other external utilities.
To get the ID number of the current/active desktop:
curdesk=$(xprop -root -notype _NET_CURRENT_DESKTOP)
curdesk="${curdesk##* }"
To get the count/quantity of desktops defined:
deskcnt=$(xprop -root -notype _NET_NUMBER_OF_DESKTOPS)
deskcnt="${deskcnt##* }"
Both depend on xprop(1) giving the answer in the form "foo = 0" (separated by spaces), and use shell pattern matching parameter expansion to match the longest substring ending in space, and remove it, leaving only the last token (the value after the equals sign).
Note that desktops are numbered from 0 (zero), so the count will be a number one higher than the ID number of the last desktop.
This should work with any window manager that adheres to the Extended Window Manager Hints (EWMH) specification (which is practically all of them, these days):
https://specifications.freedesktop.org/wm-spec/1.3/ar01s03.html
Follow a solution which need awk:
nr_of_active_workspace=$(wmctrl -d | grep "*" | awk '{print $11}')
echo $nr_of_active_workspace
It can be a solution without need awk, are possible on other way.
Based on answer of KamilCuk, its possible to output on follow way the line which is including the number of the active desktop:
nr_of_active_desktop=activedesktop=$(wmctrl -d | grep "*" | rev | cut -d ' ' -f1)
echo $nr_of_active_desktop

Download latest version of Slack automatically

I have a script that I download slack with the wget command, as the script runs every time a computer is configured I need to always download the latest version of slack.
i work in debian9
I'm doing it right now:
wget https://downloads.slack-edge.com/linux_releases/slack-desktop-3.3.7-amd64.deb
and I tried this:
curl -s https://slack.com/intl/es/release-notes/linux | grep "<h2>Slack" | head -1 | sed 's/[<h2>/]//g' | sed 's/[a-z A-Z]//g' | sed "s/ //g"
this return: 3.3.7
add this to: wget https://downloads.slack-edge.com/linux_releases/slack-desktop-$curl-amd64.deb
and not working.
Do you know why this can not work?
Your script produces a long string with a lot of leading whitespace.
bash$ curl -s https://slack.com/intl/es/release-notes/linux |
> grep "<h2>Slack" | head -1 |
> sed 's/[<h2>/]//g' | sed 's/[a-z A-Z]//g' | sed "s/ //g"
3.3.7
You want the string without spaces, and the fugly long pipeline can be simplified significantly.
barh$ curl -s https://slack.com/intl/es/release-notes/linux |
> sed -n "/^.*<h2>Slack /{;s///;s/[^0-9.].*//p;q;}"
3.3.7
Notice also that the character class [<h2>/] doesn't mean at all what you think. It matches a single character which is < or h or 2 or > or / regardless of context. So for example, if the current version number were to contain the digit 2, you would zap that too.
Scraping like this is very brittle, though. I notice that if I change the /es/ in the URL to /en/ I get no output at all. Perhaps you can find a better way to obtain the newest version (using apt should allow you to install the newest version without any scripting on your side).
echo wget "https://downloads.slack-edge.com/linux_releases/slack-desktop-$(curl -s "https://slack.com/intl/es/release-notes/linux" | xmllint --html --xpath '//h2' - 2>/dev/null | head -n1 | sed 's/<h2>//;s#</h2>##;s/Slack //')-amd64.deb"
will output:
wget https://downloads.slack-edge.com/linux_releases/slack-desktop-3.3.7-amd64.deb
I used xmllint to parse the html and extract the first part between <h2> tags. Then some removing with sed and I receive the newest version.
#edit:
On noticing, that you could just grep <h2> from the site to get the version, you can the version with just:
curl -s "https://slack.com/intl/es/release-notes/linux" | grep -m1 "<h2>" | cut -d' ' -f2 | cut -d'<' -f1

How to turn off auto add escape character feature in Linux

I am trying to pass a command to a remote server using ssh. while my commands have some characters like ", $, ', \ which often requires a backslash as a escape character except ' (single quote), but the system is automatically taking an escape character \ before the single codes while execution. Can some one help me how to turn off this.
OS : RHEL
my Code :
ssh -q $server "ps -ef | grep mongo | grep conf | awk '{print \$(NF-2)}'
While execution, the code becomes
ssh -q $server "ps -ef | grep mongo | grep conf | awk \'{print $(NF-2)}\'"
I need to turn off this feature
Your analysis isn't really correct. Anyhow, there is no particular reason to run Awk remotely, or grep at all here (because Awk does all of that nicely and efficiently with a very minor refactoring):
ssh -q "$server" ps -ef |
# This runs locally instead
awk '/mongo/ && /conf/ {print $(NF-2)}'

How to isolate part of a line of text in order to use it as a variable

I'm creating bash script and want to check version number of program from a line in a file and use it to make different checks and operations.
The version line in file is looking like this (examples):
Program Version 1.3
or
Program Version 1.3.1
It's on different lines in different versions but always follow the same syntax. How to remove the first part and isolate only the version number in order to put it in a variable?
Using GNU grep with -P for perl-style regEx match, and -o flag to return only the matching pattern.
grep -oP 'Program Version \K[^ ]*' file
1.3.1
To save it in a variable
versionNo="$(grep -oP 'Program Version \K[^ ]*' file)"
printf "%s\n" "$versionNo"
1.3.1
Use perl regEx itself,
perl -lne 'print "$1" if /^Program Version (\d.+)/' file
1.3.1
in variable,
versionNo="$(perl -lne 'print "$1" if /^Program Version (\d.+)/' file)" file
printf "%s\n" "$versionNo"
1.3.1
Using GNU sed
sed -r 's/Program Version ([[:digit:]].+).*/\1/' file
1.3.1
and
versionNo="$(sed -r 's/Program Version ([[:digit:]].+).*/\1/' file)" file
printf "%s\n" "$versionNo"
1.3.1
I guess you obtain that Program Version 1.3.1 for example launching some kind of command. Well, try this:
#!/bin/bash
version=$(yourCommandWhichShowsVersion 2> /dev/null | egrep "^Program Version [0-9]" | awk '{print $3}')
Explanation:
You need to launch the command which show the version
You redirect the output to egrep which search through all lines matching only which starts ^ <- this is to start string, with the desired text Program Version, and this [0-9] is to match one number. If you don't know if the version can be 1.3 or 1.3.1 or 1 that's all you need.
awk is going to "select" the second column (first is "Program version" and second is the version number)
Good luck!
Value=$(cat program.txt | grep Program\ Version\ | sed "s/Program \Version\ //g")
Just finds Program Version then strip it out with sed.
Edit: Sorry misread. Removed version number
I would just use grep and cut for this, since the pattern is fixed:
# find the first occurrence of the line (-m 1) starting with the pattern (^) and
# the third field (cut -f3) is the version
# this makes sure we ignore
# a) multiple occurrences of the pattern, if any
# b) occurrence of "Program Version" anywhere else on a line
# we make no assumption about the format of the version number
version=$(grep -m 1 "^Program Version " program.txt | cut -f3 -d' ')
if [[ -z $version ]]; then
# version not specified
# your exception handler here
fi
Some possible solutions for the problem:
awk awk '/Program Version/{print $3}' file.txt
grep grep -oP "Program Version \K[^ ]*" file.txt
sed sed -n '/Program Version /s///p' file.txt
perl perl -lane 'if (/Program Version/) {print $F[2]}' file.txt
(HTH) Hope This Helps.
Here is what I have used at the end:
Version=`find . -type f -name "filename" -exec grep -h 'Program Version' {} + | awk -F " " '{print $3}'`
I have used find + grep to receive the line with the version that I need and -f in awk to define field separator to be blank space and with that I have separated the version number from the rest of the results. Thank you all for your answers.

How to fetch Java version using single line command in Linux

I want to fetch the Java version in Linux in a single command.
I am new to awk so I am trying something like
java -version|awk '{print$3}'
But that does not return the version. How would I fetch the 1.6.0_21 from the below Java version output?
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
Java HotSpot(TM) 64-Bit Server VM (build 17.0-b16, mixed mode)
Redirect stderr to stdout.
Get first line
Filter the version number.
java -version 2>&1 | head -n 1 | awk -F '"' '{print $2}'
This is a slight variation, but PJW's solution didn't quite work for me:
java -version 2>&1 | head -n 1 | cut -d'"' -f2
just cut the string on the delimiter " (double quotes) and get the second field.
I'd suggest using grep -i version to make sure you get the right line containing the version string. If you have the environment variable JAVA_OPTIONS set, openjdk will print the java options before printing the version information. This version returns 1.6, 1.7 etc.
java -version 2>&1 | grep -i version | cut -d'"' -f2 | cut -d'.' -f1-2
Since (at least on my linux system) the version string looks like "1.8.0_45":
#!/bin/bash
function checkJavaVers {
for token in $(java -version 2>&1)
do
if [[ $token =~ \"([[:digit:]])\.([[:digit:]])\.(.*)\" ]]
then
export JAVA_MAJOR=${BASH_REMATCH[1]}
export JAVA_MINOR=${BASH_REMATCH[2]}
export JAVA_BUILD=${BASH_REMATCH[3]}
return 0
fi
done
return 1
}
#test
checkJavaVers || { echo "check failed" ; exit; }
echo "$JAVA_MAJOR $JAVA_MINOR $JAVA_BUILD"
~
You can use --version and in that case it's not required to redirect to stdout
java --version | head -1 | cut -f2 -d' '
From java help
-version print product version to the error stream and exit
--version print product version to the output stream and exit
Here's my variation (with thanks/acknowledgements to many answers prior to this). My goal is to produce a "Major Version" in line with JEP 223. Assumption are made; I don't know if it works across the board for all released (or to be released) versions of java.
java -version 2>&1 | fgrep -i version | cut -d'"' -f2 | sed -e 's/^1\./1\%/' -e 's/\..*//' -e 's/%/./'
Version
Results
1.6.0_65
1.6
1.8.0_282
1.8
11.0.10
11
15.0.2
15
This way works for me.
# java -version 2>&1|awk '/version/ {gsub("\"","") ; print $NF}'
1.8.0_171
Getting only the "major" build #:
java -version 2>&1 | head -n 1 | awk -F'["_.]' '{print $3}'

Resources