Loading
Home ›
Getting Loopy with Bash: using for loops
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Parallel Programming with NVIDIA CUDA
- Readers' Choice Awards 2011
- 100% disappointed with the decision to go all digital.
- Linux-Based X Terminals with XDMCP
- Validate an E-Mail Address with PHP, the Right Way
- You Need A Budget
- The Linux powered LAN Gaming House
- Why Python?
- Python for Android
- Employment Posters
3 hours 4 min ago - Sure the best distro is
4 hours 24 min ago - BeOS was the best
7 hours 8 min ago - I use Wireshark on a daily
11 hours 39 min ago - buena información
16 hours 45 min ago - One important "bucket" that I didn't note (désolé si qqun deja d
17 hours 46 min ago - Gnome3 is such a POS. No one
1 day 3 hours ago - Gnome 3 is the biggest POS
1 day 3 hours ago - I didn't knew this thing by
1 day 9 hours ago - Author's reply
1 day 12 hours ago





Comments
<grumble>
And no sooner to I post here than I find the answer on another site, ShellGeek (imagine that). So, thanks anyway.
Guy
Using for at the prompt
I was trying to come up with a way, using for, to run an executable on the output of the ls command in a directory. That is, to have the output of ls be the input to a command:
#> for m in `ls`; do echo $m; done
This works ONLY if the file names in the directory have no spaces, e.g.,
~>ls
autosave Documents Music
bin emerald-themes
Desktop logs.05182008.sp.zip
doc man
But if the filename is a multi-word name with spaces, the for construct assigns to m the individual words and the echo output breaks the filename across separate lines. I suppose this has more to do with how ls outputs, but I cannot find an ls option (not -Q nor -1 nor -x) that seems to get the for construct to 'think' that the filenames output by ls are legitimate if they have spaces (if that makes any sense).
I have found a way to use find's -exec or -print0 with xargs to do what I wanted to do, but I was curious if any of you gurus at linux journal can come up with a way of using for on the command line:
for x in '{command with output}'; do {command on output} $x; done
for m in `ls`;do command
for m in `ls`;do command "$m";done
putting $m inside " marks causes $m to be a single argument. I do this all the time when I used Cygwin on Windows (at work).
--
John
re: it should be noted though
Isn't this the LINUX Journal after all? :)
it should be noted though
it should be noted though that calling seq makes it less portable as you wont likely find seq on many flavors of unix... granted you can argue that you wont find bash either but hey, just sayin.
target :)
That looks like a Target ball in the intro :)
One of these days you should do some tech tips on how you make these videos (If you have already, please post a link so I can check it out). What hardware and programs you use to record and edit? I would like to make similar instructional videos for some of my personal and work documentation.
Good to see these shell tips. Are there cases when the list would be too long for the shell to handle directly requiring something like xargs? (maximum line length) If so, that may be another good tech tip to post. These tech tips are quickly becoming my favorite part of Linux Journal :). They're concise and informative.
for + seq
If you want use the for loop as a counting loop
then you could do this:
for x in `seq 1 1000` do echo "line: $x" doneThis will run 1000 iterations.
#!/bin/sh # doing it in C
#!/bin/sh
# doing it in C way
for ((I=0;I<=1000;I++))
do
printf "line: %03d\n" $I
done
you can also use the
you can also use the following in newer(3+ i think?) versions of bash:
for x in {1..1000}; do
echo "line: $x"
done
Though Not Quite Interchangeable
Note however that this works:
j=1000 for i in `seq 1 $j` do echo $i doneWhere as this does not:
j=1000 for i in {1..$j} do echo $i doneTo "make" it work do:
j=1000 for i in `eval echo {1..$j}` do echo $i doneMitch Frazier is an Associate Editor for Linux Journal.