Being Slightly Smarter With seq

This post comes from my ever-growing file of ‘how the hell did I not know that’ issues.

On the cluster at work we have a bunch of machines with names like mysite12 or mysite237. So quite often I’m writing shell scripts to loop through all these boxes to get info. So I do something like this:

for number in `seq 12 256`
do
    node=mysite$number
    echo $node
done

Which produces

mysite12
mysite13
.....
mysite256

It occurred to me today that this is such a staggeringly common thing to do that seq probably has a way of doing it already. Sure enough after reading the man page it turns out that you can hand seq a PRINTF style format command. So I can create my node names purely in seq

for node in `seq -f "mysite%g" 12 255"
do
    echo $node
done


mysite12
mysite13
....
mysite255

How has it taken me ten years to work that out?

One thought on “Being Slightly Smarter With seq”

  1. Note that recent versions of bash include the ‘{X..Y}’ construct which you can use instead of seq in a lot of situations. I don’t think it has printf support life this, though.

Leave a Reply

Your email address will not be published. Required fields are marked *