Here is a succinct use of the for loop using the robust Z Shell. See ZSH is cool: Brace Expansion by Stéphane Kattoor for background details. Here is a practical application of the zsh for loop.
Using the Bourne shell for loop
# sh
# for i in 1 2 3 4 5 6 7 8 9 10 11 12
do
rcp -p /etc/hosts esoftclient$i:/etc
done
Now using brace expansion for the Z shell for loop
# zsh
# for i in {1..12}
do
rcp -p /etc/hosts esoftclient$i:/etc
done
# for i in {1..100}
do
rcp -p /etc/hosts esoftclient$i:/etc
done
13 comments:
Zsh is really worth reading its man page, as it is full of gems like this !
By the way, thanks for the backlink :-)
I agree with you, and I'm going to take a closer look at it. You're welcome on the backlink -- it was worth it.
welcome back, I am now a fan
Rod
This works in Bash version 3+ as well.
Rod, I was on a long overdue vacation...I needed a break from work, Internet, my website and blog --- well, I guess everything. Roy
A more succinct version in bash is:
for i in `seq 12`; do
...
done
'seq' also has the ability to format the number (leading zeros, etc). However zsh is something I know I should investigate.
This is a cool trick.
Claudio
What's special in this case ? ksh93 has ANSI-C-like for loops like
for((i=0;i<12;i++));do echo "snoopy $i";done
anonymous, thank you for adding to the discussion
You can be even more faster in Zsh:
for i in {1..12} ; rcp -p /etc/hosts esoftclient$i:/etc
regards,
-mika- (http://michael-prokop.at/blog/)
mika, I've added you to my technical blogroll.
# to repeat a command periodically
while true; do echo "infinite loop"; sleep 5; done
Hi,
I just wanted to a comment to http://www.mysysad.com/2007/08/unix-zsh-for-loop.html but unfortunately I couldn't figure out, how to do this w/o registration. Perhaps you could post it:
It's Posted Manuel...
---
Tarka: 'seq' also has the ability to format the number (...)
Which zsh has, too :-)
zsh# print {1..10}
1 2 3 4 5 6 7 8 9 10
zsh# print {01..10}
01 02 03 04 05 06 07 08 09 10
-----------------
Thanks,
Manuel.
Post a Comment