[NTLUG:Discuss] Shell script help 2
LEROY TENNISON
leroy_tennison at prodigy.net
Fri May 12 18:33:38 CDT 2006
I'll probably get bashed for this but 'use awk' (or gawk or several other ?awk variants). Those who have an aptitude for programming can learn it in a day and become reasonably good in a week or so. Although it seems to be 'depreciated' at this point by the programming 'politically correct' crowd it's a great tool for simple to moderately complex text processing tasks. I prefer it over Perl, etc. because it does the following without a line of code:
Opens the file specified on the command line.
Automatically loops reading the file (supplied as a parameter to the program) line by line (technically record by record but the default record delimiter is a newline).
Tokenizes the line (default token delimeter is white space but this can be set as well) providing each token as a variable.
Makes a number of counters available as variables (number of records, number of fields in a record, etc.)
Closes the input file at the end of processing.
Sends the output to standard out (great for debugging, easy to redirect).
Can accept it's script as a command line parameter (grep your /etc/init.d scripts for 'awk', you will hopefully find some examples).
Just so there aren't complaints, it's not a miracle working program. Like any language it has an intended purpose and if you try to make it do things it wasn't intended for life becomes hard. Perl was written when "awk ran out of gas". However, for quick-and-dirty it's a good tool.
". Daniel" <xdesign at hotmail.com> wrote: > > Also on my list of "seems simple enough" things to script:
> >
> > A simple backup script. Basically, what I want is this:
> >
> > backup.sh source_dir backup_dir
> >
> > where the copy operation is controlled like this:
>
>#!/bin/bash
>
>rsync -avz source_dir backup_dir
>
>since you have it in a script. :)
>
>man rsync for more details or see here for more detailed scripts:
>
>http://www.mikerubel.org/computers/rsync_snapshots/
Okay, that's not what I was after but I have been playing with rsync just a
bit. I may end up simply using rsync in the end but I still want to learn
more shell scripting through this activity. In studying random examples on
the web, I have come up with an interesting way of reading input from
outside sources such as a file or the execution of a command.
Using "find" I'd like to be able to read in all the file and directory
names line-by-line and process a file copy based on 'whatever' criteria.
Here's what I have come up with so far:
--------------------------------
echo ""; echo -n "Reading file structure..."
cd $1
echo "Processing..."
for line in $(find)
do
if [ ${#line} -ne 1 ]
then
echo "${line#'./'}"
fi
done
--------------------------------
The "cd $1" line takes the first command parameter and changes to that
directory. I have successfully written some limited error checking code
that determines the file exists and that it's a directory.
Using the for loop, I have found that I can get the output of the "find"
command "word by word."* The first line of output contains a "." so I want
to filter out any single-character line by saying "if it's not a length of
1, process the line." The echo line processes the line by removing the
"./" from the front of each line. Again, the output of "find" isn't
perfect, but close enough.
Here's the problem. As I mentioned, the for loop reads the output of
"find" "word by word" and not "line by line." This presents a problem when
filenames have spaces or other delimiter characters in them. Is there
another way to read input line-by-line the way I need? I can create a
temporary file containing the output of "find" but I don't see what good
that does since it's something about the for loop that's making it
word-by-word. Since this will not work, how can I read a file or output
"line-by-line"?
_______________________________________________
http://ntlug.pmichaud.com/mailman/listinfo/discuss
More information about the Discuss
mailing list