Results 1 to 4 of 4

Thread: [bashscript] input doesn't work?

  1. #1
    Join Date
    Nov 2007
    Location
    Netherlands
    Beans
    4
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    [bashscript] input doesn't work?

    Hi y'all,

    i'm pretty new to the whole bashscript thing, so my following question probably is stupid. Sorry for this.

    I have this test code:

    Code:
    #!/bin/bash
    
    echo -n "enter your name: "
    read name
    echo "Your name is $name"
    I made this little script executable and when it opens up in the terminal I can see the 'enter your name: ' phrase. Now when I enter something and press enter, my terminal just closes itself...

    What is it that I'm doing wrong?
    Last edited by Digitz; November 13th, 2007 at 07:00 PM.

  2. #2
    Join Date
    Jun 2007
    Location
    Tacoma, WA
    Beans
    244
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [bashscript] input doesn't work?

    I ran your script and did not have any problems. After re-reading your post, it sounds like you are executing this script from outside a terminal. Is this correct?

    If so, consider opening up a terminal and manually running the command.

    Another experiment is to add 'sleep 10' as the last line in your script. Run the program as you originally did. I think this will cause the script output to remain displayed for 10 seconds after the name is echo'd.

  3. #3
    Join Date
    Nov 2007
    Location
    Netherlands
    Beans
    4
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: [bashscript] input doesn't work?

    Quote Originally Posted by meatpan View Post
    I ran your script and did not have any problems. After re-reading your post, it sounds like you are executing this script from outside a terminal. Is this correct?

    If so, consider opening up a terminal and manually running the command.

    Another experiment is to add 'sleep 10' as the last line in your script. Run the program as you originally did. I think this will cause the script output to remain displayed for 10 seconds after the name is echo'd.
    Yes you're correct, i'm executing my script by double clicking on it. But the sleep 10 made it stay open for 10 secs indeed! Now if i just add another 'read var_name' as last line, it wil just stay open if that's what I want.

    Thnx for your help!!!

  4. #4
    Join Date
    May 2007
    Location
    Canada
    Beans
    374
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [bashscript] input doesn't work?

    I was going to suggest doing what you stated Digitz (about using read at the end of the script to keep it open). You might also want to "echo" a statement about pressing the enter key to close the program.

    If you were to become accustomed to using the terminal, you might find those kinds of messages irritating. It might be a good idea to allow the user to input an option (such as "-p") which invokes a "press enter to continue" message. It would also be a good exercise if you're new to scripting.

Bookmarks

Posting Permissions

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