Link to home
Start Free TrialLog in
Avatar of Frank Helk
Frank HelkFlag for Germany

asked on

Bash - tricky string extraction ....

I'm stuck at a (presumalby simple) problem with bash. I want to extract the info from ntpq -p into separate variables. But I'm stuck at the point where I want to assign the tally code (the asterisk preceeding the first server address) to a variable. ntpq -p returns something like
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.20.1    .PZF.            1 u   36   64  377    1.123   -5.097   5.434
 192.168.30.2    .INIT.          16 u    -   64    0    0.000    0.000   0.000

Open in new window

This is my code by now:
i=192.168.20.1
ntpq -p > $TEMPFILE
TEMPSTRING=$(grep $i $TEMPFILE|gawk '{ print $1 }')
NTP_TALLY=${TEMPSTRING:0:1}
echo NTP_TALLY

Open in new window

I would expect that NTP_TALLY contains a single asterisk, but it contains a list of the files in the current directory - it seems that bash expands it to that. The variable TEMPSTRING contains "*192.168.20.1", as expected.

Any hint how to manage that I get the expected result ?
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Frank Helk

ASKER

Ooops - looks like I've looked at the wrong point ... thanks a lot !