Click to See Complete Forum and Search --> : shell scripting


onesixnine
03-11-2004, 07:32 PM
Ok, today I decided to try my hand at some shell scripting. I have been using linux for close to 4 years now but have never wrote any of my own scripts ...

Anyways, I decided to write a script that would start and stop my UT2003 server. I initially put the UT server startup string in another small script, (called startupUT) which this main script calls on.

The whole things works pretty flawlessly, you enter a 1-3 and it performs the options it is supposed to.

But, when you just press enter, I thought I had made it so it would just say "you must enter a number between [1-3], and it does that, but it also prints some other output for some reason. It prints this:

/usr/bin/manageut: line 10: test: -eq: unary operator expected
/usr/bin/manageut: line 14: test: -eq: unary operator expected
/usr/bin/manageut: line 18: test: -eq: unary operator expected
you must enter a number between [1-3]

Here is my script:

clear
input=0
echo "UT Server Manager"

echo "1. Start UT Server"
echo "2. Don't start : exit"
echo "3. Stop UT Server"
echo -n "Enter in your choice [1-3]"
read input
if test $input -eq 1
then

echo "starting UT Server"
startupUT
elif test $input -eq 2
then
echo "Ok, closing now"
exit 1
elif test $input -eq 3
then
echo "Shutting down UT server now!"
killall -e ucc-bin
else
echo "you must enter a number between [1-3]"
fi


Any help/comments/opinions/flames ???
thanks :)

blackrax
03-11-2004, 07:53 PM
hmm - it was a "thing" with your if statement (look at the new code, which i took the liberty of formatting); and you might just aswell do a string comparison - the thing with bash is that it's not particularly easy to debug, unfortunately. however, my theory is that, in your original code, you supplied an empty variable ($input) when just pressing enter, when it expected an integer. i think.


#!/bin/bash
clear
input=0
echo "UT Server Manager"

echo "1. Start UT Server"
echo "2. Don't start : exit"
echo "3. Stop UT Server"
echo -n "Enter in your choice [1-3]"
read input
if [ "$input" == "1" ]; then
echo "starting UT Server"
startupUT
elif [ "$input" == "2" ]; then
echo "Ok, closing now"
exit 1
elif [ "$input" == "3" ]; then
echo "Shutting down UT server now!"
killall -e ucc-bin
else
echo "you must enter a number between [1-3]"
fi

knute
03-11-2004, 07:54 PM
With a couple of changes to your code, it should work.

I haven't tested this, but what do you think?
clear
input=0
echo "UT Server Manager"

echo "1. Start UT Server"
echo "2. Don't start : exit"
echo "3. Stop UT Server"
echo -n "Enter in your choice [1-3]"
read input
if test $input <> {1 | 2 | 3}
then
echo "you must enter a number between [1-3]"
exit 1
elif test $input -eq 1
then

echo "starting UT Server"
startupUT
elif test $input -eq 2
then
echo "Ok, closing now"
exit 1
elif test $input -eq 3
then
echo "Shutting down UT server now!"
killall -e ucc-bin
fi

Bold is an addition to your script.

Also I took your message from the bottom of the script. I'm not sure if the syntax is right for the case checking, but I think you get where I'm going on the changes. :)

HTH

pickarooney
03-11-2004, 07:55 PM
I'd read the input variable as a char value
if [ "$input" == "1" ]
then
do stuff
elif [ "$input" =="2" ]
etc..


Not saying it will work any better, but you might get different errors ;)

blackrax
03-11-2004, 08:08 PM
i wish i could get this many replies when asking about iptables or other obscure issues :eek: otoh, good that bash support is easy to come by :D

onesixnine
03-11-2004, 08:37 PM
blackrax, you were right.
It must have been something with my if comments and using 'test'.
When trying the modifications that knute gave me, it had the same errors. I used Blackrax's code, and no more errors. I wonder why ...
thanks for the help though :)

blackrax
03-11-2004, 10:49 PM
hmm; if you really want to know why your original code didn't work, try supplying -x as a parameter to bash - will give you some debugging output.; and check the man page for 'test' - although i think it's bash related.

in case you want to learn some more about bash scripting, i recommend these two guides; they are both worth taking a look at, and a great reference.

Bash-Prog-Intro-HOWTO (http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html)
advanced bash scripting guide (http://www.tldp.org/LDP/abs/html/)

cheers,
//blackrax