0

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.

3 Answers 3

1

Ah, I see the problem in the script:

The variable is being split here:

echo $namevers

To fix it, do:

printf '%s\n' "$namevers"

Here's the output:

------------------------------
    System Info
------------------------------
Operating System : Linux
Kernel Version   : 3.16.0-4-amd64
NAME             : Debian GNU/Linux
VERSION          : 8 (jessie)
0

If I've understood correctly, this should produce the output you're after:

function  os_info(){
    write_header "System Info"
    echo "Operating System : $(uname --kernel-name)"               
    echo "Kernel Version   : $(uname --kernel-release)"             
    awk -F'[="]+' '/^(NAME|VERSION)=/{printf("%-17s:    %s\n",$1,$2)}' /etc/os-release
}

If you really want to use a variable, you need to quote it to preserve the formatting:

echo "${namevers}"
4
  • Sorry for the lack of clarity, but that solution does not first assign a local variable to "namevers". As I said, my problem arises when first trying to assign the local variable namevers and then later call it in the function. Commented Dec 10, 2016 at 16:06
  • Yes, but as far as I can see you don't need a variable at all! Commented Dec 10, 2016 at 16:08
  • But see my edit. Commented Dec 10, 2016 at 16:11
  • I tried that earlier, but it still mangles the output for me. I think I have an answer, though. Commented Dec 10, 2016 at 16:17
0

While this answer may not answer your question directly concerning the awkkung-fu, you also could just source the /etc/os-release and use the variables right away.

function  os_info() {
    . /etc/os-release
    write_header "System Info"
    echo "Operating System : $(uname --kernel-name)"               
    echo "Kernel Version   : $(uname --kernel-release)"             
    echo "NAME             : $NAME"
    echo "VERSION          : $VERSION"
}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.