LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash Script - Reading User Input while Processing output from Command within Bash (https://www.linuxquestions.org/questions/linux-general-1/bash-script-reading-user-input-while-processing-output-from-command-within-bash-4175505797/)

cleeky 05-22-2014 03:30 PM

Bash Script - Reading User Input while Processing output from Command within Bash
 
I am trying to get a users input by using read -p method while it inside a loop which is processing an output from a command by using read -r method. read -p method does not work it seems to be still reading data from the output from the command. any ideas?

Code:

while read -r LLINE; do
        echo `echo $LLINE | awk -F '[:]' '{print $1}'`
        echo 'Do You Want To Delete,Archive or Skip this file'
        echo 'Type (D|A|S) and press enter'
        read -p "Do You Want To Delete,Archive or Skip this file (D|A|S) ?" Responce
        case $Responce in
        [Dd]*)        #delete the file
                        ;;
        [Aa]*)        #Archive the file
                        ;;
        *)                # Skip File
                        ;;
        esac
       
       
done < <(find -P $HomeDirPath -size +$MaxSize\k -type f -printf "%h/%f:%s\n")


Ygrex 05-23-2014 01:15 AM

both «read» commands work with the same file descriptor, the default STDIN, because you did not specify any; since you attached STDOUT from «find» command to STDIN with a pipe, it works exactly like you explained; what kind of ideas you are asking about?

cleeky 05-25-2014 01:42 PM

What I want to achive
 
What i was tryinbg to do was to process the output from the find command query as it is generated and get a user to choose to archive, delete or skip any actions on the large file. I did not want to put the output from the find into an array nor file then process the array/file, Has this would give a period of time where there is no activity on screen whilw the output from the command was collected.

So is there any way round this issue with read using stdin channel for the both reads?

I would be greatfully for any assistance on this.

Keith Hedger 05-25-2014 02:26 PM

Put this:
Code:

THISTTY=$(tty)
At the front of your script.
Wrap your read like so:
Code:

exec 6<&0
exec 0<"$THISTTY"
read -p "Do You Want To Delete,Archive or Skip this file (D|A|S) ?" Responce
exec 0<&6 6<&-

Should work on both the console and a terminal.

cleeky 05-27-2014 03:44 PM

Thanks Keith, it works like a charm and the day is not wasted because I've learned something new today.

Keith Hedger 05-27-2014 03:57 PM

No probs, but if you are going to be doing any bash scripting you will want to grab a copy of the advanced bash scripting guide here:
http://www.tldp.org/guides.html


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