LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-10-2011, 08:39 PM   #1
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Rep: Reputation: 0
beginner script errors 2


Hi guys,

my scripts is not giving me the desired output and i can't seem to figure out why.

all its doing is pinging a host and based on the exit staus print a few lines.

here's the code:
Code:
#!/bin/bash
#Purpose: to ping the gateway 5 times 

STATUS=1

COUNT=0

until [[ $STATUS -eq 0 ]]
do
  ping -c 1 192.168.0.45
  RETURNVAL=$?  
  echo "return value: $RETURNVAL"

if [[ $RETURNVAL -eq 0 ]]
  echo "entering loop if return value is 0"
  then 
   echo "gateway reachable"  
    let "COUNT+=1"
   echo "count: $COUNT"
   sleep 3
  elif [[ $RETURNVAL -ne 0 ]]
  echo "entering loop if return value is 1"   
  then 
    echo "$?"
    echo "gateway unreachable"   

   fi
 
  if [[ $COUNT -eq 5 ]] 
  then
  STATUS=0
  fi
done


the issue i'm having is in the following:

Code:
ping -c 1 192.168.0.45
  RETURNVAL=$?
the variable RETURNVAL holds the value 1 (host 192.168.0.45 doesn't exist) but it keeps entering the loop for
Code:
if [[ $RETURNVAL -eq 0 ]]
so my output looks like this:

Code:
PING 192.168.0.45 (192.168.0.45) 56(84) bytes of data.
From 192.168.0.20 icmp_seq=1 Destination Host Unreachable

--- 192.168.0.45 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms

return value: 1
entering loop if return value is 0
gateway reachable
count: 1
can somebody please tell me why this is happening..? thanks!
 
Old 09-10-2011, 11:20 PM   #2
tbrand
Member
 
Registered: Jul 2006
Location: Toronto, Canada
Distribution: gentoo
Posts: 33

Rep: Reputation: 17
The ``then'' statement must immediately follow the ``if'' statement.

That's why I usually use the following style:

Code:
if [[ $RETURNVAL -eq 0 ]]; then
  echo "entering loop if return value is 0"
  echo "gateway reachable"  
  let "COUNT+=1"
  echo "count: $COUNT"
  sleep 3
elif [[ $RETURNVAL -ne 0 ]]; then
  echo "entering loop if return value is 1"   
  echo "$?"
  echo "gateway unreachable"   
fi
 
Old 09-10-2011, 11:22 PM   #3
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Try moving the lines

echo "entering loop if return value is 0"
echo "entering loop if return value is 1"

AFTER the then statement.
 
Old 09-11-2011, 02:27 AM   #4
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Thanks a lot guys, that was a fundamental error on my part. cheers!
 
Old 09-11-2011, 06:14 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The bracket tests are really designed for use with string values. In bash, when testing numerical values such as exit codes, the arithmetic operator is recommended.

Code:
if (( RETURNVAL == 0 )); then
Or since 0 evaluates as false in numeric tests, and !0 evaluates as true (some value exists), you can even just do this:
Code:
if (( RETURNVAL )); then
This does reverse the logic of the if construct, however.

There's also no need to do use elif here, since the result is just the inverse of the first test. "else" only will do. elif is only needed when you have two or more independent tests.

Code:
if (( RETURNVAL )); then
	echo "entering loop if return value is 1"   
	echo "$?"		#see the note below about this line
	echo "gateway unreachable"   
else
	echo "entering loop if return value is 0"
	echo "gateway reachable"  
	(( COUNT+=1 ))
	echo "count: $COUNT"
	sleep 3
fi
This doesn't do what you think it does, however.
Code:
  echo "entering loop if return value is 1"   
  echo "$?"
$? will always be "0" here, because it contains the exit code of the previous command, which is echo.

Edit: Come to think of it, a case construct would also be a viable option here.
Code:
case "$RETURNVAL" in
	0) echo "entering loop if return value is 0"
	   echo "gateway reachable"  
	   (( COUNT+=1 ))
	   echo "count: $COUNT"
	   sleep 3
	   ;;
	*) echo "entering loop if return value is 1"   
	   echo "gateway unreachable"
	   ;;
esac

Last edited by David the H.; 09-11-2011 at 06:29 AM. Reason: as posted
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner script errors greenpool Linux - Newbie 9 09-04-2011 12:39 PM
reading a value from a textfile using bash script (beginner problem) AndrewJS Linux - Newbie 1 06-10-2011 07:16 PM
Beginner question, Bash-script - date kickarzt Programming 15 03-10-2010 12:18 PM
Multiple Errors in C program (beginner) circuit_girl Programming 9 09-19-2006 03:47 AM
Beginner on Scilab, matlab script conversion munichtexan Linux - Software 6 12-08-2005 06:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:33 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