bash script

adds certain values in string

Page 1 of 1

5 Replies - 810 Views - Last Post: 24 May 2010 - 12:24 AM Rate Topic: -----

#1 kyousuke805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 27-November 09

bash script

Posted 23 May 2010 - 10:44 PM

Hello everyone. I am trying to parse through an input line for the string ttl and then adding the numbers that follow. Basically I take the output of the shell command ping, go through to the ttl, then add the numbers that follow. Here is my code:


#!/bin/bash

#get input from user
echo -n "Please enter the output of ping: "
read -e PING
divisor = 0
sum = 0
for i in $(PING); do
        if [i == "t"]
        #also check if i+1 is a t and i+1 is an l
        #this part I am not sure how to do
        divisor = divisor + 1
        sum = sum + i
done
echo "The average ttl is "
echo (sum/divisor)
echo "ms"
quit



Any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: bash script

#2 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5072
  • View blog
  • Posts: 26,471
  • Joined: 10-May 07

Re: bash script

Posted 23 May 2010 - 11:10 PM

For starters your variables are being referenced wrong & you don't have an fi to end your if.

For example :

for i in ${PING}
do
        if [ ${i} == "t" ]
        #also check if i+1 is a t and i+1 is an l
        #this part I am not sure how to do
        divisor = ${divisor} + 1
        sum = ${sum} + i
        fi
done


Was This Post Helpful? 1
  • +
  • -

#3 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5072
  • View blog
  • Posts: 26,471
  • Joined: 10-May 07

Re: bash script

Posted 23 May 2010 - 11:19 PM

I'm not sure exactly what you are trying to trigger by checking for lower case t, but running the following will at least show you how the script is working, compared to how you expect it to work :

#!/bin/bash

#get input from user
echo -n "Please enter the output of ping: "
read -e PING
set divisor = 0
set sum = 0
for i in ${PING}
do
        echo ${i}
        if [ "${i}" = "t" ]; then
          #echo ${i}
          divisor = `expr ${divisor} + 1`
          sum = `expr ${sum} + ${i}`
          echo "The average ttl is "
          echo ${sum} ${divisor}
          echo "ms"
        fi
done

Was This Post Helpful? 1
  • +
  • -

#4 kyousuke805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 27-November 09

Re: bash script

Posted 23 May 2010 - 11:20 PM

Thank you for the suggestion. It appears that I also have a problem when I initialized my variables. I really don't know what the problem is because I have heard that all you have to do is declare a name and assign in. Also I get an error that says there is an unexpected token before fi. Can you please explain to me what that means? Thank you, and sorry for asking silly questions.
Was This Post Helpful? 0
  • +
  • -

#5 kyousuke805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 27-November 09

Re: bash script

Posted 23 May 2010 - 11:39 PM

Okay, now I have it all working I think except I cannot get the value res to be shown in the output. Do I have to put a dollar sign somewhere?

#!/bin/bash

#get input from user
echo -n "Please enter the output of ping: "
echo ""
read -e PING
set divisor = 0
set sum = 0
for i in $(PING) 
do
        if [${i} == "t"]; then
        #also check if i+1 is a t and i+1 is an l
        #this part I am not sure how to do
        divisor = 'expr ${divisor} + 1'
        sum = 'expr ${sum} + i'
        fi
done
set res = 'expr ${sum}/${divisor}'
echo "The average ttl '/res' ms"




Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5072
  • View blog
  • Posts: 26,471
  • Joined: 10-May 07

Re: bash script

Posted 24 May 2010 - 12:24 AM

the variable res does not hold a value because the variable i is never equal to the letter t.

The code that I posted previously shows you why that is so. When you run the for loop through the variable PING's value, each instance is an entire word. No word is only the letter t, thus the if condition is never met.

Also you set res, but you never echo it out.

Lastly, I'm not certain why you 'undid' my correction to your if statement. The if conditional check in bash shell uses a single equal sign for checking character values.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1