Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just 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

  2. #2
    Linux 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

  3. #3
    Just 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

  4. #4
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    Sounds a bit like homework...
    Linux User #453176

  5. #5
    Just Joined!
    Join Date
    Jun 2009
    Posts
    44
    Quote Originally Posted by Kieren View Post
    Sounds a bit like homework...
    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

  6. #6
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    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 you
    Linux User #453176

  7. #7
    Just Joined!
    Join Date
    Jun 2009
    Posts
    44
    thank you all
    ADFC

  8. #8
    Just 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

  9. #9
    Just 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

  10. #10
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    Quote Originally Posted by Kieren View Post
    For your problem I would suggest using the read command with the –n option set to 1
    So in your script you would use:

    Code:
    read -n 1 temp1     #reads the keystroke again
    Linux User #453176

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...