Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

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 ?
0
frankhelk
Asked:
frankhelk
1 Solution
 
woolmilkporcCommented:
echo "$NTP_TALLY"

means: Enclose the variable in double quotes.
0
 
frankhelkAuthor Commented:
Ooops - looks like I've looked at the wrong point ... thanks a lot !
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Tackle projects and never again get stuck behind a technical roadblock.
Join Now