Unix script - For loop not working with regular expr

A forum for discussing DataStage<sup>®</sup> basics. If you're not sure where your question goes, start here.

Moderators: chulett, rschirm, roy

Post Reply
Kirtikumar
Participant
Posts: 437
Joined: Fri Oct 15, 2004 6:13 am
Location: Pune, India

Unix script - For loop not working with regular expr

Post by Kirtikumar »

Hi

I have a simple shell script in which I am taking the filename/dirname/reg expr as input.
On it I am exeuting the ls command and taking some action on it.

Code: Select all

for i in `ls $1`
do
     echo "Working on file: $i"
     <other stmts>
done
When the script is called with directory name as arguement, it works fine i.e. for all the files under that dir, it executed all statements.
But if the instead of the directory, I give a reg expr like /tmp/file*.txt, it just picks up first file from the list and loop just executes once.

E.g. consider the script has name ProcesFile.sh and directory /tmp/kirtis/ has following files - file1.txt, file2.txt, emp1.txt and emp2.txt.
When called as ProcesFile.sh /tmp/kirtis/ it processes all 4 files.
But when called as ProcesFile.sh /tmp/kirtis/file*.txt, it only processes file1.txt and skips the other files starting with file i.e. file2.txt.
Regards,
S. Kirtikumar.
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

That's because the * is being interpreted at the command line, and not passed as an argument. Try quoting the argument when passing in.
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
Kirtikumar
Participant
Posts: 437
Joined: Fri Oct 15, 2004 6:13 am
Location: Pune, India

Post by Kirtikumar »

Thanks Kenneth!!!

I tried it and now its it working when used with quotes.
In the script, I do not want the user to use it with quote in this case. So is there anyway that while taking the input, instead of interpreting it at command prompt, it will taken as complete arg with * without interpreting it? Of course without using quote characters.

I will also look if there is any other way to do this.
Regards,
S. Kirtikumar.
Post Reply