LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2013, 02:37 PM   #1
Jsaba2
LQ Newbie
 
Registered: Jan 2013
Posts: 14

Rep: Reputation: Disabled
Bash countdown help


So I have a script that is turning off some computers in my network and I have a countdown going, written like this:


echo "Shutting down all instructor machines";
echo "5...";
sleep 1;
echo "4...";
sleep 1;
echo "3...";
sleep 1;
echo "2...";
sleep 1;
echo "1...";
sleep 1;

echo "Waiting for machines to power down, please wait..."

network_admin -shutdown machine_list_instr



I am fully aware that this is a shitty way to do a countdown, is there a more, uh, elegant way to make this countdown?
 
Old 01-23-2013, 03:33 PM   #2
bipinajith
LQ Newbie
 
Registered: Jan 2013
Posts: 1

Rep: Reputation: Disabled
Here is one way of doing it by using a while loop. I have also used \c to suppress trailing newline. You can remove if you don't want them.

Code:
#!/bin/bash
echo "Shutting down all instructor machines";
i=5
while [ $i -ge 0 ]
do
        [[ $i -ne 0 ]] && echo -e "${i}...\c";
        [[ $i -eq 0 ]] && echo -e "${i}\n"
        i=$(( i - 1 ))
        sleep 1
done
 
1 members found this post helpful.
Old 01-23-2013, 04:56 PM   #3
antegallya
Member
 
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109

Rep: Reputation: 42
Another flavor
Code:
#!/bin/sh
echo "Shutting down all instructor machines"
for i in $(seq 5 -1 1)
do
  echo -ne "$i...\r"
  sleep 1
done
echo "Waiting for machines to power down, please wait..."
network_admin -shutdown machine_list_instr
 
1 members found this post helpful.
Old 01-23-2013, 07:12 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
#!/bin/bash
echo "Shutting down all instructor machines";
for i in  {5..1}
do
    echo -n "$i ... "
    sleep 1
done
echo "Waiting for machines to power down, please wait..."
network_admin -shutdown machine_list_instr
 
1 members found this post helpful.
Old 01-23-2013, 07:18 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Moved: This thread is more suitable in the Programming forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
1 members found this post helpful.
Old 01-23-2013, 07:28 PM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Code:
#!/bin/bash

function countdown {
    local -i S=$1
    for (( ; S > 0; --S )); do
        echo "${S}..."
        sleep 1s  # read would do but it accepts input.
    done
}

echo "Shutting down all instructor machines.";
countdown 5
echo "Waiting for machines to power down, please wait..."
network_admin -shutdown machine_list_instr
 
1 members found this post helpful.
Old 01-24-2013, 02:19 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
here's mine:

Code:
countdown()
{
    (( $# > 0 )) || return
    count=$1
    while (( count ))
    do
        printf "\033[K\r$count"
        sleep 1
        (( count -- ))
    done
}
 
1 members found this post helpful.
Old 01-24-2013, 03:34 AM   #8
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 275Reputation: 275Reputation: 275
Code:
#!/bin/bash

echo "Shutting down all instructor machines"
i=5

while true
do
  echo "${i}.."
  sleep 1
  i=$(($i-1))

  if [[ $i -eq 0 ]]; then
          break
  fi

done

echo "Waiting for machines to power down, please wait..."
 
2 members found this post helpful.
Old 01-24-2013, 06:58 AM   #9
Jsaba2
LQ Newbie
 
Registered: Jan 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Oh wow, Thank you guys so much! Seeing all these different methods really shows me some options I have when working with shell script, you guys rule! Props all around!
 
  


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
[SOLVED] bash: countdown fatalerror0x00 Programming 6 11-05-2012 04:46 PM
countdown clock timl Linux - General 2 04-11-2011 01:52 PM
Countdown while booting the CD could be helpful pxumsgdxpcvjm Slackware 4 06-19-2006 07:47 PM
Countdown clock - how hard could it be? krock923 Linux - Software 1 07-21-2004 11:07 PM
Countdown to the new LOTP isajera General 26 03-03-2003 09:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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