Register
It is currently Thu Jul 24, 2014 6:17 am

super noob scripter Intro/Help


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sun Feb 19, 2012 2:36 pm   

Joined: Sat Feb 18, 2012 10:20 pm
Posts: 2
Hi everyone, I just joined this forum in hopes to learn more about bash scripts.
Since I am completely new to this, it didn't take long to learn something after reading just a few threads.
Seems like a great community and I am excited to keep learning.

Now that my intro is out of the way, I would like to ask for help.
I am trying to write my very first script.
Just a simple apt-get update and upgrade, clean and autoclean script.

Code:
#!/bin/bash
clear
echo "Welcome"
echo
echo
echo
echo "1.Update & Upgrade"
echo "2.Clean & Autoclean"
echo "3.Exit"
echo
echo -n "Select a task  "
read opt

cd ~

if [ "$opt" != "1" ]
      then sudo apt-get update && sudo apt-get upgrade
          # ./test.sh
           exit
    #  else echo "Please select a task from the list"
fi

if [ "$opt" != "2" ]
      then sudo apt-get clean && sudo apt-get autoclean
           ./test.sh
           #  exit
    #  else echo "Please select a task from the list"
fi

if [ "$opt" != "3" ]
      then exit
    #  else echo "Please select a task from the list"
fi


Obviously, this script is far from clean, as I was trying different things to see what happens, like I said I'm new!
The script actually works, but backwards, or "not right"

when "1" is entered it runs the "clean" line, and when "2" is entered it runs the "update" line.
Why is this?

Thanks in advance, for this and, I'm sure, many more questions to follow. :))

Edit -
Well, after playing with it, I got it.

Code:
#!/bin/bash
clear
echo "Welcome"
echo
echo
echo
echo "1.Update & Upgrade"
echo "2.Clean & Autoclean"
echo "3.Exit"
echo
echo -n "Select a task  "
read opt

cd ~

if [ "$opt" == "1" ]
      then sudo apt-get update && sudo apt-get upgrade
          ./test.sh
          # exit
    #  else echo "Please select a task from the list"
fi

if [ "$opt" == "2" ]
      then sudo apt-get clean && sudo apt-get autoclean
           ./test.sh
           #  exit
    #  else echo "Please select a task from the list"
fi

if [ "$opt" == "3" ]
      then
        exit 0
fi


Now I need to clean it up, suggestions are welcome!

Edit again -

OK, cleaned it up.
Tell me what I could do better.
I know the comments might be a little obvious, but I want to get in the habit of using comments to help me along.

Code:
#!/bin/bash
clear
echo "Welcome to"
echo
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "Shane's command center"
echo "Continue at your own risk"
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo
echo
echo "1.Update & Upgrade"
echo "2.Clean & Autoclean"
echo "3.Exit"
echo
read -p "Select a task  " opt #Assigns value to opt variable

cd ~ #Changes to the home dir

if [ "$opt" == "1" ] #If 1 is entered
      then sudo apt-get update && sudo apt-get upgrade #Runs command
          ./test.sh #Returns to menu
fi

if [ "$opt" == "2" ] #If 2 is entered
      then sudo apt-get clean && sudo apt-get autoclean #Runs command
           ./test.sh #Returns to menu
fi

if [ "$opt" == "3" ] #If 3 is entered
      then
        exit 0 #Exit
fi


Top
 Profile  
 PostPosted: Sun Feb 19, 2012 6:11 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
hi,

Quote:
Tell me what I could do better.

learn more about `case', and `select' builtins


Top
 Profile  
 PostPosted: Sun Feb 19, 2012 10:24 pm   

Joined: Sat Feb 18, 2012 10:20 pm
Posts: 2
Thanks for the advice.
Looking into them now.


Top
 Profile  
 PostPosted: Sat Mar 31, 2012 9:57 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
stmcmurray76 wrote:
Tell me what I could do better.


Learn about functions to avoid executing the same script while it's running to go back to the "menu";
here-documents to avoid using echo(0) that much; while-loops to put your code in an infinite loop
that prompts for the number; and case statements not to use multiple if statements and have a
better view on the possible options.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP