Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[SOLVED]: awk output as a variable

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
cds60601
df -h | participant
df -h | participant
Posts: 706
Joined: 2017-11-25 05:58
Location: Florida
Has thanked: 129 times
Been thanked: 60 times

[SOLVED]: awk output as a variable

#1 Post by cds60601 »

Hi all -

Please have a look at the syntax I currently use below:

Code: Select all

#!/bin/bash
os=`hostnamectl |grep "Operating System:" |awk '{print $3" "$5}'`
When ran, it produces the output, Debian 9 What I need it to do, is put print $3 into a variable that can be used in the variable $destdir
Any help would be ideal

Cheers
Chris
Last edited by cds60601 on 2018-04-08 22:42, edited 2 times in total.
Supercalifragilisticexpialidocious

peter_irich
Posts: 1403
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation
Been thanked: 11 times

Re: awk output as a variable

#2 Post by peter_irich »

Try it:

Code: Select all

#!/bin/bash
os=`hostnamectl |grep "Operating System:" |awk '{printf("%s %s",$3, $5)}'`
echo "$os"

User avatar
cds60601
df -h | participant
df -h | participant
Posts: 706
Joined: 2017-11-25 05:58
Location: Florida
Has thanked: 129 times
Been thanked: 60 times

Re: awk output as a variable

#3 Post by cds60601 »

Thank you. This does work pretty darned well for my overall use.
The next part of the puzzle, would be to take one piece of that data (for example just the value of $3 and assign just that value to a var?

Again, thank you for the response - its certainly neater than the kludge I came up with since this posting.

Cheers
Chris
Supercalifragilisticexpialidocious

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: awk output as a variable

#4 Post by Dai_trying »

You could get the two items in there own variables and join them if and when required

Code: Select all

#!/bin/bash
ver=`hostnamectl |grep "Operating System:" |awk '{print $5}'`
os=`hostnamectl |grep "Operating System:" |awk '{print $3}'`
osver="$os $ver"
echo "$os"
echo "$ver"
echo "$osver"

User avatar
cds60601
df -h | participant
df -h | participant
Posts: 706
Joined: 2017-11-25 05:58
Location: Florida
Has thanked: 129 times
Been thanked: 60 times

Re: awk output as a variable

#5 Post by cds60601 »

I appreciate the resonances, thank you so much!

I was working on this and kludged something similar - not as clean as these provided examples.
The end result (so far) was to have the code span only one or two lines several lines opposed several as the examples show above.
My original intention was to derive all vars from the base line as defined in os=
I don't know how complex this can/should be but the above examples will work well enough for my needs.

Again, I thank you both for the super fast replies. I'll mark this resolved but others are welcome to continue to chime in.

Cheers
Chris
Supercalifragilisticexpialidocious

Post Reply