Results 1 to 10 of 11
hello.
i want to make a bash script that does the following:
if i press y it deletes /home/user/something
if i press n it exits the script
any other keystroke=IGNORE
...
- 06-23-2009 #1Just Joined!
- Join Date
- Jun 2009
- Posts
- 44
[SOLVED] very easy one
hello.
i want to make a bash script that does the following:
if i press y it deletes /home/user/something
if i press n it exits the script
any other keystroke=IGNORE
thank you all
- 06-23-2009 #2Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
Have a look at the case command which is built in to Bash. You might find getopts useful as well.
Check the man pages or have a read here for case or here for getopts
- 06-23-2009 #3Just Joined!
- Join Date
- Jun 2009
- Posts
- 44
Yes, but could you please give me an working example??
So that I can understand the command syntax?
thank you
- 06-23-2009 #4
Sounds a bit like homework...
Linux User #453176
- 06-23-2009 #5Just Joined!
- Join Date
- Jun 2009
- Posts
- 44
well i'm a linux begginer, so I'm sorry if I don't have your knowledge.
I've tried this
#!/bin/bash
# Capture keystrokes without needing to press ENTER.
old_tty_setting=$(stty -g) # Save old terminal settings.
stty -icanon -echo # Disable canonical mode and echo
keys=$(dd bs=1 count=1 2> /dev/null)
echo you pressed $keys
stty "$old_tty_setting" # Restore old terminal settings.
but it returns me an error
- 06-23-2009 #6
I'm sorry if I was wrong. We don't mind helping out but we often get students coming on trying to trick us into doing homework assignments. The task you are trying to do sounded a bit like homework and when people ask for a 'working example' it usually means they want something written for them
When asking for help with code it is best practice to post the code, or part of code, that you are having problems with to show that you have at least tried it yourself. It will also let us know where you may have gone wrong
For your problem I would suggest using the read command with the –n option set to 1. As bigtomrodney suggested you can then use case to check what value the user entered and act accordingly.
You can find out more about read here
read MAN Page
And case here
case MAN Page
Both pages have examples for youLinux User #453176
- 06-23-2009 #7Just Joined!
- Join Date
- Jun 2009
- Posts
- 44
thank you all
ADFC
- 06-24-2009 #8Just Joined!
- Join Date
- Jun 2009
- Posts
- 44
Hello!
OK, and what if I make it like this:
If I press s or n the program prints something, otherwise, it reads the keystroke again.
Code:#!/bin/bash read temp1 while [ "$temp1" != 's' && "$temp1" != 'n' ] do clear #clears the screen read temp1 #reads the keystroke again done echo "You pressed n or s keys!"
But it doesn't work.
It returns:
./test.sh: line 5: [: missing `]'
Ideas??
Thank you
- 06-24-2009 #9Just Joined!
- Join Date
- Jun 2009
- Posts
- 44
Ah ok!
I figured it out!
You have to use [[ ... ]] test construct.
The only problem is that when I use read I have to press the enter key.
And I just want to press the keystroke (without enter).
Is that possible??
Thanks
- 06-25-2009 #10



