Last weekend (and the weeks before
I put my thoughts into IRC, which gave me some answers from chattr and glebihan. With these under my arm, I intended to script something in Perl, as that's a language I really want to learn and of which I think it's stronger than just good ol' shell.
However... I didn't have the slightest idea on how to script Perl!!
Here's the shell-script that I whipped up in a just few minutes:
- Code: Select all
#!/bin/bash
### Just some globals and checks to keep me sane,
### the outpout correct and the filesystem clean.
temp_good=/tmp/apt_temp_good.txt
temp_fail=/tmp/apt_temp_fail.txt
if [ -w ${temp_good} ]
then
rm -f ${temp_good}
fi
if [ -w ${temp_fail} ]
then
rm -f ${temp_fail}
fi
### Gotta have something to search for...
if [ -z ${1} ]
then
echo "Give parameter to work with..."
exit 1
fi
### Do your stuff!
for i in `apt-cache search ${1} | awk -F " - " '{ print $1 }'`
do
result=`dpkg-query -W ${i} 2>/dev/null`
if [[ `echo ${result} | grep -v "No packages found matching"` == "" ]]
then
echo "${i}" >> ${temp_fail}
else
echo "${i}" >> ${temp_good}
fi
done
### Let the user know the output.
echo "############# Installed #################"
cat ${temp_good} 2>/dev/null
echo "############# Available #################"
cat ${temp_fail} 2>/dev/null
exit 0
Wanting to do this in Perl, it took me numerous hours to achieve the same thing:
- Code: Select all
#!/usr/bin/perl
#use diagnostics;
$i=0;
$j=0;
$k=0;
$ls_cmd = 'sudo apt-cache search';
$dpkg_cmd = 'dpkg-query -W 2>&1';
open(LS_CMD, "$ls_cmd $ARGV[0] |") or die "Can't run '$ls_cmd'\n$!\n";
while(<LS_CMD>){
@fields = split (" - ",$_);
$ls_list[$i++] = "$fields[0]" ; # save output line in the array
}
open(DPKG_CMD, "$dpkg_cmd 2>&1 @ls_list |") or die "Can't run '$dpkg_cmd'\n$!\n";
while (<DPKG_CMD>){
if ($_ =~ m/^No packages/){
@package = split(/ /,$_);
$ls_notpresent[$j++] = "$package[-1]" ;
} else {
$ls_output[$k++] = "$_" ;
}
}
print "################ Installed ############### \n";
print @ls_output;
print "################ Available ############### \n";
print @ls_notpresent;
Important note: the Perl-version is MUCH quicker, as it runs dpkg-query once with a lot of parameters, instead of running dpkg-query a lot of times with a single parameter.
I have to admit, Perl is much more complex but also much, MUCH more flexible... This example reallly makes me want to learn more!
The result of either scripts with parameter "geany" would look like this:
- Code: Select all
################ Installed ###############
geany 0.20-0ubuntu2
geany-plugins
geany-plugins-common
################ Available ###############
geany-plugin-addons.
geany-plugin-codenav.
geany-plugin-doc.
geany-plugin-extrasel.
geany-plugin-gdb.
geany-plugin-gendoc.
geany-plugin-insertnum.
geany-plugin-latex.
geany-plugin-lipsum.
geany-plugin-lua.
geany-plugin-prettyprinter.
geany-plugin-prj.
geany-plugin-sendmail.
geany-plugin-shiftcolumn.
geany-plugin-spellcheck.
geany-plugin-treebrowser.
geany-plugin-updatechecker.
geany-plugin-vc.
geany-plugin-webhelper.
The shell-script will not display versions on installed packages. One thing in the Perl-version I can't get my head around is to remove the period at the end of the package-names that aren't installed...
Feel free to address any issues and mention any improvements you see!
Anakin






