Page 1 of 1

Having problems with my maintemance script...

Posted: Fri Mar 29, 2013 10:21 pm
by jamesringhof
Yes. This one has me stumped all the way. First let me say that I am not very experianced...but with your help...who knows.
I am trying to write a script to manage apt-get (although I use apt-fast.) Trying to cut down on key strokes to keep my system up to date with a little choice (Freedom.)
I want to be able to form a menu of things to do, choose one, and get the appropiate command to execute.
At the moment I just want the choice to print out. When this is going OK, I will proceed in getting the commands to execute.

This is my script (that fails disamaly.)

Code: Select all

#!/bin/bash
clear
echo "You are currently logged in as" $USER"."
echo "Please choose from the list below."
echo "1. update"
echo "2. upgrade"
echo "3. dist-upgrade"
echo "4. autoremove"
echo "5. "
echo "6. "
echo "7. "
echo "8. "
echo "9. "
echo "10. "
echo -n "Please Make Your Choice... "
read name
if $name == 1
	echo "update"
if $name == 2
	echo "upgrade"
if $name == 3
		echo "dist-upgrade"
if $name == 4
	echo "autoremove"
if $name == 5
	echo "5"
if $name == 6
	echo "6"
if $name == 7
	echo "7"
if $name == 8
     echo "8"
if $name == 9
     echo "9"
if $name == 10
     echo "10"
else 
     echo "INCORRECT CHOICE"
fi
(My formatting has disapeared. I do have the ECHO after the IF statement indented.)

Please, please, please help.

Thank You

James (Tamworth N.S.W Australia)

Re: Having problems with my maintemance script...

Posted: Fri Mar 29, 2013 11:30 pm
by jawgrape
This works.

in order to use else you need "then" in bash I think.

"==" is a string comparison but you were trying to use it with integers. Use -eq -lt etc for integers. if you want to still use "==" then you would need to convert the integers to strings first, or list your menu item indexes as a. b. c. etc. :-)

Code: Select all

#!/bin/bash
clear
echo "You are currently logged in as" $USER"."
echo "Please choose from the list below."
echo "1. update"
echo "2. upgrade"
echo -n "Please Make Your Choice... "
read name

if [ $name -eq 1 ]
then
    echo "update"
elif [ $name -eq 2 ]
then
    echo "upgrade"
else 
    echo "INCORRECT CHOICE"
    exit
fi
Hope this gets you over the hump

Re: Having problems with my maintemance script...

Posted: Sun Apr 07, 2013 7:11 am
by Pilosopong Tasyo
In lieu of nested/multi-level if-then-elif-else-fi statements, consider using the case statement instead.

Code: Select all

...
echo -n "Please Make Your Choice... "
read name

case $name in
   1 ) echo "update" ;;
   2 ) echo "upgrade" ;;
   3 ) echo "dist-upgrade" ;;
...
   8 ) echo "..." ;;
   9 ) echo "..." ;;
  10 ) echo "..." ;;
   * ) echo "INCORRECT CHOICE" ;;
esac
...
It makes code easier to read and write.
I want to be able to form a menu of things to do, choose one, and get the appropiate command to execute.
Have you considered using YAD (Yet Another Dialog)? It's a fantastic little program that wraps your scripts in a GUI. Here's an example of what you can do with it.

Re: Having problems with my maintemance script...

Posted: Sat Apr 20, 2013 5:24 am
by jamesringhof
:lol: :lol: :lol: :lol: :lol:

Thanking you all for your help. Yes, I did get over this hump in my script. It now works. I am just fine tunning the GUI now. Thanks again to all (2) who replied and offered help.

:lol: :lol: :lol: :lol: :lol:

Re: Having problems with my maintemance script...

Posted: Sat Apr 20, 2013 5:48 am
by catweazel