Bash script to update Debian type systems, a couple of questions
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900
Rep:
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
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.
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900
Original Poster
Rep:
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?
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
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900
Original Poster
Rep:
Quote:
Originally Posted by grail
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
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
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.
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900
Original Poster
Rep:
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.
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900
Original Poster
Rep:
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.
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.
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900
Original Poster
Rep:
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.