LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-22-2014, 02:30 PM   #1
cleeky
LQ Newbie
 
Registered: May 2014
Posts: 3

Rep: Reputation: Disabled
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")

Last edited by cleeky; 05-22-2014 at 02:32 PM. Reason: Made the COdes Section small to make it clearer
 
Old 05-23-2014, 12:15 AM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
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?
 
Old 05-25-2014, 12:42 PM   #3
cleeky
LQ Newbie
 
Registered: May 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-25-2014, 01:26 PM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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.
 
1 members found this post helpful.
Old 05-27-2014, 02:44 PM   #5
cleeky
LQ Newbie
 
Registered: May 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks Keith, it works like a charm and the day is not wasted because I've learned something new today.
 
Old 05-27-2014, 02:57 PM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script for reading output of linux command and make input Black187 Linux - Newbie 8 09-08-2013 03:25 PM
Push output string from script directly into bash / readline command input buffer GreyBeard Linux - Software 3 02-20-2012 11:26 PM
Help on my Linux Homework ! bash shell script / input-output data etc Bebelindo Programming 2 03-03-2009 12:51 PM
Pass output of one command as input to another on bash linuxlover.chaitanya Linux - Newbie 3 01-08-2009 01:46 AM
input into bash script(reading path) dalicros Linux - General 3 05-30-2007 06:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:18 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