Solved

Bash - tricky string extraction ....

Posted on 2014-09-30
2
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40351654
echo "$NTP_TALLY"

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

Author Closing Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

739 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