Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Script question - Reboot or exit?

  1. #1
    Join Date
    Mar 2014
    Location
    Netherlands
    Beans
    Hidden!
    Distro
    Ubuntu 13.10 Saucy Salamander

    Script question - Reboot or exit?

    Hi,

    Got an nice installation script, but got a problem, on the end you must do [Enter] for a reboot or CTRL+C to exit the terminal, but it reboots automatic.
    This is the code:
    Code:
    echo "Bedankt voor het gebruiken van het installatie script. :)"
    read -r -p "Druk op [Enter] om het systeem te herstarten of op CTRL+C om te annuleren..."
    sudo reboot

  2. #2
    Join Date
    Sep 2011
    Beans
    615

    Re: Script question - Reboot or exit?

    If you are running that in a windowed terminal, instead of cntrl-c you could instruct the user to close the window.

    More elegantly, you can use that read -p to set a variable from input and then an if/then/fi statement to branch to either an "exit" command or a "reboot" command, depending on the response. BTW, if you want a normal user to be able to run this without entering a password you'll have to exempt it in etc/sudoers.

    And another BTW: You've taught me something. I didn't know there WAS a reboot command. I've been using "shutdown -r now".

  3. #3
    Join Date
    Jul 2007
    Location
    Magic City of the Plains
    Beans
    Hidden!
    Distro
    Xubuntu

    Re: Script question - Reboot or exit?

    Moved to Programming Talk.

  4. #4
    Join Date
    Mar 2014
    Location
    Netherlands
    Beans
    Hidden!
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Script question - Reboot or exit?

    Quote Originally Posted by Dreamer Fithp Apprentice View Post
    If you are running that in a windowed terminal, instead of cntrl-c you could instruct the user to close the window.

    More elegantly, you can use that read -p to set a variable from input and then an if/then/fi statement to branch to either an "exit" command or a "reboot" command, depending on the response. BTW, if you want a normal user to be able to run this without entering a password you'll have to exempt it in etc/sudoers.

    And another BTW: You've taught me something. I didn't know there WAS a reboot command. I've been using "shutdown -r now".
    Hi,

    how can I make the variable exactly?

    Thank you,
    Korkel

  5. #5
    Join Date
    Apr 2012
    Beans
    6,872

    Re: Script question - Reboot or exit?

    You could do something like

    Code:
    #!/bin/bash
    
    while true; do 
      read -p "Would you like to reboot the system now (Y/N)? " ans
      case "$ans" in
        [yY]*) echo "You chose yes"
        # do something 
        break
        ;;
    
        [nN]*) echo "You chose no"
        # do something else
        break
        ;;
    
        *) echo "Oops - try again"
        ;;
      esac
    done
    The while loop isn't strictly necessary - you could decide that everything except 'y/Y/es' means 'no' for example. Another possibility (in bash) is the 'select' statement which basically does the presentation of the choices / looping for you:

    Code:
    #!/bin/bash
    
    select action in "reboot" "exit"
    do
      case "$REPLY" in
    
      1) echo "You selected $action"
      # do something
      break
      ;;
    
      2) echo "You selected $action"
      # do something else
      break
      ;;
    
      *) echo "Please try again"
      ;;
    
      esac
    done

  6. #6
    Join Date
    Mar 2014
    Location
    Netherlands
    Beans
    Hidden!
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Script question - Reboot or exit?

    OK, I don't get it, when I test it in terminal, it closes without any error.

  7. #7
    Join Date
    Apr 2012
    Beans
    6,872

    Re: Script question - Reboot or exit?

    ... you will need to add whatever actual commands you want to execute in place of the comments

    Code:
    # do something
    
    # do something else

  8. #8
    Join Date
    Mar 2014
    Location
    Netherlands
    Beans
    Hidden!
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Script question - Reboot or exit?

    Must I keep the # ?

    If I got this:
    Code:
    #!/bin/bash
    
    while true; do 
      read -p "Would you like to reboot the system now (Y/N)? " ans
      case "$ans" in
        [yY]*) echo "You chose yes"
        # sudo reboot
        break
        ;;
    
    
        [nN]*) echo "You chose no"
        # exit
        break
        ;;
    
    
        *) echo "Oops - try again"
        ;;
      esac
    The terminal closes without information.

  9. #9
    Join Date
    Apr 2012
    Beans
    6,872

    Re: Script question - Reboot or exit?

    # indicates that the rest of the line should be treated as a comment - you need

    Code:
      case "$ans" in
        [yY]*) echo "You chose yes"
        sudo reboot
        break
        ;;
    and so on (the break is not necessary assuming the reboot executes successfully, but it won't do any harm to leave it there)

  10. #10
    Join Date
    Mar 2014
    Location
    Netherlands
    Beans
    Hidden!
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Script question - Reboot or exit?

    Tried it, but when I open it in terminal closes directly.

Page 1 of 2 12 LastLast

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
  •