Having problems with my maintemance script...

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
jamesringhof

Having problems with my maintemance script...

Post 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)
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
jawgrape

Re: Having problems with my maintemance script...

Post 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
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Having problems with my maintemance script...

Post 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.
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].
jamesringhof

Re: Having problems with my maintemance script...

Post 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:
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Having problems with my maintemance script...

Post by catweazel »

"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
Locked

Return to “Scripts & Bash”