Solved

Bash - tricky string extraction ....

Posted on 2014-09-30
2
355 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

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

Question has a verified solution.

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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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

630 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