LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to take input from anything except [a-zA-Z0-9] (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-take-input-from-anything-except-%5Ba-za-z0-9%5D-909915/)

Batistuta_g_2000 10-24-2011 05:43 PM

How to take input from anything except [a-zA-Z0-9]
 
As i understand it there is no way of having <ENTER> or <SPACE> as an input (read value) - so how can I prompt a user for a file beginning with any option from [a-zA-Z0-9] and then give another option ideally of just pressing <enter> for all files and <esc> to exit?

couldn't track too much information on this, any help is greatly appreciated.

David the H. 10-24-2011 06:51 PM

I'm not sure I quite understand what you're asking. Reading input from what, into what? Using what? And your title doesn't seem to match your description. Care to give us a real example of what you want to do?

Are you talking about the shell read built-in? There's nothing special about enter or spaces there. All the enter button does is send a newline character into the buffer, and read by default terminates on newline. But you can set the -d option to terminate on some other character instead.

Batistuta_g_2000 10-25-2011 03:11 PM

Quote:

Originally Posted by David the H. (Post 4507093)
I'm not sure I quite understand what you're asking. Reading input from what, into what? Using what? And your title doesn't seem to match your description. Care to give us a real example of what you want to do?

Are you talking about the shell read built-in? There's nothing special about enter or spaces there. All the enter button does is send a newline character into the buffer, and read by default terminates on newline. But you can set the -d option to terminate on some other character instead.

Firstly apologies for not making my question clear, so I want my Bash shell script to ask the user to enter the first "Character" of a file name (in [a-zA-Z0-9]) to search ( find -L ./"$filename") or else press ENTER to list all files in my "Restore" directory. Is this possible as in;

[Code]
"Press Enter to list full directory of restores or just enter the first letter of filename:"
read input
if [ $input = [a-zA-Z0-9] ]
find -L ./"$input"
elif
[ $input = [<ENTER KEY>]
ls "/full directory of restores"

Thanks in advance for help...

David the H. 10-26-2011 12:21 PM

I still don't understand completely.

What exactly should happen if the user enters a single letter? Your current code has it searching a directory of that name. (enter "a" --> list files in directory "a"). But you want it to match the start of a filename? If so, what file, and where? Could there be multiple matches, and what should happen if there are?

Or do you just want it to just return a list of files to choose from; to create a sub-menu to make selecting the correct name easier?


Please break down what you want to do in detail. Show us the directory and file structure, and give us a step-by-step run-through on what should happen at the command line.


One possible simple solution may be to simply use the -e option in read to enable the readline library. This lets you use the shell's tab completion and other editing features to navigate your filesystem.

As for testing the "enter key", the newline character it sends is the delimiter that terminates the read, so if there's no text the variable will be empty. So just test for nothing.


All times are GMT -5. The time now is 10:34 PM.