bash Script Variable Assignment Problem

Status
You're currently viewing only willmo's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

willmo

Ars Tribunus Militum
1,652
palsyboy":3hh8d5z8 said:
I'm still new to bash scripting, and I'm trying to get a script to work. The other scripts to which this refers, start.sh and stop.sh, work fine, as well as my references to them. The only problem is that running a bad option or no option at all, i.e., "multiseatctl.sh asdlkfjasdflkj" instead of "multiseatctl.sh start," just keeps looping with "Please enter one of the following parameters: 'start' 'stop' 'restart'" instead of assigning a new value to the variable and moving on. Can anyone help?
1) That indentation is crazy. :p
2) You can't assign to the positional parameters ($1 through whatever) normally--you have to use the "set" command--and it seems like bad form to begin with.
3) You'd need a dollar sign in front of newinput in that assignment anyway.

I fixed all these problems and sexed it up a bit by rewriting with a case statement for you. Hopefully it still makes sense. Note that the breaks at the end of the first three cases break out of the while-loop, not out of the case statement like they would (with a switch statement) in C/C++/Java/etc.

Code:
#!/bin/bash
# This script starts, stops, or restarts (stops-then-starts) the multiseat, RMI
# registry, Java, and the simulation.  Ideally, the user will run it with
# "start," "stop," or "restart" as options.  If not, he will be prompted to 
# choose an option.

MY_CMD=$1
while true
do
    case $MY_CMD in
        start)
            echo 'Starting the multiseat, RMI, and settings dialog box.'
            # Call the start script.
            /etc/X11/start.sh
            # Add delays before providing text feedback, or it will be
            # lost amongst other messages.
            sleep 10
            echo 'Ready to begin simulation.'
            break
        ;;
        stop)
            echo 'Stopping RMI and multiseat.'
            # Call the stop script.
            /etc/X11/stop.sh
            sleep 10
            echo 'RMI and multiseat stopped.'
            break
        ;;
        restart)
            echo 'Restarting RMI and the multiseat.'
            # Stop
            /etc/X11/stop.sh
            # Start after a ten-second delay.  Doing otherwise will try to
            # start Java before the X servers have finished starting.
            sleep 10
            /etc/X11/start.sh
            sleep 10
            echo 'Ready to begin simulation.'
            break
        ;;
        *)
            # Error tolerance.
            echo 'Please enter one of the following parameters: "start" "stop" "restart"'
            read MY_CMD
        ;;
    esac
done
 
Status
You're currently viewing only willmo's posts. Click here to go back to viewing the entire thread.
Not open for further replies.