bash Script Variable Assignment Problem

Status
Not open for further replies.

2 Donuts

Ars Tribunus Militum
2,872
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?

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.

# Error tolerance.
while [ "$1" != "start" ] && [ "$1" != "stop" ] && [ "$1" != "restart" ]
        do
          	echo 'Please enter one of the following parameters: "start" "stop" "restart"'
                read newinput
                $1=newinput
        done
            	if [ "$1" = "start" ]; then
                        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.'
                elif [ "$1" = "stop" ]; then
                        echo 'Stopping RMI and multiseat.'
                        # Call the stop script.
                        /etc/X11/stop.sh
                        sleep 10
                        echo 'RMI and multiseat stopped.'
                else
                    	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.'
                fi
 

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
 

2 Donuts

Ars Tribunus Militum
2,872
willmo: Thanks for the lesson, and the script that works perfectly. I definitely like your indentation better, but I'm just using single tabs with nano 2.0.9. Is it more common to just use spaces instead?

ronelson: Thanks. The design lead wants this to always run from a terminal so that he can always see any possible error messages on-the-fly. Either he or one other specific person will be running it, as it's not for production software. The only reason the error correction was important was that typos will happen, and it's nice to account for them.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
I definitely like your indentation better, but I'm just using single tabs with nano 2.0.9. Is it more common to just use spaces instead?
Common? Probably half and half. I like spaces better. You can set many editors to transform your tabs into a set number of spaces, which means you can set the indent that you want and it will always be there. Whatever you do, just pick one - tabs or spaces - but do not mix and match.
ronelson: Thanks. The design lead wants this to always run from a terminal so that he can always see any possible error messages on-the-fly. Either he or one other specific person will be running it, as it's not for production software. The only reason the error correction was important was that typos will happen, and it's nice to account for them.
You should find a way to simply error out entirely if it is called non-interactively, then. Otherwise you could have a process sitting there for months waiting on input it will never receive - or multiple processes if it gets stuck in cron as "script strat" by accident. I would suggest starting here and customizing it with the appropriate error message.
 
Status
Not open for further replies.