If I run this command:
awk -F'[="]+' '/^(NAME|VERSION)=/{printf("%-17s: %s\n",$1,$2)}' /etc/os-release
from a terminal, I can retrieve this:
NAME : Debian GNU/Linux
VERSION : 8 (jessie)
(note the formatting/spacing). However, when I try to assign this command to a local variable and call it, as I do in this function:
#!/bin/bash
#### Display header message ####
# $1 - message
function write_header(){
local h="$@"
echo "------------------------------"
echo " ${h}"
echo "------------------------------"
}
#### Get info about Operating System ####
function os_info(){
local namevers=$(awk -F'[="]+' '/^(NAME|VERSION)=/{printf("%-17s: %s\n",$1,$2)}' /etc/os-release)
write_header "System Info"
echo "Operating System : $(uname --kernel-name)"
echo "Kernel Version : $(uname --kernel-release)"
echo $namevers
}
os_info
My formatting get's mangled (see output after NAME):
------------------------------
System Info
------------------------------
Operating System : Linux
Kernel Version : 3.16.0-4-amd64
NAME : Debian GNU/Linux VERSION : 8 (jessie)
I can work around the formatting error by getting rid of the local variable, and calling awk on individual lines like this:
awk -F'[="]+' '/^(NAME)=/{printf("%-17s: %s\n",$1,$2)}' /etc/os-release
awk -F'[="]+' '/^(VERSION)=/{printf("%-17s: %s\n",$1,$2)}' /etc/os-release
but that looks a little clunky, and doesn't follow the structure of the larger script I am writing. Any suggestions on how to fix this?
Please note: I can't use the LSB module
because some machines I am testing this script on don't have that package installed. Also, things need to run without elevated privileges.