LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-29-2014, 12:26 AM   #1
diamond_D
Member
 
Registered: Jul 2005
Posts: 51

Rep: Reputation: 0
Bash while read loop ping script - help?


I'm trying to run a ping latency script provided by my VOIP provider but it is not working. I believe this issue is setting the variable that points to my txt file. Looks like the script fails on the cat command on line 17. Here is the code followed by the results.

Code:
  #!/bin/sh
  # Ping several servers and display Latency, Jitter and Packet Loss 
  #
  myHF="voip_ping_hosts.txt"
  # Sample file:
  #    toronto.voip.ms
  #    montreal.voip.ms
  #    seattle.voip.ms
  #    chicago.voip.ms
  #    newyork.voip.ms
  #
  echo "============================================"
  printf "%-20s %7s %8s %6s\n" "VoIP Server" "Latency" "Jitter" "Loss"
  echo "============================================"
  cat ${myHF} |\
  while read myLn
  do
     ping -c 3 -i 5 -q $myLn |\
     awk '/^PING / {myH=$2}
          /packet loss/ {myPL=$6}
          /min\/avg\/max/ {
             split($4,myS,"/")
             printf( "%-20s    %3.1f    %1.3f   %4s\n", myH, myS[2], myS[4], myPL)
         }'
  done
  echo "============================================"

Result:
[delslige@centos-vm01 scripts]$ ./pingvoip.sh
============================================
VoIP Server Latency Jitter Loss
============================================
: command not found 17:
: No such file or directory
./pingvoip.sh: line 29: syntax error: unexpected end of file
 
Old 07-29-2014, 01:52 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
use
Code:
#!/bin/bash
set -xv
....
at the beginning of your script to see what's happening.
 
Old 07-29-2014, 03:08 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
As you have only shown 26 lines and the final error is listed at line 29, this would indicate we are not seeing the entire file.
 
Old 07-29-2014, 06:39 AM   #4
diamond_D
Member
 
Registered: Jul 2005
Posts: 51

Original Poster
Rep: Reputation: 0
The script was only missing a few comments. Not sure what happened there. Here is the original. Ran it again but get the same results. I event tried the "set-xv" option as suggested by pan64 but it basically gave me the same result.

Code:
  #!/bin/sh
  # Ping several servers and display Latency, Jitter and Packet Loss 
  #
  # First, create a text file with all servers you want to ping - one host name per line. 
  # The list of voip.ms servers is available at http://wiki.voip.ms/article/Choosing_Server
  myHF="voip_ping_hosts.txt"
  # Sample file:
  #    toronto.voip.ms
  #    montreal.voip.ms
  #    seattle.voip.ms
  #    chicago.voip.ms
  #    newyork.voip.ms
  #
  echo "============================================"
  printf "%-20s %7s %8s %6s\n" "VoIP Server" "Latency" "Jitter" "Loss"
  echo "============================================"
  cat ${myHF} |\
  while read myLn
  do
     ping -c 3 -i 5 -q $myLn |\
     awk '/^PING / {myH=$2}
          /packet loss/ {myPL=$6}
          /min\/avg\/max/ {
             split($4,myS,"/")
             printf( "%-20s    %3.1f    %1.3f   %4s\n", myH, myS[2], myS[4], myPL)
         }'
  done
  echo "============================================"
Results
[delslige@centos-vm01 scripts]$ ./pingvoip.sh
============================================
VoIP Server Latency Jitter Loss
============================================
: command not found 17:
: No such file or directory
./pingvoip.sh: line 29: syntax error: unexpected end of file
 
Old 07-30-2014, 03:25 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
The idea behind set -xv is not to change the result but for you to be able to follow the code and see what variables have been set, what the code is doing and in your case where you are up to in your file.
 
Old 07-30-2014, 11:23 PM   #6
diamond_D
Member
 
Registered: Jul 2005
Posts: 51

Original Poster
Rep: Reputation: 0
New code with verbose debug. Still no luck.

Code:
  #!/bin/bash
  set -xv
  # Ping several servers and display Latency, Jitter and Packet Loss
  #
  # First, create a text file with all servers you want to ping - one host name per line.
  # The list of voip.ms servers is available at http://wiki.voip.ms/article/Choosing_Server
  myHF="voip_ping_hosts.txt"
  # Sample file:
  #    toronto.voip.ms
  #    montreal.voip.ms
  #    seattle.voip.ms
  #    chicago.voip.ms
  #    newyork.voip.ms
  #
  echo "============================================"
  printf "%-20s %7s %8s %6s\n" "VoIP Server" "Latency" "Jitter" "Loss"
  echo "============================================"
  cat ${myHF} |\
  while read myLn
  do
     ping -c 3 -i 5 -q $myLn |\
     awk '/^PING / {myH=$2}
          /packet loss/ {myPL=$6}
          /min\/avg\/max/ {
             split($4,myS,"/")
             printf( "%-20s    %3.1f    %1.3f   %4s\n", myH, myS[2], myS[4], myPL)
         }'
  done
  echo "============================================"


[delslige@centos-vm01 scripts]$ ./pingvoip.sh
./pingvoip.sh
+ ./pingvoip.sh
: invalid optionine 2: set: -
set: usage: set [--abefhkmnptuvxBCHP] [-o option-name] [arg ...]
============================================
VoIP Server Latency Jitter Loss
============================================
: command not found 18:
: No such file or directory
./pingvoip.sh: line 30: syntax error: unexpected end of file
printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
++ printf '\033]0;%s@%s:%s\007' delslige centos-vm01 '~/scripts'
[delslige@centos-vm01 scripts]$
 
Old 07-31-2014, 12:09 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
that was definitely another script
 
Old 07-31-2014, 12:31 AM   #8
diamond_D
Member
 
Registered: Jul 2005
Posts: 51

Original Poster
Rep: Reputation: 0
What do you mean? That was the script and the result. I have no idea why it is not running.
 
Old 07-31-2014, 01:46 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
how did you edit that script?
 
1 members found this post helpful.
Old 07-31-2014, 06:41 AM   #10
diamond_D
Member
 
Registered: Jul 2005
Posts: 51

Original Poster
Rep: Reputation: 0
Now we are on to something. It was edited in Notepad++ and I thought that the correct format would be saved by selecting file type - 'Unix script file' but after going back and looking at the characters, I had to change the EOL conversion over to Unix. Now I am not getting any errors but also no results. I think it is a DNS problem now which I will troubleshoot when I get the chance...Thanks!
 
Old 07-31-2014, 06:58 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
glad to help you. if you really want to say thanks just press yes
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
BASH: Read entire file line in for loop clinton Programming 16 04-18-2013 12:06 PM
bash script that read from file and ping remote host zunder1990 Linux - Newbie 4 12-19-2012 10:15 AM
BASH: Continuing while read loop with SIGINT drj000 Programming 6 08-10-2008 03:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration