LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2004, 03:51 PM   #1
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 31
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
 
Old 05-19-2004, 11:06 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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
 
Old 05-20-2004, 10:23 AM   #3
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Original Poster
Rep: Reputation: 31
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
 
Old 05-20-2004, 01:01 PM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Set your IFS to be a newline and you can read filenames with spaces.
Code:
IFS='
'
 
Old 05-20-2004, 06:53 PM   #5
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

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

Thanks again
 
Old 05-21-2004, 08:21 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a word in a string with bash orgazmo Programming 6 06-07-2005 10:19 AM
How do I read from a file in BASH? vous Programming 4 03-22-2005 06:51 PM
trouble with find command in bash vrdhananjay Linux - Newbie 3 10-30-2004 11:37 PM
Help with BASH script PLEASE READ!!! hroman Programming 7 10-08-2004 07:39 PM
bash scripting read from file cadj Programming 2 02-29-2004 10:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration