UNIX Hints & Hacks

ContentsIndex

Chapter 1: Topics in Administration

 

Previous ChapterNext Chapter

Sections in this Chapter:

1.1 Collecting System Information

 

1.7 Swap on-the-Fly

 

1.13 Remove the ---- Dashes ----

1.2 Backup Key Files!

 

1.8 Keep It Up with nohup

 

1.14 echo Does ls

1.3 Execution on the Last Day of a Month

 

1.9 Redirecting Output to Null

 

1.15 Building Large Dummy Files

1.4 Dealing with Unwanted Daemons

 

1.10 Keeping Remote Users Out

 

1.16 Burning-in Disk Drives

1.5 Keep Those Daemons Running

 

1.11 Rewinding Tapes Fast

 

1.17 Bringing a System Down

1.6 fuser Instead of ps

1.12 Generating a Range of Numbers

 

 

1.12 Generating a Range of Numbers

1.12.1 Description

1.12.1 Description

This function script counts from and to a range of numbers.

Flavors: AT&T and BSD

Shell: sh

This script counts between a range of numbers that are passed through STDIN. It is sometimes necessary to generate a range of numbers to be used within a command or another shell script.

#! /bin/sh
LO=$1;HI=$2
while [ $LO -le $HI ]
do
 echo -n $LO " "
 LO=`expr $LO + 1`
done

Line 1: Define the shell being used.

Line 3: Set the variable $LO to the first argument passed to STDIN. Set the variable $HI to the second argument passed to STDIN.

Line 5: Process through the range of numbers until $LO is greater than $HI.

Line 7: Output the current number in $LO.

Line 8: Increment the number in $LO.

Line 9: Exit if $LO is greater than $HI; otherwise, continue with the next number in the range.

If the previous script is named count and executed

$ count 4 13
4 5 6 7 8 9 10 11 12 13

the script will count from 4 to 13 without any carriage returns.

Reason

This script is very useful. The output from the script can be passed to other scripts to enhance their functionality. Shell scripts do not have the capability to count within a range of numbers. This little function script provides this capability.

Real World Experience

As administrators, you are often faced with writing shell scripts that require some kind of loop within them. Performing a loop around a range of numbers can get messy if there is a large range involved. By using the counting script described previously, a lengthy for loop can be shortened to a call to the counting script.

Here is what you have to do when there is no counting script to call:

#! /bin/sh

STRING="The quick brown fox jumped really high"

for i in `echo "5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20"`
do
 echo $STRING | cut -c$i
done

Compare that to this, when the counting script is available:

#! /bin/sh

STRING="The quick brown fox jumped really high"

for i in `count 5 20`
do
 echo $STRING | cut -c$i
done

This script simply prints out each letter between the fifth and twentieth characters in the string on a line by itself. If the script is needed to process a large range of numbers, the first method of counting with an echo command isn't too efficient. This little function script is great for processing strings, characters, files, and directories--there are many possible uses for this script.

UNIX Hints & Hacks

ContentsIndex

Chapter 1: Topics in Administration

 

Previous ChapterNext Chapter

Sections in this Chapter:

1.1 Collecting System Information

 

1.7 Swap on-the-Fly

 

1.13 Remove the ---- Dashes ----

1.2 Backup Key Files!

 

1.8 Keep It Up with nohup

 

1.14 echo Does ls

1.3 Execution on the Last Day of a Month

 

1.9 Redirecting Output to Null

 

1.15 Building Large Dummy Files

1.4 Dealing with Unwanted Daemons

 

1.10 Keeping Remote Users Out

 

1.16 Burning-in Disk Drives

1.5 Keep Those Daemons Running

 

1.11 Rewinding Tapes Fast

 

1.17 Bringing a System Down

1.6 fuser Instead of ps

1.12 Generating a Range of Numbers

 

 

© Copyright Macmillan USA. All rights reserved.