Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I wrote this script to check if my dns server works fine or not, and if not change it. but it gives out error ./dns-ping.sh: line 15: ((: 3 < : syntax error: operand expected (error token is "< ")

where is the problem?

#! /bin/bash

loss_count=0
echo "--------------------------------------------------------------"
echo "++ DNS Ping Tester ++"
echo "--------------------------------------------------------------"
echo "please enter threshold:"
read $threshold
for timer in {1..2}
    do
        png=`ping -c 5 -q yahoo.com | grep -oP '\d+(?=% packet loss)'`
        let loss_count=$png+$loss_count
done
let loss_mid=loss_count/12
if (($loss_mid < $threshold)); then
#   ifdown eth0
    echo "less"
else
    echo "nameserver 8.8.8.8" > /etc/resolv.conf
fi
share|improve this question
    
You want to replace < with -lt, which stands for "less than". –  schaiba Apr 8 '13 at 15:25
1  
Looks like you executed this script using sh rather than bash. –  jordanm Apr 8 '13 at 15:34
    
@schaiba actually the < is perfectly valid bash when used inside (( )). –  Patrick Apr 8 '13 at 15:39

1 Answer 1

up vote 1 down vote accepted

You have to replace the following line:

if (($loss_mid < $threshold)); then

By this one:

if [ "$loss_mid" -lt "$threshold" ]; then
share|improve this answer
    
now it gives this error: ./dns-ping.sh: line 15: [: : integer expression expected –  Hojat Taheri Apr 8 '13 at 17:50
    
This modification works for me. $threshold has to be a numeric variable. –  user36782 Apr 8 '13 at 18:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.