Wednesday, November 17, 2010

Bash for loops with a series of numbers or letters

Bash scripting is great. Here is a quick trick to funnel a series of numbers (or letters) into a variable, and thus an argument of a script:


#!/bin/bash
for a in {1..18}
do
echo "The number $a"
done


#!/bin/bash
for a in {a..z}
do
echo " The letter $a "
done

Here is another example:

for a in {0..15};  do  /usr/sbin/xm vcpu-pin 0 $a 0-1,4-15;  done


Such a quick and easy trick!

No comments: