# For Loop

**URL:** <https://community.unix.com/t/for-loop/142874>\
**Category:** Shell Programming and Scripting\
**Created:** [June 4, 2002, 11:04am UTC](https://community.unix.com/t/for-loop/142874 "2002-06-04T11:04:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![supercbw](https://community.unix.com/letter_avatar/supercbw/32/5_5575768a8748004e209b776fc1b2916d.png) [@supercbw](https://community.unix.com/u/supercbw)\
**Post date:** [June 4, 2002, 11:04am UTC](https://community.unix.com/t/for-loop/142874/1 "2002-06-04T11:04:52Z")

</div>

I have executed the command

numOfAcct=`ls availablityFax.acc* | wc -l`  
echo "${numOfAcct}

numOfAcct displays 4

I want to use a for loop to execute a call to another program numOfAcct times. How do you write the for portion?

I originally had:  
for i in $numOfAcct}  
do  
echo "ME"  
done

This did not execute 4 times.

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [June 4, 2002, 11:13am UTC](https://community.unix.com/t/for-loop/142874/2 "2002-06-04T11:13:04Z")

</div>

This didn't work because the for loop works off a list.  
(For each file in a directory do this...for each word in a line, do this)

You should check the man page and try using a while loop or change the condition for the FOR loop to look at the files instead of the number of files. I don't know which you need without further info.

numOfAcct=`ls availablityFax.acc*`  
echo "${numOfAcct}

numOfAcct displays each filename

for i in $numOfAcct}  
do  
echo "$i"  
done

This would list all the files one by one inside the for loop.

---

<div class="post-metadata">

**Author:** ![LANkrypt0](https://community.unix.com/letter_avatar/lankrypt0/32/5_5575768a8748004e209b776fc1b2916d.png) [@LANkrypt0](https://community.unix.com/u/LANkrypt0)\
**Post date:** [June 4, 2002, 11:14am UTC](https://community.unix.com/t/for-loop/142874/3 "2002-06-04T11:14:09Z")

</div>

I would use until

#!/bin/ksh  
numOfAcct=4  
ncounter=1  
until [[$ncounter -gt 4]];do  
echo $ncounter  
ncounter=`calc "$ncounter + 1"`  
done

---

<div class="post-metadata">

**Author:** ![LivinFree](https://community.unix.com/user_avatar/community.unix.com/livinfree/32/20_2.png) [@LivinFree](https://community.unix.com/u/LivinFree)\
**Post date:** [June 5, 2002, 8:02pm UTC](https://community.unix.com/t/for-loop/142874/4 "2002-06-05T20:02:58Z")

</div>

That will get pretty slow if you start working with large numbers...

If you're going to use ksh, why bother calling calc?

```nohighlight
until [[$something -eq 10]]; do
print $something
something=$((something+1))
done

```

OP, it really matter what you're trying to accomplish...  
Does each filename need to be sent to another command as an argument?  
If so, hoghunter is pointing you right...

```nohighlight
for each in availabilityFax* ; do
command $each
done

```

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [June 5, 2002, 9:31pm UTC](https://community.unix.com/t/for-loop/142874/5 "2002-06-05T21:31:26Z")

</div>

will this help?

======================  
let count=0

for x in `ls availablityFax.acc*`  
do  
let count=$count+1  
done

# echo $count

thanks 🙂
