45.11 The Multipurpose jot CommandThe jot command is an extremely powerful tool for shell programming. People who have used jot end up using it all the time, but those who haven't been exposed to it may be perplexed by its function. For that reason, I think the best way to learn jot is by example. (If you've read many of my other articles, then you might notice that I think that example is the best way to learn anything; but for jot , it's doubly true!)
% Big deal, you might say. Well, it may not be earth-shattering on the surface, but it can make your life much easier if you program in the Bourne shell. Take the instance when you want to increment a number in a loop. The usual way of doing this is:
counter=1 while [ $counter -le 10 ] do ... counter=`expr $counter + 1` done This is laborious and quite slow. Because the Bourne shell doesn't have any built-in ( 1.10 ) number crunching, the only way to increment the counter is to use the expr command in each iteration of the loop. But if you have jot , the same loop can be written in a simple for loop:
for counter in `jot 10` do ... done You can also use jot to show any other sequence of numbers. For example, to show the integers between 24 and 28 (inclusive), try:
% The first argument ( 5 ) is taken to be how many values should be shown. The second number ( 24 ) is the number to start counting with. This may seem frustrating - why not just let us say "jot 24 28" to specify the beginning and end of the sequence, rather than make us figure out how many numbers will be in the sequence first? Well, the reason is that you might not always want to be counting by whole numbers. If supplied with a third argument, jot takes it as the number to end the sequence with. And if you specify either the beginning or end boundary with a decimal point, it will produce fractional numbers in its output:
% You can also use the -p option to specify a given precision:
% By default, the values shown are evenly spaced across the interval. You can change this by using a fourth numerical argument, which becomes the size of each step in the iteration. For example:
% Notice in this example that only the first four iterations are shown, because we asked for only four values in the first argument. This is because any three values determine the fourth automatically, so when the values conflict, jot takes the lower value. jot stopped after four values regardless of the fact that it would need nine iterations to complete the sequence. However, jot will also stop if the sequence is completed before the specified number of values are shown.
%
To omit any of these values, replace them with a single dash
(
% And of course, you can use negative numbers and negative steps:
% If you want the output separated by a string other than a newline, use the -s option. For example, to have the output of the previous command separated by spaces, enter:
% That's jot in its no-frills form, already potentially useful for any writer of shell scripts. However, jot does quite a lot more. The -c option can be used to show ASCII ( 51.3 ) characters instead of integers. To print out the character for ASCII 65 (decimal), for example, try:
% You can also do the ASCII-to-decimal conversion in reverse, by just specifying a character in place of the lower bound:
% This can be handy if you want an automatic listing of all 26 letters:
% The -r option produces random numbers, which is very useful in shells with no random number generator (such as the Bourne or C shells). To create a 6-digit random number, try:
% (Using -r , the fourth numerical argument, if specified, is taken to be a seed for the random number.) The -b option can be used to repeat a given word, much like the yes ( 23.4 ) command:
% The jot manual page suggests a clever way of using this feature: if you want to search for lines in files that have 40 or more characters, you could do this using regular expressions, but you'd have to count out all those dots.
grep "........................................" Using jot , you can pat yourself on the back for being ingenious (or for just reading the manpage!):
grep `jot -s "" -b . 40`
But the most powerful feature of
jot
comes with its
-w
option.
The
-w
option accepts a word containing format
conversion characters as used by the
printf( )
function.
(For example, This allows you to combine strings with jot output, a useful feature for manipulating temporary files within scripts. For example, suppose you have a shell script that creates multiple temporary files that you want to remove at the end of the script. You might have even created the files using jot earlier in the script, as shown previously:
for counter in `jot 10 1` do Then later on you want to remove the files. You could do another loop, but it's more efficient to just enter:
rm `jot -w tmp%d 10 1` The jot command expands to the strings tmp1 through tmp10 , which are the names of the temporary files created earlier in the script. - |
|