LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Presskey (https://www.linuxquestions.org/questions/programming-9/presskey-4175424116/)

Aerosilver 08-26-2012 05:56 PM

Presskey
 
Hello everyone,

This is "my first question", and I have to solve one problem with the bash scripts.

My problem is: I am programming a bash file which will be used to create, delete and modify users and groups.

Now I am building a menu, but I want to use the array keys to navigate in the system.

My doubt is:
How to press once a specifically key, and then, save that key in a variable, and continue the program.

The next line don't run in my bash script:
Code:

read -n1 "Press any key" var
echo $var

I don't know if you can understand my doubt, my english is not my native language, but please help me.

Thanks,
Federico.

jlinkels 08-26-2012 06:31 PM

Code:

read -a var -p "Enter a key: " -n 1
echo key read: $var

Google for specific Bash commands. For example: http://ss64.com/bash/read.html

Then run your script with -x as outlined in my sig.

jlinkels

Aerosilver 08-26-2012 07:44 PM

I write exactly this:

Code:

#!/bin/bash
clear
read -a var -p "Enter a key: " -n 1
echo "key read: $var"

And the system show this error:
Code:

read: 3: Illegal option -a
key read:


jlinkels 08-26-2012 09:17 PM

Code:

jlinkels@donald-pc:~$ cd /tmp
jlinkels@donald-pc:/tmp$ cat readkey.sh
read -a var -p "Enter a key: " -n 1
echo key read: $var

Code:

jlinkels@donald-pc:/tmp$ bash -x readkey.sh
+ read -a var -p 'Enter a key: ' -n 1
Enter a key: 3+ echo key read: 3
key read: 3
jlinkels@donald-pc:/tmp$

Code:

jlinkels@donald-pc:/tmp$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
jlinkels@donald-pc:/tmp$

I am pretty much surprised. Can you check you bash version.
Remove the shebang (#!/bin/bash) and run the command I did?

jlinkels

NevemTeve 08-26-2012 11:21 PM

Lack of she-bang invokes /bin/sh which is platform-specific: ash/bash/dash/ksh/...

jlinkels 08-27-2012 11:03 AM

Quote:

Originally Posted by NevemTeve (Post 4765029)
Lack of she-bang invokes /bin/sh which is platform-specific: ash/bash/dash/ksh/...

Sure about that?
Quote:

Originally Posted by jlinkels
bash -x readkey.sh

jlinkels

NevemTeve 08-27-2012 11:50 AM

> Remove the shebang (#!/bin/bash) and run the command I did?

If you run the script with "bash <scriptfile>", then the she-bang won't hurt, because it is a comment. If you run the script simply entering "./scriptfile", then the lack of she-bang causes execve(2) to return ENOEXEC, which causes execlp(3) to invoke /bin/sh, which is platform-dependent. So I don't really see the point of removing the she-bang.

Aerosilver 08-27-2012 01:44 PM

I try to see the bash version: "GNU bash, version 3.2.49(1)-release (i586-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.".

Now I solve the problem with this:

Code:

read -rsn 1 var
echo $var

Thanks for all.

Now I've another problem, but I will post it in another post.

Thanks again.


All times are GMT -5. The time now is 05:47 PM.