[NTLUG:Discuss] Shell script help 2

. Daniel xdesign at hotmail.com
Fri May 12 17:49:30 CDT 2006


> > 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"?





More information about the Discuss mailing list