RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Bash reverse counting with while loop

Question:

Hi,

I have a question for my bash scripting.
Can you help me by this script?

Thanks Chris

(Modify countdown such that it prompts user for a starting value, and counts down from there.

Write a script called countdown2 that accepts the initial value as a command-line argument. For example, the command and its output might look like the following.

$ ./countdown2 12
12
11
10
9
8
7
6
5
4
3
2
1
GO!

Answer:

Probably the easiest way to do count reverse in bash is to use while loop. Save this script as countdown2, make it executable and run:

#!/bin/bash
COUNT=$1
# bash while loop
while [ $COUNT -gt 0 ]; do
        echo $COUNT
        let COUNT=COUNT-1
done
echo GO!

Output:

./countdown2 4
4
3
2
1
GO!

 Linux questions and answers

Share this linux post:

Submit Bash reverse counting with while loop in Delicious Submit Bash reverse counting with while loop in Digg Submit Bash reverse counting with while loop in FaceBook Submit Bash reverse counting with while loop in Google Bookmarks Submit Bash reverse counting with while loop in Stumbleupon Submit Bash reverse counting with while loop in Technorati Submit Bash reverse counting with while loop in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download