LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 02-08-2012, 02:25 PM   #1
GreyBeard
LQ Newbie
 
Registered: Oct 2003
Location: Taxachusetts, USA
Distribution: Slackware
Posts: 25

Rep: Reputation: 0
Question Push output string from script directly into bash / readline command input buffer


Hi,

I have an application (bash script) which looks at certain conditions on a machine and generates a bash command to deal with those conditions. Trouble is that sometimes the generated command is not exactly what a user wants so the user has to be able to edit the command before it is executed.

The way it works right now is that my script just outputs the generated command to STDOUT, then the users cut and paste the command into the command line prompt, and finally they use the command as is or change it as needed using the readline() command editing capabilities built into bash.

What I would like to do is have my script put the proposed command directly into readline's input buffer and have it displayed after the prompt ready for editing just as if the users had done the cut and paste themselves.

Any idea how I might do that short of modifying bash or readline?

Thanks in advance.

Jeff B.
 
Old 02-08-2012, 08:20 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Code:
OUTPUT=$(some_command)

read -e -i $OUTPUT -p "Cmd to run: " ANSWER

if [[ -z "${ANSWER}" ]]
then
    echo "Nothing entered"
else
    $ANSWER
fi
.. adjust as required - while loops etc ...
 
1 members found this post helpful.
Old 02-11-2012, 04:48 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
To expand on kbp's answer, you need read's "-e" option so that it uses readline for the input field, and "-i" to insert the default text to modify. Another nice side effect of using readline is that it also allows tab completion.

"help read" and "man bash" for more details.

Note that these options are bash extensions, and likely won't work in other shells.
 
1 members found this post helpful.
Old 02-20-2012, 11:26 PM   #4
GreyBeard
LQ Newbie
 
Registered: Oct 2003
Location: Taxachusetts, USA
Distribution: Slackware
Posts: 25

Original Poster
Rep: Reputation: 0
Thanks for your answers. That solves my problem.

I did have to improve on one aspect of the suggested solution. I know I
said my script generated "a bash command" but what I really meant was that
it generated a string of single commands separated by semicolons the sum
of which constituted (in my view) "a command". I solved that problem by
modifying the script above to be as shown below, then it all worked.

Thanks again.


OUTPUT=$(some_command)

read -e -i $OUTPUT -p "Cmd to run: " ANSWER

if [[ -z "${ANSWER}" ]]
then
echo "Nothing entered"
else
eval "$ANSWER"
fi

Last edited by GreyBeard; 02-20-2012 at 11:27 PM. Reason: formatting
 
  


Reply

Tags
bash, bash scripting, command line


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
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
Bash script to check the input string length fjkum Programming 3 06-30-2007 08:43 PM
[bash / readline] Access / output content of yank buffer? FnordPerfect Programming 1 01-19-2007 10:54 AM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 02:36 PM


All times are GMT -5. The time now is 07:53 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration