LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script to update Debian type systems, a couple of questions (http://www.linuxquestions.org/questions/programming-9/bash-script-to-update-debian-type-systems-a-couple-of-questions-913593/)

k3lt01 11-15-2011 02:02 AM

Bash script to update Debian type systems, a couple of questions
 
Hi everyone

I have written a script to update Debian type systems and it works until the end where it just stops. I also have it installing Debdelta so it can use debdelta to minimise download times.

My questions are, can I get the script to work out if it needs to install Debdelta and then continue on if it doesn't?
Also can anyone see why it just stops at the end without actually upgrading?

Code:

#!/bin/bash
# A script to update package lists, creates debs from available deltas
# and upgrade the system without adding or removing anything.
echo "This script relies on debdelta being install on your system for it to
  work effectively"
read -p "Do you wish to install debdelta? (Y/n)"
        if [ "$REPLY" = "n" -o "$REPLY" = "N" ]; then
                echo "closing now"
                sleep 5
                exit 1
        fi
        if [ "$REPLY" = "y" -o "$REPLY" = "Y" ]; then
                echo
                echo "This script requires super-user access to continue."
                echo 'Checking for super-user access...'
                echo
                # Temporarily set sudo timeout to 360 mins
                echo "Defaults passwd_timeout=360" | sudo tee -a /etc/sudoers > /dev/null
                echo -en $WHITE "Access Granted." $GRAY

                echo "installing debdelta now"
                sudo aptitude update
                sudo aptitude install debdelta
                read -p "debdelta installed. Do you wish to continue? (Y/n)"
                        if [ "$REPLY" = "n" -o "$REPLY" = "N" ]; then
                        echo "closing now"
                        sleep 5
                        exit 1
                fi
                if [ "$REPLY" = "y" -o "$REPLY" = "Y" ]; then
                        echo "starting update/upgrade now"
                        sudo aptitude update
                        sudo debdelta-upgrade
                        sudo aptitude safe-upgrade
                fi
        fi
fi


evo2 11-15-2011 02:10 AM

Hi,

you can use something like 'dpkg -l <package>' to find out if a given package is installed.
If you indent the script properly I think you'll see you've got an extra fi.

Cheers,

Evo2.

evo2 11-15-2011 02:14 AM

Opps, sorry, not 'dpkg -i', 'dpkg -s'.

Eg.
Code:

dpkg -s debdelta > /dev/null 2>&1
if [ "$?" != "0" ] ; then
  echo "Please install debdelta"
  exit 1
fi

Cheers,

Evo2.

k3lt01 11-15-2011 02:33 AM

Hi Evo
Thanks for your reply. I see how that would work on its own, yes I tried it after reading the man page (should have read that before lol) but I can't see how to add it to the script. Should I use grep or something similar?

evo2 11-15-2011 02:39 AM

Hi,

well, first test if it is installed then if it is not you can ask the user if they want to install it, or otherwise exit.

Something like:

Code:

dpkg -s debdelta > /dev/null 2>&1
if [ "$?" != "0" ] ; then
  echo "It seems debdelta is not installed.
  read -p "This script needs debdelta. Can I install it? y/n >"
  if [ "$REPLY" != "y" ] ; then
    exit 1
  fi
  sudo apt-get install debdelta
fi

Cheers,

Evo2.

grail 11-15-2011 02:44 AM

Quote:

I see how that would work on its own
I am not sure why you think it would work any differently in your script?

You can also make it a little simpler (as your using bash):
Code:

if ! dpkg -s debdelta > /dev/null 2>&1
then
  echo "Please install debdelta"
  exit 1
fi


k3lt01 11-15-2011 02:47 AM

Quote:

Originally Posted by evo2 (Post 4524312)
Hi,

well, first test if it is installed then if it is not you can ask the user if they want to install it, or otherwise exit.

[code]
dpkg -s debdelta > /dev/null 2>&1
if [ "$?" != "0" ] ; then
echo "It seems debdelta is not installed.
echo "This script needs debdelta. Can I install it?"

exit 1
fi

Shouldn't something be infront of dpkg so the script can use it? like if, read, fi, echo etc, that is what I am confused with.

k3lt01 11-15-2011 02:54 AM

Quote:

Originally Posted by grail (Post 4524315)
I am not sure why you think it would work any differently in your script?

Hi grail. I test things on their own before I try them with other things, it is how I work through things. It is the way I diagnose things that I don't know how to do. I'm basically sitting here teaching myself scripting.

Quote:

Originally Posted by grail (Post 4524315)
You can also make it a little simpler (as your using bash):
Code:

if ! dpkg -s debdelta > /dev/null 2>&1
then
  echo "Please install debdelta"
  exit 1
fi


Doesn't seem to want to work in the script

trappa01 11-15-2011 03:15 AM

Quote:

Shouldn't something be infront of dpkg so the script can use it? like if, read, fi, echo etc, that is what I am confused with.
No. It just runs the command and bins the output. The next line check the return code for the command. If the package is installed $?=0 . Otherwise $?=1.

The later version.
Code:

if ! dpkg -s debdelta > /dev/null 2>&1
then
  echo "Please install debdelta"
  exit 1
fi

does the same thing but compares the result of the command at execution.

k3lt01 11-15-2011 03:42 AM

Ok the adddition is now working, sort of, in the script. One thing is wrong, everytime I run it it still wants to install debdelta which is already installed.

k3lt01 11-15-2011 04:12 AM

I think I know what's going wrong with the debdelta installation, I haven't given any options in the script so it is still going to install debdelta because the script says to. There isn't any conditional to say hey if it isn't installed install it but if it is installed move to the next section.

I'll have to do some more reading, I don't know at the moment how to use the /dev/null to say move to the next section cause you already have debdelta or if I need to modify the next section.

EDIT: Also what's the proper format for indentation? I have been using the tab key.

grail 11-15-2011 04:50 AM

Indentation is a personal thing and tab is fine if you like the look of it :)

As for how to utilise the code simply place install part inside if.

trappa01 11-15-2011 04:52 AM

Don't worry too much about /dev/null for your script. It is only a "bin" device that acts like a black hole. If you run a command but don't want the output clogging up your screen, you send the output to /dev/null. This does not change the result of the command, just the output. The example also adds 2>$1 which says to also send stderr to /dev/null.

k3lt01 11-15-2011 05:21 AM

Thanks guys, I have the debdelta part working perfectly now but the upgrade still isn't working as it should. There is a error at the end but it flashes by and then the terminal closes. Is there a way to create a readable log file from what is on the terminal screen?

My current file looks like this
Code:

#!/bin/bash
# A script to update package lists, creates debs from available deltas
# and upgrades the system without adding anything that is not required or
# removing something that is required.
# Created by me so I could teach myself scripting
# with help from trappa01, grail, and evo2 from LinuxQuestions.
if ! dpkg-query -l debdelta = i > /dev/null 2>&1
then
        echo "This script requires super-user access to continue."
        echo 'Checking for super-user access...'
        echo
        # Temporarily set sudo timeout to 360 mins
        echo "Defaults passwd_timeout=360" | sudo tee -a /etc/sudoers > /dev/null
        echo -en $WHITE "Access Granted." $GRAY
        echo "starting update/upgrade now"
        sudo aptitude update
        sudo debdelta-upgrade
        sudo aptitude safe-upgrade
        fi
fi
else
        echo "It seems debdelta is not installed."
        read -p echo "This script relies on debdelta being installed /non your system for it to work effectively"
        read -p "Do you wish to install debdelta? (Y/n)"
        if [ "$REPLY" = "n" -o "$REPLY" = "N" ]; then
                echo "closing now"
                sleep 5
                exit 1
        fi
        if [ "$REPLY" = "y" -o "$REPLY" = "Y" ]; then
                echo
                echo "installing debdelta now"
                sudo aptitude update
                sudo aptitude install debdelta
                read -p "debdelta installed. Do you wish to continue? (Y/n)"
                        if [ "$REPLY" = "n" -o "$REPLY" = "N" ]; then
                        echo "closing now"
                        sleep 5
                        exit 1
                fi
                if [ "$REPLY" = "y" -o "$REPLY" = "Y" ]; then
                        echo "starting update/upgrade now"
                        sudo aptitude update
                        sudo debdelta-upgrade
                        sudo aptitude safe-upgrade
                fi
        fi
fi


grail 11-15-2011 05:30 AM

Well, evo pointed out in his first post that you have to look at how many times you open / close your if statements.

Also I cannot understand how you new if statement works?
Code:

if ! dpkg-query -l debdelta = i > /dev/null 2>&1
Remembering that a script just helps you from typing a lot of stuff at the command line, what do you get when you run the following on the command line:
Code:

dpkg-query -l debdelta = i
Because I get errors, which of course are hidden by outputting to /dev/null as pointed out by trappa01


All times are GMT -5. The time now is 06:52 PM.