Register
It is currently Wed Apr 23, 2014 11:32 pm

Allowing user entry in a while read loop


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Thu Jun 28, 2012 4:31 am   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
Greetings all,
How do you pause the screen in a while read loop? Here is a snippet of code from another post yesterday. I've gotten the real script pretty well developed, but then thought my screen output was rather long and thought I'd allow a pause to give the user time to read it vice scrolling back and forth. But, I can't get an echo -n nor a read -p to stop. Is a pause impossible inside of this kind of while construct? Or, perhaps, I am facing the possibility that my bash shell is outdated as I found out recently it does not have some of the shopt constructs. I also tried to setup a pause function and call it from within the loop. Should I format my while read statement differently?

Code:
#!/bin/bash
while read STRING
do
   echo "$STRING" >> certfile1.out
   echo "$STRING"  # Echoing for testing and watching for END string.
   if [ "$STRING" = "-----END CERTIFICATE-----" ]; then
      echo END found
      echo -n "Press any key to continue: "
      read KEY
      #DID NOT WORK# read -p "Press any key to continue: "
   fi
done < inputcertfile.crt


Top
 Profile  
 PostPosted: Thu Jun 28, 2012 7:23 am   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
Go it. I finally found the correct way to ask Google the question.

http://stackoverflow.com/questions/2087188/how-to-read-using-read-from-file-descriptor-3-in-bash-script

Here is the code should the URL link become unavailable. What I needed started at the exec 3<&0 line.
Code:
counter=1
echo -n "Enter a file name : "
read file

if  [ ! -f $file ]
then
    echo "$file not a file!"
    exit 1
fi

# read file line by line
exec 3<&0
while read line
do
       # pause at line no. 15
    if [ $counter -eq 15 ]
    then
        counter=0 # reset counter
        echo " *** Press [Enter] key to continue ..."
        read -u 3 enterKey
    fi
    echo $line
    (( counter++ ))
done < $file


This worked perfectly. I didn't need the pause at line 15 part.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP