Click to See Complete Forum and Search --> : Some Scripting Help


RAGEAngel9
11-17-2002, 07:39 PM
I'm trying to create a script that will read in each item in a directory and write them to a text file without the .au extension. The problem I'm having is i forgot the proper way to read in the items one at a time.

Assuming the directory is constant, it should just be a while loop right?

RAGEAngel9
11-17-2002, 07:52 PM
This is what I have so far. I know I need to use ls on the directory and then use that output in the loop but I'm not sure how to use ls and it's data.

#! /bin/ksh

IFS="\n"


direct="./whateveritscalled"



while read
do
${Data%%.au}

done < word.txt



PS- for those wondering. This technically is for a school project but it's not graded so I don't mind asking for help.

z0mbix
11-18-2002, 04:37 AM
I would have thought a for loop would be more appropriate for iterating through each file in a directory.

Rüpel
11-18-2002, 06:52 AM
hmm. i have no shell here (it's time to install cygwin!!!) ;)

but something like

ls Data* | awk '{ printf("%s.au\n",$1); }' > files.txt

might work.

but i'm not sure, if i got your problem right... :rolleyes:

========= edit ==========
bummer!

mixed up sed and awk again... :rolleyes:

btw: i did install cygwin and the 1-liner above does list all files from the current directory starting with Data, appends .au at each and prints them one per line into files.txt

is this want you want?

RAGEAngel9
11-18-2002, 01:17 PM
That's it!?!
Geez! I really need to learn those cl commands and stuff.
Thank you very muc hfor your help.



========EDIT=======
Oh actually there is one thing. I wanted to get rid of the .au's. But that can't be that hard to change fro mthis right?

bwkaz
11-18-2002, 03:35 PM
Pipe the output of that through sed -e 's/.au//g' to get rid of all occurrences of the string ".au".

Or just ls Data* | sed -e 's/.au//g' to begin with, rather than adding a .au and then deleting more than one...

RAGEAngel9
11-18-2002, 04:48 PM
Ok so far I'm using this:

ls | sed -e 's/.au//g' > word.txt

and it works mainly.
1 problem and 1 thing I'd like to change is:
1. when I run it it adds word.txt to word.txt is there a way to block that?

2. How can I run this from the parent directory?

goon12
11-18-2002, 05:06 PM
ls | grep -v word.txt | sed -e 's/.au//g' > word.txt


I dont have access to a shell right now, but that might do it.

RAGEAngel9
11-18-2002, 05:11 PM
Ok ignore my last post.
Upon actually trying things on my own, I now have:

ls ./Words | sed -e 's/.au//g' > word.txt


Which does what I want.
The only thing remaining is I need to make it a script. What is the best way to do this? I was thinking the following:

#! /usr/sh

ls ./Words | sed -e 's/.au//g' > word.txt

But for some reason it won't run.
What's the correct way torun a script?

dubbac
11-18-2002, 05:19 PM
did you make the file executable?
if so, try running it like ./scriptname.
If you aren't sure if you made it executable you probably didn't.
If you don't want anyone else to have access to the file, do this:
chmod 700 scriptname
the numbers come from the access type you want to give, 4 for read, 2 for write, 1 for execute. Add up the number, 4 + 2 + 1 = 7.
The remaining zeroes are actually how others will access the file.
So think of it like this.
Owner|Group|World
rwx|rwx|rwx
421|000|000
Result is then:
7|0|0

I think thats it in a nutshell.
Hope it isn't too unclear.

bwkaz
11-18-2002, 06:10 PM
Originally posted by RAGEAngel9
#! /usr/sh

ls ./Words | sed -e 's/.au//g' > word.txt Err, that should be like this (I also made it accept the directory to search as the first command line argument -- you wouldn't have to do this, but it helps with reusability):

#!/bin/sh

ls $1 | sed -e 's/.au//g' You can then chmod 700 (or whatever) it, and do a ./scriptname >word.txt to run it. You could move the >word.txt inside the script, but that again reduces reusability. Whatever.

RAGEAngel9
11-18-2002, 06:36 PM
Well I figure it's better to have it as simple as possible.
This whole thing is a service project and the script onyl really needs to be run once a year. The main program just needs the text file.