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've made a simple script for rebooting my router and afterwards showing a progress bar until my computer has an internet connection again.

When running the script I get the following output:

The router is now rebooting...
############################################bin/reboot_router: line 48:  4758 Alarm clock: 14         ping -Q -c 1 -t 1 google.com >&/dev/null
#bin/reboot_router: line 48:  4761 Alarm clock: 14         ping -Q -c 1 -t 1 google.com >&/dev/null
#bin/reboot_router: line 48:  4763 Alarm clock: 14         ping -Q -c 1 -t 1 google.com >&/dev/null
#####bin/reboot_router: line 48:  4773 Alarm clock: 14         ping -Q -c 1 -t 1 google.com >&/dev/null
#bin/reboot_router: line 48:  4775 Alarm clock: 14         ping -Q -c 1 -t 1 google.com >&/dev/null
##bin/reboot_router: line 48:  4777 Alarm clock: 14         ping -Q -c 1 -t 1 google.com >&/dev/null
#

I want to suppress the Alarm clock: 14 thing from appearing, so the output becomes:

The router is now rebooting...
#######################################################

The relevant part of the script is:

#!/bin/bash

COLUMNS=$(tput cols)

# Reboot router code here

echo 'The router is now rebooting...'

min_time=60
max_time=120
start_time=$SECONDS
time=0
progress=0

until [[ $min_time -lt $time ]] && ping -Q -c 1 -t 1 google.com &> /dev/null; do
    let time=SECONDS-start_time
    let new_progress=COLUMNS*time/max_time
    let diff=new_progress-progress
    let progress=new_progress
    for((i=0; i<diff; i++)); do
        echo -n '#'
    done
    sleep 1
done

echo
share|improve this question
    
You have an error in your ping command (try running it without redirecting output), the -Q flag needs a value. –  terdon Oct 21 '13 at 16:00
    
@terdon Nope. My version of ping doesn't need/support an argument for -Q. It works fine without an argument and fails with an argument. –  Tyilo Oct 21 '13 at 21:05

1 Answer 1

up vote 1 down vote accepted

Pull the ping out into it's own function and do whatever you want in there with the results. At the end return a 0 or 1 depending on the status internally in the function.

ping_func(){
    results=$(ping -c 1 -t 1 google.com &> /dev/null)
    return $?
}

until [[ $min_time -lt $time ]] && ping_func; do
    let time=SECONDS-start_time
    let new_progress=COLUMNS*time/max_time
    let diff=new_progress-progress
    let progress=new_progress
    for((i=0; i<diff; i++)); do
        echo -n '#'
    done
    sleep 1
done

echo

Alternatively you can use a more elaborate case/switch within the function if you need to act differently when getting different statuses from the ping.

case "$status" in
    1) return 0 ## success   ;;
    0) return 1 ## fail      ;;
    *) ### do something else ;;
esac
share|improve this answer
    
That wouldn't work as I can't extract the exit code from ping then. –  Tyilo Oct 21 '13 at 14:09
    
@Tyilo - see updates –  slm Oct 21 '13 at 14:19
    
that doesn't work either as: 1) grep will wait for a full line, however the script outputs # on at a time and I want to see them 2) It would also remove the whole line including the previous # 3) I think ping does something like echo foobar > /dev/tty, which can't be blocked like that. –  Tyilo Oct 21 '13 at 14:39
    
@Tyilo - someone mentioned there was a typo in your ping command in the chatroom. -Q needs an arg. ping -Q <some num> -c 1 -t 1 google.com. –  slm Oct 21 '13 at 16:01
    
@Tyilo - 3rd time a charm? –  slm Oct 21 '13 at 16:13

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.