Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A primer on using 'jot' in UNIX UNIX
I am new to Mac OS X but come from the FreeBSD world so I'm having fun finding out what is here and what isn't. I just realized that one of my favorite tools, 'jot', is here and I wanted to point this out to people. jot allows you to print sequential and random data. More specifically, it allows you to do some really cool things in the shell. Let me give you an example.

I am a big fan of the online music trading organization Etree. There are well-established ways of organizing and naming the concerts that are traded. Occasionally I'll come across a downloaded show that doesn't follow the scheme. Say there are several songs from a Phish show named something like '2001-05-26-phish-d1t1.shn'. The correct format should be 'ph01-05-26d1t1.shn' It would be a real pain in the ass to rename them, right? Well, with jot the answer is a resounding "NO!"

Read the rest of this article for a great primer on using 'jot'...

First, let's look at some basics. Open up a terminal shell and type: "jot 3 1" and hit enter. What you'll see is this:
1
2
3
What those basic options did was to generate a series of numbers (3) beginning at 1. Try something 'jot 5 2' or 'jot 10 20'. You may wonder what is so special about that. I'm going to take a monstrous leap here and jump right into some shell programming (if you aren't familiar with it, I encourage you to find some online documentations, buy a book or something). Please also note that the syntax used here is of the Bourne shell variety. If you don't have 'bash' installed, when you open up a terminal window, type 'sh' and then enter to run a Bourne shell. With modified syntax, these same examples could run under csh/tcsh.

While generating numbers themselves might not be so interesting, when fed into other commands, they can help you do quite a bit. Try this from your shell:
for i in `jot 10 1`; do echo $i; done
It looks basically like the normal jot output. But now try:
for i in `jot 10 2`; do echo "I wish I had $i dollars"; done
Notice how easy it was to integrate text and a sequence of numbers? Perhaps now you are beginning to see the power of jot. Let's look at some other features before tackling our renaming problem.

The -b option:
Say you need to generate data for a file to be read by another program. If you want the same thing to be generated over and over (like the 'yes' command if you are familiar with it), you can say:
jot -b Input 10 1 > inputfile
If you type 'cat inputfile' you'll see that the word 'Input' has been generated ten times. This is a useful way to generate files that need to be of a certain size or to test boundary conditions, buffering, etc.

The -r option:
Say you want to fill a file with random numbers (don't count on them being cryptographically secure!), you can say:
jot -r 10 1 > randomdata
now type 'cat randomdata' to see the results.

The -w option:
Finally, this option works a little like the C printf function in that it takes a formatting string. A simple substitution is to use the %d token to represent a number. Try typing:
jot -w "file%d" 10 1
You should see:
file1
file2
file3
file4
.
.
file10
If you want to specify leading zeros and two digits you would say:
jot -w "file%02d" 10 1
(n.b. leave off the zero for a space-padded number that takes up two digits).

So, let's put it all together and get back to the example. Our source files are named '2001-05-26-phish-d1t1.shn' and we want them to become 'ph01-05-26d1t1.shn'. We notice that the variance will be in the track number (t1 t2 t3) but that the mapping is otherwise the same. So, we could simply say:
for i in `jot  10 1`; do mv 2001-05-26-phish-d1t$i.shn ph01-05-26d1t$i.shn; done
What would have been cumbersome to do manually becomes a snap with this great tool. If we then want to unshorten only the first disc's files and encode the resulting .wav files as .mp3s, we could say:
for i in `jot -w "ph01-05-26d1t%d"; do shorten -x $i.shn $i.wav; rm $i.shn; bladenc -del $i.wav; done
In one swell foop, you've combined several powerful commands into a sequential and specific flow that takes only seconds to type.

Play around with jot and get used to what it can do. You'll find that it becomes a very useful tool to have on hand when you need it. If you are new to shell programming, I encourage you to exercise caution when mv'ing, rm'ing or otherwise potentially losing data. Getting the arguments wrong to a tool like jot can result in situations you did not intend.

I hope you find this tool as useful as I do. Don't forget to check out all the great music on Etree, too!
    •    
  • Currently 5.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[14,995 views]  

A primer on using 'jot' in UNIX | 5 comments | Create New Account
Click here to return to the 'A primer on using 'jot' in UNIX' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Bash shell and jot
Authored by: el bid on Nov 19, '01 02:40:22AM

> Please also note that the syntax used here is of the Bourne
> shell variety. If you don't have 'bash' installed, when you
> open up a terminal window, type 'sh' and then enter to run
> a Bourne shell.

Useful takedown on jot, but the above doesn't seem to make a lot of sense. If you don't have Bash installed, you don't have Bash installed. What you'll get carrying out the instructions above is whatever is linked to /bin/sh, which in the default Mac OS X system is tcsh.

If you _do_ happen to have Bash installed, but not running as the current user's default shell, you're better off running /<pathtoBash> rather than just sh.

--
el bid



[ Reply to This | # ]
Bash shell and jot
Authored by: pmccann on Dec 01, '01 12:14:24PM

This can't be correct or our beloved osx boxes wouldn't boot! All 'dem shell scripts executed at startup (all with "#!/bin/sh" as their opening play) would just vanish in a flash of incompatible syntax.

/bin/sh can't be tcsh, but is of course ye olde worlde Bourne shell (as distinct from bash).

The shell scripts listed in the article should work fine post entering "/bin/sh" (or even just "sh" if you haven't changed your default PATH).

Just for the archive!

Paul



[ Reply to This | # ]
Bash shell and jot
Authored by: bsletten on Dec 02, '01 05:04:12PM

You are correct, that didn't make a whole lot of sense, but it should still work. I guess I was thinking that anyone with a leaning toward Bourne shells would have installed bash (as opposed to the default shell with OS X). bash and sh have the same basic programmatic syntax at least as far as the examples were concerned. I was anticipating that people playing around with it might get confused typing the shell constructs into a csh interpreter and miss the larger issue of learning jot. I also expected them to play around interactively so I wasn't worried about making sure they built scripts properly....

Oh well.



[ Reply to This | # ]
CLI info great, but....
Authored by: pecosbill on Nov 19, '01 04:34:05PM

Of course, everything depends on your vantage point, but there are many of us who would rather depend on a GUI tool than a CLI. File Buddy is more than happy to help with your file naming problems and it's a lot easier than trying to remember how to script the shell & use jot. Of course, if that's your style, more power to you.


There are other GUI utilities out there with the same ability whose names escape me.



[ Reply to This | # ]
...with sed...
Authored by: louisg on Nov 26, '01 05:41:10PM

>for i in `jot 10 1`; do mv 2001-05-26-phish-d1t$i.shn ph01-05-26d1t$i.shn; done

You could also have...

for i in `ls 2001-05-26-phish-d1t*shn` ; do
echo $i | sed 's/2001-05-26-phish-d1t/ph01-05-26d1t/' | read newname
mv $i $newname
done

This method may work better in general because we don't always have filenames that contain sequential numbers.

The guys still using File Buddy will notice that these classic Mac programs have a long way to go before they can match the power of the shells, sed, awk and yes, lets not forget, jot!

Louis



[ Reply to This | # ]