LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-26-2012, 05:56 PM   #1
Aerosilver
LQ Newbie
 
Registered: Aug 2012
Posts: 28

Rep: Reputation: Disabled
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.
 
Old 08-26-2012, 06:31 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 08-26-2012, 07:44 PM   #3
Aerosilver
LQ Newbie
 
Registered: Aug 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
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:
 
Old 08-26-2012, 09:17 PM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
1 members found this post helpful.
Old 08-26-2012, 11:21 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Lack of she-bang invokes /bin/sh which is platform-specific: ash/bash/dash/ksh/...
 
Old 08-27-2012, 11:03 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by NevemTeve View Post
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
 
Old 08-27-2012, 11:50 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> 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.
 
Old 08-27-2012, 01:44 PM   #8
Aerosilver
LQ Newbie
 
Registered: Aug 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply


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



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

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