Unix Programming - Looping through files in a directory (Korn on AIX)

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2004 > Looping through files in a directory (Korn on AIX)





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Looping through files in a directory (Korn on AIX)
Craig

2004-11-20, 8:12 am

I have just started programming UNIX on AIX using the Korn shell and I
would be grateful if sample code could be posted to help me achieve
the following:

I have files called-
/home/scripts/main.ksh
/home/scripts/run.ksh
/home/sql/interviews/int1.sql

I would like to have main.ksh read through all the files located in
the same dir as int1.sql (there may be more than 1 file in this dir).

Once main.ksh has the filename it will pass it to run.ksh to do some
processing with it.

Many thanks in advance.
Trent Buck

2004-11-20, 8:12 am

Quoth Craig on or about 2004-11-20:
> I would like to have main.ksh read through all the files located in
> the same dir as int1.sql (there may be more than 1 file in this dir).
>
> Once main.ksh has the filename it will pass it to run.ksh to do some
> processing with it.


If no file contains a space, it's probably safe to say:

for i in /home/sql/interviews/*; do
/home/scripts/run.ksh $i
done

This is safer, but still prone to malicious file naming:

ls /home/sql/interviews \
| xargs /home/scripts/run.ksh

Note that I have used these with a POSIX shell (dash) on Debian. AFAIK
korn is mostly compatible with POSIX, but your mileage may vary.

-trent
PS: `set -e' and `set -x' may be advisable.
Lőrinczy Zsigmond

2004-11-20, 5:49 pm

Trent Buck wrote:

> If no file contains a space, it's probably safe to say:
>
> for i in /home/sql/interviews/*; do
> /home/scripts/run.ksh $i
> done


You may quote the variable:

for i in /home/sql/interviews/*; do
/home/scripts/run.ksh "$i"
done
Craig

2004-11-22, 5:51 pm

L?rinczy Zsigmond <nospam@for.me> wrote in message news:<cnnsg2$i4g$1@namru.matavnet.hu>...
> Trent Buck wrote:
>
>
> You may quote the variable:
>
> for i in /home/sql/interviews/*; do
> /home/scripts/run.ksh "$i"
> done


Many thanks for your help, the info has proved very useful.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com