LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Trouble with bash read command (https://www.linuxquestions.org/questions/programming-9/trouble-with-bash-read-command-183415/)

Bebo 05-19-2004 03:51 PM

Trouble with bash read command
 
Hello,

I've wrote me a script that checks for new configuration files after a Slackware upgrade. In Slackware, updates in /etc come as files ending in ".new", i.e. inittab.new. This is of course to avoid screwing up a working configuration. Now, I've written a script that uses find to locate the .new files. It then checks if the corresponding "old" file exists, and asks me if I want to diff them. Then it asks if I want to move the .new file over the old file. Like this:

Code:

#!/bin/sh

DIR=/etc
[ x`echo $1` != x ] && DIR=$1

echo "Checking for .new files in $DIR..."

find $DIR -xdev -name '*.new' | while read NEWFILE ; do
  OLDFILE=${NEWFILE%%.new}

  if [ -e "$NEWFILE" -a -e "$OLDFILE" ] ; then
      AGE=older
      [ `find "$NEWFILE" -cnewer "$OLDFILE" | grep -c .new` -gt 0 ] && AGE=newer

      echo -n "$NEWFILE is $AGE than $OLDFILE. Wanna diff them? (y/N)"
      read ANSWER

      [ x`echo $ANSWER` == xy ] && diff -bB "$NEWFILE" "$OLDFILE"

  else
      echo "$NEWFILE has no $OLDFILE counterpart."
  fi
   
  echo -n "Wanna move $NEWFILE to $OLDFILE? (y/N)"
  read ANSWER
  [ x`echo $ANSWER` == xy ] && mv -iv "$NEWFILE" "$OLDFILE"
done

Now my problem: the script doesn't stop and prompt me for an answer at the read statements. It just skips them, and I have absolutely no idea why. I've been staring me blind at this darn script and all I've managed is to start thinking that the problem doesn't really have to do with the read statements. But I can't see what's wrong. Does anyone have a clue?

TIA,


Bebo

crabboy 05-19-2004 11:06 PM

The find | while read was queueing up more data then you could read in one read statement. When you attemped to read the y/n, the read was grabbing another filename from the find pipe. Change the find line to this:
Code:

for NEWFILE in `find $DIR -xdev -name '*.new'`; do

Bebo 05-20-2004 10:23 AM

Thanks a lot, crabboy! I would never have realized this myself.

However, I was avoiding the for "method" since I wanted to allow for more general file names, e.g. file names with spaces in them. I think I'm actually gonna solve this by first redirecting the find output to a file, and then look in that file.

Again, thanks a lot! :)

Cheers

crabboy 05-20-2004 01:01 PM

Set your IFS to be a newline and you can read filenames with spaces.
Code:

IFS='
'


Bebo 05-20-2004 06:53 PM

Aha, great, now it actually works :) 'Cause my thought to redirect to a file didn't work either for some reason.

Thanks again :)

jschiwal 05-21-2004 08:21 AM

You could use the 'select' command instead of read. This gives you a menu. If you mistype the answer, it will present the options again.


All times are GMT -5. The time now is 03:11 PM.