Click to See Complete Forum and Search --> : some bash shell scripting questions...


3m00
01-27-2003, 01:19 AM
Okie, here they go...

1) How can I perform the same action to all the files in my current directory?

2) How can I ask for user input? (y/n answers)

error27
01-27-2003, 03:16 AM
1)
for i in * ; do
echo foo $i
done

2)
read foo
echo $foo

root.veg
01-27-2003, 11:12 AM
Follow up questions for anyone willing (sorry to piggyback, but this thread just answered my first question):

I want to run a couple of hundred MS Word documents and templates through antiword and save the results as plain text files with the same name, just without the .doc suffix - so can anyone fill in the blanks?:

for i in ./*.do? ; do
antiword [blank] >> [blank]
done

This means I need to know a method for:

(1) referring to the filename I currently want to look at. At a command prompt I would use a pipe, something like

cat ./*.do? | antiword

but I'm stumped when needing to save the output to to separate fielnames which depend on the current filename.

(2) Stripping the last n characters off the current filename from (1).

These seem like such basic questions, but using man, info and websearches hasn't been useful because I seem to need to know the exact command name...

bwkaz
01-27-2003, 11:22 AM
for i in * ; do
newfilename=$(echo $i | sed -e 's/.doc/.txt/')
antiword $i >> $newfilename
done Of course, you'll have issues if any of your Word filename have spaces in them. If that's the case, you'll have to save IFS, replace IFS with $'\n', do the "for i in" stuff, replace $i with "$i" everywhere, and replace $newfilename with "$newfilename". Then set IFS to the saved value.

root.veg
01-27-2003, 11:34 AM
Blimey. I was way off the mark there! Will read the man pages for sed and then I should understand properly what you mean.

And yes, there are loads of spaces in the filenames, but I'll try what you recommend and post back if it works...

Thanks!

root.veg
01-27-2003, 07:35 PM
works a treat, thanks bwkaz: this is how it turned out:

keptvalue=$IFS

IFS=$'\n'

for i in *.doc ; do
newfilename=$(echo "$i" | sed -e 's/.doc/.txt/')
antiword "$i" >> "$newfilename"
done

IFS=keptvalue


Now I know more about sed, but still need to read on about the difference between single and double quotes, and what IFS is... time to rtfm. Joy!

bwkaz
01-27-2003, 10:34 PM
For that last line, you'll probably want IFS=$keptvalue instead. ;)

IFS is the shell variable that contains the characters that separate words (the letters stand for Internal Field Separator, I think). It's talked about somewhere in bash's man page.

By default on most distros, IFS is set to $' \t\n', which is space, tab and newline. By setting it to $'\n', you ignore spaces and tabs, so that the shell doesn't think that spaces or tabs separate words.

The $'....' construct lets you use most C escape sequences between the single quotes.

root.veg
01-28-2003, 06:56 AM
Many thanks for the concise explanation. Saved me a lot of time!