LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2011, 01:02 AM   #1
k3lt01
Senior Member
 
Registered: Feb 2011
Location: Australia
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900

Rep: Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637
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
 
Old 11-15-2011, 01:10 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
1 members found this post helpful.
Old 11-15-2011, 01:14 AM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
1 members found this post helpful.
Old 11-15-2011, 01:33 AM   #4
k3lt01
Senior Member
 
Registered: Feb 2011
Location: Australia
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900

Original Poster
Rep: Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637
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?
 
Old 11-15-2011, 01:39 AM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.

Last edited by evo2; 11-15-2011 at 01:44 AM.
 
1 members found this post helpful.
Old 11-15-2011, 01:44 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
 
1 members found this post helpful.
Old 11-15-2011, 01:47 AM   #7
k3lt01
Senior Member
 
Registered: Feb 2011
Location: Australia
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900

Original Poster
Rep: Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637
Quote:
Originally Posted by evo2 View Post
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.
 
Old 11-15-2011, 01:54 AM   #8
k3lt01
Senior Member
 
Registered: Feb 2011
Location: Australia
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900

Original Poster
Rep: Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637
Quote:
Originally Posted by grail View Post
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 View Post
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
 
Old 11-15-2011, 02:15 AM   #9
trappa01
LQ Newbie
 
Registered: Dec 2009
Posts: 20

Rep: Reputation: 8
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.
 
1 members found this post helpful.
Old 11-15-2011, 02:42 AM   #10
k3lt01
Senior Member
 
Registered: Feb 2011
Location: Australia
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900

Original Poster
Rep: Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637
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.
 
Old 11-15-2011, 03:12 AM   #11
k3lt01
Senior Member
 
Registered: Feb 2011
Location: Australia
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900

Original Poster
Rep: Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637
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.

Last edited by k3lt01; 11-15-2011 at 03:13 AM.
 
Old 11-15-2011, 03:50 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
 
1 members found this post helpful.
Old 11-15-2011, 03:52 AM   #13
trappa01
LQ Newbie
 
Registered: Dec 2009
Posts: 20

Rep: Reputation: 8
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.
 
1 members found this post helpful.
Old 11-15-2011, 04:21 AM   #14
k3lt01
Senior Member
 
Registered: Feb 2011
Location: Australia
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900

Original Poster
Rep: Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637
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

Last edited by k3lt01; 11-15-2011 at 04:28 AM.
 
Old 11-15-2011, 04:30 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
My very own Kernel build script for Debian/Ubuntu-based systems Kenny_Strawn Linux - Kernel 5 12-02-2010 09:57 AM
Shell script to compile a custom Linux kernel on Debian-based systems Kenny_Strawn Linux - Kernel 3 09-25-2010 01:29 PM
Bash script questions. ArthurHuang Programming 6 03-20-2007 10:59 PM
Couple of Shell Script Questions mathias1979 Programming 3 06-16-2005 03:22 PM
Couple Questions about Debian MikeeX Linux - Distributions 5 12-09-2002 08:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration