Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 28th, 2005, 11:20 PM   #1
shinni
Newbie
 
Join Date: Jan 2005
Posts: 5
Rep Power: 0 shinni is on a distinguished road
Bash script to access all files in a directory

Hi .. I am a new bie in writing scripts, so this doubt may be a simple one.

I am trying to apply a parsing program to a set of input files in a directoy.
How can i access all the files from a source directory , one by one and to write the
output files with another name to some directory ? i.e
my exe file is a.out
I have input files 1.txt,2.txt .. in one directory.
I need this cmd line ./a.out <inputfilename> > <outputfilename> , on all input files.

can i write a loop to get all files like this ? right now i am writing the same line
again and again to apply to all files..

Plz post ur suggestions.

Thank u

Shinni
shinni is offline   Reply With Quote
Old Jan 30th, 2005, 12:42 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,466
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
in your script, have ls -lstr > file

then have a while loop that gets fed file

inside the while loop...

assign a variable to the 10th position in that file (the filename of the current file in your dir) - you can do this with awk

then, still in the loop, call your a.out $curFile > newFile
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jan 31st, 2005, 5:02 AM   #3
shinni
Newbie
 
Join Date: Jan 2005
Posts: 5
Rep Power: 0 shinni is on a distinguished road
Re :Bash script to access all files in a directory Reply to Thread

Thank u .. I had posted this doubt at one more forum n i got the following steps also..
This also works.

#!/bin/bash
cd <input_file_directory>
for file in `dir -d *` ; do
<exeFile with full path> "$file" <output_file_path/"$file".out>
done

I have another doubt , as a follow up .. If i need to change this input directory and output directory to test different data sets, what should i do ? Now, i hav specified SourceDir and DestDir within the program. Can i pass command line arguements or soemthing like that to assign some path to variables representing the directories ?

Thank u

Shinni
shinni is offline   Reply With Quote
Old Jan 31st, 2005, 9:18 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,466
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
yes, parameters will work fine.

$0 = program name
$1 = 1st parameter
$2 = 2nd parameter
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Feb 18th, 2005, 3:26 PM   #5
magyarkid
Newbie
 
Join Date: Jan 2005
Location: Virginia USA
Posts: 6
Rep Power: 0 magyarkid is on a distinguished road
Send a message via AIM to magyarkid
Quote:
Originally Posted by Infinite Recursion
yes, parameters will work fine.

$0 = program name
$1 = 1st parameter
$2 = 2nd parameter
Yeah... that will work if you want to run
./a.out <infilename> <outfilename>
Then you just use $1 and $2 to reffer to infilename and outfilename, respectively.
__________________
Signed,
Magyarkid
magyarkid is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC