Having problems with my maintemance script...

Forum rules
Before you post please read this

Having problems with my maintemance script...

Postby jamesringhof on Fri Mar 29, 2013 10:21 pm

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)
Last edited by xenopeek on Sat Mar 30, 2013 6:23 am, edited 1 time in total.
Reason: Script put in CODE tags, so formatting is preserved...

You Can't Trust Atoms ... They Make Up Everything ... !
James - 2B, or not 2B, that is the FF
Asus N53S, 3.3g i7, 16g RAM, 750g HDD, Nvidia CUDA 2g, BR B&O. Mint 15 Cinnamon. 03 July 2013

User avatar
jamesringhof
Level 1
Level 1
 
Posts: 13
Joined: Sun Mar 10, 2013 9:34 pm
Location: Mulla Creek (Tamworth), NSW 2340, Australia

Linux Mint is funded by ads and donations.
 

Re: Having problems with my maintemance script...

Postby jawgrape on Fri Mar 29, 2013 11:30 pm

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
jawgrape
Level 1
Level 1
 
Posts: 29
Joined: Tue Mar 12, 2013 9:10 pm

Re: Having problems with my maintemance script...

Postby Pilosopong Tasyo on Sun Apr 07, 2013 7:11 am

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.
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1174
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Having problems with my maintemance script...

Postby jamesringhof on Sat Apr 20, 2013 5:24 am

: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:

You Can't Trust Atoms ... They Make Up Everything ... !
James - 2B, or not 2B, that is the FF
Asus N53S, 3.3g i7, 16g RAM, 750g HDD, Nvidia CUDA 2g, BR B&O. Mint 15 Cinnamon. 03 July 2013

User avatar
jamesringhof
Level 1
Level 1
 
Posts: 13
Joined: Sun Mar 10, 2013 9:34 pm
Location: Mulla Creek (Tamworth), NSW 2340, Australia

Re: Having problems with my maintemance script...

Postby catweazel on Sat Apr 20, 2013 5:48 am

Mint Testing Team & Mint Donor #3606
KDE 4.12.0, custom preemptive kernel 3.12.5,
Intel i7 4770K @ 4.7GHz, 16GB 2666MHz XMP,
4 Samsung 840 PRO 512GB SSDs in RAID0,
6TB HW RAID10, dual 24" Acer X243H,
Gigabyte nVidia GTX 680 Super Overclock
User avatar
catweazel
Level 7
Level 7
 
Posts: 1656
Joined: Fri Oct 12, 2012 9:44 pm


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 0 guests