0
#!/usr/bin/env bash  
#### Extract OS-related info from a Linux box  #### 

#### Display header message ####
# $1 - message

function write_header(){
    local h="$@"
    echo "------------------------------"
    echo "  ${h}"
    echo "------------------------------"
}

#### Get info about Operating System ####

function  os_info(){
    write_header "System Info"
    echo "Operating System : $(uname --kernel-name)" #uname -s 
    echo "Kernel Version   : $(uname --kernel-release)"  #uname -r 
    awk '/^NAME=/||/^VERSION=/' /etc/os-release    
}    

In the above code, I can extract specific fields from uname and /etc/os-release that looks like this:

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

What I haven't been able to do, though is to add an awk pipe like this, e.g.: awk -F'=' '{print $2}, to the original awk statement. I'd like for output that looks like this for the last two lines: NAME: Debain GNU/Linux and VERSION: 8 (jessie).

Any suggestions on how to combine the awk statements to achieve the desired outcome?

1
awk -F'[="]+' '/^(NAME|VERSION)=/{printf("%-17s: %s\n",$1,$2)}' /etc/os-release

produce

NAME             : BunsenLabs GNU/Linux
VERSION          : 8.6 (Hydrogen)
  • That works EXACTLY as I'd like. Could you explain this portion: {printf("%-17s: %s\n",$1,$2)} of the code? – marshki Nov 30 '16 at 21:02
  • @marshki man 3 printf: "%-17s: %s\n" — use format( "-" left justified, "17" field wide, "s" field in string format, ": " as is, "%s" next field in string format, "\n" new line), "$1,$2" list fields to print – Costas Nov 30 '16 at 21:24
1

An alternative to the regex matching:

awk -F= '$1 == "NAME" || $1 == "VERSION" {print $2}'
0

The following appears to work for me:

awk -F'=' '/^NAME=/||/^VERSION=/ { print $2 }' /etc/os-release

Input:

~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.1 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.1 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial

Output:

~$ awk -F'=' '/^NAME=/||/^VERSION=/ { print $2 }' /etc/os-release
"Ubuntu"
"16.04.1 LTS (Xenial Xerus)"
  • Could you share the output of that command? I get this: ./os_info.sh: line 22: unexpected EOF while looking for matching ''` ./os_info.sh: line 28: syntax error: unexpected end of file – marshki Nov 30 '16 at 20:30
  • Certainly. Done and done. – DopeGhoti Nov 30 '16 at 20:33
  • sure. just add | sed 's/"//g' after the awk line. – DopeGhoti Nov 30 '16 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.