LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 09-23-2010, 01:01 AM   #1
make
Member
 
Registered: Apr 2004
Distribution: Mandriva, Ubuntu, openSuSE, FreeBSD, OpenSolaris, PC-BSD
Posts: 195

Rep: Reputation: 30
Bash - Read user input: URLs without the enter key stroke


Here's a challenge I've been struggling for months with:

I have a bash script that reads URL addresses of our internal server and then executes some test commands on them. Something like this:

Code:
read -p "Enter URL: " url
sh execute-what-ever-to $url
After copy-pasting the URL the user taps the enter key and the script proceeds, but here comes the tricky part: I want this to work without the need to press the enter key after copy-pasting the URL.

"read -n" does not work in this case, as the URLs vary greatly in length. However, the URLs always end to the same string. They could be like "http://url1/END", "http://url2/END" and so on. So this ending string "END" could be theoretically used to recognize that the whole URL has been pasted.

Any easy solution? Bash, perl, python... others?
 
Old 09-23-2010, 07:46 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
You can read one letter at a time:
Code:
#!/bin/bash

url=''
echo -n 'Enter URL: '
while grep -qv 'END$' <<<"$url" ; do
    read -n1 next
    url="$url$next"
done
echo

sh execute-what-ever-to "$url"
 
Old 09-23-2010, 04:29 PM   #3
make
Member
 
Registered: Apr 2004
Distribution: Mandriva, Ubuntu, openSuSE, FreeBSD, OpenSolaris, PC-BSD
Posts: 195

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by ntubski View Post
You can read one letter at a time:
BIG thanks to you! This was the kind of solution I was actually looking for, I was thinking about a loop for reading letter-by-letter, but never got it working on my own.

Again, thank you!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Bash & Xdialog - how to code to recognize ENTER key on keyboard.? craftybytes Linux - Software 6 04-03-2008 01:00 AM
How can you determine if a user pressed the enter key? jimwelc Programming 3 04-04-2006 10:08 PM
Bash: how to read <enter> key linmix Programming 3 12-02-2005 04:38 PM
Key Stroke Logger MrTheGeek Linux - Security 1 02-03-2002 01:05 AM

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

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