Getting Loopy with Bash: using for loops

FAIL (the browser should render some flash content, not this).

Download in .ogv format

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

<grumble>

Guy Stalnaker's picture

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

Guy Stalnaker's picture

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

Anonymous's picture

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

pschmitt52's picture

Isn't this the LINUX Journal after all? :)

it should be noted though

slacker's picture

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 :)

thogarty's picture

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

pschmitt52's picture

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"
done

This will run 1000 iterations.

#!/bin/sh # doing it in C

Anonymous's picture

#!/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

exsnafu's picture

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

Mitch Frazier's picture

Note however that this works:

j=1000
for i in `seq 1 $j`
do
    echo $i
done

Where as this does not:

j=1000
for i in {1..$j}
do
    echo $i
done

To "make" it work do:

j=1000
for i in `eval echo {1..$j}`
do
    echo $i
done

Mitch Frazier is an Associate Editor for Linux Journal.

Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

Webcast
8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
On Demand
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot