Solved

Bash - tricky string extraction ....

Posted on 2014-09-30
2
327 Views
Last Modified: 2014-09-30
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
Comment
Question by:frankhelk
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
echo "$NTP_TALLY"

means: Enclose the variable in double quotes.
0
 
LVL 13

Author Closing Comment

by:frankhelk
Comment Utility
Ooops - looks like I've looked at the wrong point ... thanks a lot !
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now