Problems with Bash script to install packages

Q I'm writing a Bash script that, among other things, installs assorted packages. I'm using Zenity to make it look sexy in a GUI sort of way and running it on 64-bit Debian 5.0. Unfortunately, if one of the packages I want to install is available on the Lenny installation DVD, the thing just sits there: under the hood, it has said 'please insert DVD', but Zenity doesn't see that and doesn't relay that. So to the user, it just looks as if the thing has hung. The relevant bit of code is as follows:

Select all
( echo "33"
aptitude -y install gcc
echo "66"
aptitude -y install sysstat
echo "99"
) | zenity --auto-close --progress --text="Fetching software..." --title="Installing Software " --width 300

A number of questions arise. First, is there a way to make Zenity 'event aware' so that it displays messages like 'please insert your installation DVD'? I don't think there is, but I could well be wrong. If so that would be the ideal solution and you don't have to read this question to the end (just let me know the relevant piece of magic!). Alternatively, I could write code that says 'if they've got the installation DVD in their

/etc/ apt/sources.list, comment it out. And 'if they haven't got http://volatile.debian.org/debian- volatile in their sources, add it'.

Unfortunately, to analyse the textual contents of files, I can feel heavy doses of awk and sed coming on, neither of which make me feel particularly clever, inspired or confident! In fact, I'm clueless about both, despite reading the man pages and Google articles about them until I'm cross-eyed.

Would you therefore be able to suggest some Bash code that tests for the existence of the two sources in the sources.list file, removes the DVD one if present, adds the online one if it's missing and doesn't add it if it is already present?!

A Is the "please insert" message being sent to standard error rather than standard output? If so, you should be able to capture it by adding 2>&1 to your aptitude call. Then you should look at the output for the relevant string before passing it to Zenity. You are quite correct in your assumption that modifying text files from the command line needs sed or awk - sed in this case - but these are tools well worth learning. They can be daunting at first, but are indispensable once you get the hang of them. To comment out any CD/DVD source from sources.list, you would use this command

sed  -i 's/^deb cdrom/# deb cdrom/' /etc/apt/sources.list

To break this down into manageable chunks, the -i means 'replace the existing file with the modified contents'; the s means 'substitute anything matching the first string with the second'. So this replaces any instance of deb cdrom at the start of a line with # deb cdrom. This is the same way that Synaptic modifies the file so the source can be re-enabled in there should the need arise.

Adding a source line is as simple as echoing it to the file, but messing with someone's sources file is not particularly friendly, so you could check whether you need to make the modifications with grep, back up the file before changing it and restore the backup when you've finished.

cp /etc/apt/sources.list /etc/apt/sources.list.$$
sed  -i 's/^deb cdrom/# deb cdrom/' /etc/apt/sources.list
grep -q "^deb http://volatile.debian.org/debian-volatile" /etc/apt/sources.list || echo "repository line" >>/etc/apt/sources.list
# do your stuff here
if diff -q /etc/apt/sources.list /etc/apt/sources.list.$$
then
rm /etc/apt/sources.list
else
mv /etc/apt/sources.list.$$ /etc/apt/sources.list
fi

$$ is the current process number, giving a unique(ish) name for the backup file. The grep command means your repository is only added if missing, and the final part checks whether sources.list has changed, and restores the old one if so.

Back to the list