Efter att ha jobbat en massa gånger inom Debian/Ubuntu/Mint, SuSE/SLED/SLES och RedHat/CentOS/Fedora och hela tiden varit tvungen att sätta mig in i de skillnader som finns inom installation och pakethantering... så stod jag till slut inte ut längre, utan bestämde mig för att hacka ihop ett script som ger ett enhetligt gränssnitt....
Tyvärr har jag inte hunnit testa det fullt ut inom alla distributioner ännu, så de som vill och kan, får gärna göra det.....
Alltså, prova att köra det här scriptet (som jag valt att kalla för "pak") och se om det inom rimliga gränser kan ersätta "apt", "apt-get", "synaptic", "zypper", "yast" och "yum". Givetvis är det de ifrågavarande programmen som ligger till grund för det, det handlar bara om att få ett enhetligt gränssnitt....
Kom gärna med synpunkter, jag kämpar på....

Kod: Markera allt
#! /bin/bash
#
echo
#
if [[ -z $1 || $1 == "-h" || $1 == "--help" || $1 == "help" ]]; then
echo " pak {graphic|install|key|purge|reinstall|remove|update|upgrade} [package]"
echo
echo " graphic - start graphical interface, like synaptic, yast or yum, depending on distribution"
echo " autoremove - remove packages that are no longer needed"
echo " dist - show the current dist"
echo " help - show this"
echo " install - install a named package"
echo " list - list installed packages, if you are searching for a specific package, use this as parameter"
echo " purge - remove a package and all its configuration files, if possible"
echo " key - add key, just for Ubuntu derived systems so far (not tested for anything else)"
echo " reinstall - reainstall a package, some differences between distibutins here"
echo " remove - remove a package, but leave its configuration files (in some distributons)"
echo ' update - update all package and repository info (Debian based) or perform "update/upgrade" (RedHat)'
echo ' upgrade - upgrade all packages with info from "update" (Debian), upgrade everything (SuSE) or remove obsolete (RedHat)'
echo
echo " All functions can be abbreviated to the least number of characters that uniquely identifies them. Often this means"
echo " two characters but some functions have to be specified with three characters. The only exception is remove, that are"
echo " also accepted as "rm", on honor to SuSE fans and to follow old Un*x standard. However, I have determined to not support"
echo " "up" as the eqivalence to "upgrade", due to the risk of"
echo
echo " The actual dist is determined in different ways and currently "Mint", "Ubuntu", "Debian", "SuSE", "Fedora", "CentOS" and"
echo " RedHat is supported. If you have a different dist, but the installation management is similar to any of these, you can"
echo " override the normal way of dist determination by defining FORCEDIST."
echo " For example:"
echo
echo " If you for example are running Knoppix:"
echo
echo " export FORCEDIST=Debian"
echo " ...and then run this program..."
echo
echo " Example:"
echo " pak list"
echo " pak list zip"
echo " pak install coreutils"
echo " pak remove windows"
echo
exit
fi
#
###################################################################################################################################################
function abrv {
TEMPLATE=$1
CMD=$2
TMPL1=$(echo $TEMPLATE | awk -F \* '{ print $1 }')
TMPL2=$(echo $TEMPLATE | awk -F \* '{ print $2 }')
cLEN=$(echo -n $CMD | wc -c)
tLEN=$(echo -n $TMPL1 | wc -c)
dLEN=$(echo "$cLEN-$tLEN" | bc)
if [[ $dLEN -gt 0 ]]; then
M=$(echo -n $TMPL2 | cut -c -$dLEN)
A1=$TMPL1"$M"
else
A1=$TMPL1
fi
if [[ $A1 == $CMD ]]; then
echo "ok"
return 0
else
return 1
fi
}
###################################################################################################################################################
DEFAULT_OPTS="-l -r -c -v -i --"
#
function mint {
if [[ -e /etc/linuxmint/info ]]; then
if [[ $FULL == 1 ]]; then
OS_NAME=$(cat /etc/linuxmint/info | grep DESCRIPTION | awk -F = '{ print $2 }' | sed 's/"//g')
else
OS_NAME="Mint"
fi
else
return 2
fi
return 0
}
#############################################################################################
# LSB
#
function lsb {
lsb=$(which lsb_release 2> /dev/null)
if [[ ! -z $lsb ]]; then
if [[ -e $lsb ]]; then
if [[ $FULL ]]; then
OS_NAME=$(lsb_release -d | sed 's/Description://g' | sed 's/^ *//' | sed 's/^\t*//')
else
OS_NAME=$(lsb_release -d | awk '{ print $2 }' | sed 's/^ *//' | sed 's/^\t*//')
fi
return 0
else
return 2
fi
else
return 2
fi
}
function issue {
if [ -e /etc/issue ]; then
if [ ! -z /etc/issue ]; then
OS_NAME=$(strings /etc/issue |\
head -1 |\
sed 's/Welcome to //g' |\
awk '{ \
print $1
}')
else
return 2
fi
else
return 2
fi
return 0
}
function release {
dists=$(ls -r /etc/*-release 2>> /dev/null)
for dist in $dists; do
if [[ -e $dist ]]; then
if [[ ! -h $dist ]]; then
BN=$(basename $dist)
if [[ $BN != "lsb-release" && $BN != "os-release" ]]; then
if [[ $RTYPE == 0 ]]; then
if [[ ! FULL ]]; then
name=$(basename $dist|awk -F- '{ print $1 }')
OS_NAME=$(for i in $name; do \
B=`echo -n "${i:0:1}" |\
tr "[:lower:]" "[:upper:]"`;\
echo -n "${B}${i:1} ";\
done)
else
name=$(basename $dist|awk -F- '{ print $1 }')
fi
return 0
else
lines=$(cat $dist|wc -l)
if [[ $lines -ge 1 ]]; then
name=$(head -1 $dist|awk '{ print $1 }')
OS_NAME=$(for i in $name; do \
B=`echo -n "${i:0:1}" |\
tr "[:lower:]" "[:upper:]"`;\
echo -n "${B}${i:1} ";\
done)
return 0
fi
fi
elif [[ $BN == "os-release" ]]; then
OS_NAME=$(cat $dist | grep PRETTY_NAME | awk -F = '{ print $2 }' | sed 's/"//g')
return 0
elif [[ $BN == "lsb-release" ]]; then
OS_NAME=$(cat $dist | grep DISTRIB_DESCRIPTION | awk -F = '{ print $2 }' | sed 's/"//g')
return 0
fi
fi
fi
done
return 2
}
function version {
dists=$(ls /etc/*-version 2>> /dev/null)
for dist in $dists; do
if [[ -e $dist ]]; then
if [[ ! -h $dist ]]; then
if [[ $RTYPE == 0 ]]; then
name=$(basename $dist|awk -F- '{ print $1 }')
OS_NAME=$(for i in $name; do \
B=`echo -n "${i:0:1}" |\
tr "[:lower:]" "[:upper:]"`;\
echo -n "${B}${i:1} ";\
done)
return 0
else
lines=$(cat $dist|wc -l)
if [[ $lines -ge 1 ]]; then
name=$(head -1 $dist|awk '{ print $1 }')
OS_NAME=$(for i in $name; do \
B=`echo -n "${i:0:1}" |\
tr "[:lower:]" "[:upper:]"`;\
echo -n "${B}${i:1} ";\
done)
return 0
fi
fi
fi
fi
done
return 2
}
function dist {
OS=$(uname -s)
if [[ $OS == "Linux" ]]; then
FOUND_OS=false
PARAMS=$DEFAULT_OPTS
mint
if [[ $? == 0 ]]; then
FOUND_OS=true
FOUND="/etc/linuxmint"
else
lsb
if [[ $? == 0 ]]; then
FOUND_OS=true
FOUND="LSB (-l, --lsb)"
else
RTYPE=0
release
if [[ $? == 0 ]]; then
FOUND_OS=true
FOUND="Release (-r, --release)"
else
RTYPE=1
release
if [[ $? == 0 ]]; then
FOUND_OS=true
FOUND="Contents (-c, --contents)"
else
RTYPE=0
version
if [[ $? == 0 ]]; then
FOUND_OS=true
FOUND="Version (-v, --version)"
else
issue
if [[ $? == 0 ]]; then
FOUND_OS=true
FOUND="Issue"
fi
fi
fi
fi
fi
fi
elif [[ $OS == "AIX" ]]; then
# Mer info finns via "oslevel".
OS_NAME="AIX"
FOUND_OS=true
elif [[ $OS == "SunOS" ]]; then
# Mer info finns via "uname".
OS_NAME="Solaris"
FOUND_OS=true
elif [[ $OS == "HP-UX" ]]; then
OS_NAME="HP-UX"
FOUND_OS=true
elif [[ $OS == "OSF1" ]]; then
OS_NAME="Tru64"
FOUND_OS=true
elif [[ $OS == "FreeBSD" ]]; then
OS_NAME="FreeBSD"
FOUND_OS=true
elif [[ $OS == "OpenBSD" ]]; then
OS_NAME="OpenBSD"
FOUND_OS=true
elif [[ $OS == "NetBSD" ]]; then
OS_NAME="NetBSD"
FOUND_OS=true
fi
if [[ $FOUND_OS == true ]]; then
if [[ $DEBUG ]]; then
echo "Found via: $FOUND"
fi
echo $OS_NAME
return 0
fi
return 1
}
###################################################################################################################################################
if [[ -z $FORCEDIST ]]; then
DIST=$(dist | awk '{ print toupper($0) }')
else
DIST=$(echo $FORCEDIST | awk '{ print toupper($0) }')
fi
FUNC=$1
PARAM=$2
shift
ARGS=$@
#
if [[ $DIST == "MINT" || $DIST == "UBUNTU" || $DIST == "DEBIAN" ]]; then
if [[ $(abrv "gr*aph" $FUNC) == "ok" ]]; then
# Graphical user interface.
synaptic 2> /dev/null &
elif [[ $(abrv "au*toremove" $FUNC) ]]; then
# Remove no longer needed packages
apt-get autoremove
elif [[ $(abrv "di*st" $FUNC) ]]; then
echo $DIST
elif [[ $(abrv "fi*x" $FUNC) == "ok" ]]; then
dpkg --configure -a
apt-get clean
apt-get check
apt-get -f install
apt-get autoremove
apt-get update
aptitude purge $(dpkg -l|grep ^rc|awk '{ print $2 }')
dpkg --purge `dpkg --get-selections | grep deinstall | cut -f1`
elif [[ $(abrv "in*stall" $FUNC) == "ok" ]]; then
# Install a new, previously not installed package.
PKG=$(echo $ARGS | awk -F . '{ print $1 }')
EXT=$(echo $ARGS | awk -F . '{ print $2 }')
if [[ -z $EXT ]]; then
apt-get install $ARGS
elif [[ $EXT == "deb" ]]; then
dpkg -i $ARGS
elif [[ $EXT == "rpm" ]]; then
if [[ ! -z $(which rpm) ]]; then
rpm -ivh $ARGS
elif [[ ! -z $(which alien) ]]; then
alien --to-deb $ARGS
dpkg -i $PKG".deb"
else
echo "-Cannot install $ARGS, rpm or alien does not exist - consider installing one of them"
fi
else
echo "-Unknown format .$EXT"
fi
elif [[ $(abrv "li*st" $FUNC) == "ok" ]]; then
if [[ -z $PARAM ]]; then
dpkg --get-selections
else
dpkg --get-selections | grep --color=always $PARAM
fi
elif [[ $(abrv "pu*rge" $FUNC) == "ok" ]]; then
# Remove/uninstall a package and all its configuration files.
apt-get purge $ARGS
elif [[ $(abrv "ke*y" $FUNC) == "ok" ]]; then
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com $ARGS
elif [[ $(abrv "rei*nstall" $FUNC) == "ok" ]]; then
# Reinstall an already installed package. Most of its configuration
# files will possibly be overwritten.
apt-get install --reinstall $ARGS
elif [[ $(abrv "rem*ove" $FUNC) == "ok" || $FUNC == "rm" ]]; then
# Remove/uninstall a package.
apt-get remove $ARGS
elif [[ $(abrv "upd*ate" $FUNC) == "ok" ]]; then
# Update information about new packages.
apt-get update
elif [[ $(abrv "upg*rade" $FUNC) == "ok" ]]; then
# Upgrade all packages.
apt-get upgrade
apt-get dist-upgrade
else
echo '-Can not understand what function "'$FUNC'" means...'
fi
elif [[ $DIST == "SUSE" || $DIST == "SLES" || $DIST == "SLED" || $DIST == "SLE" ]]; then
if [[ $(abrv "gr*aph" $FUNC) == "ok" ]]; then
# Graphical user interface.
yast >2 /dev/null &
elif [[ $(abrv "au*toremove" $FUNC) == "ok" ]]; then
# Remove no longer needed packages
# zypper packages --orphaned <-- Removes manually installed
zypper packages --unneeded
elif [[ $(abrv "di*st" $FUNC) ]]; then
echo $DIST
elif [[ $(abrv "fi*x" $FUNC) == "ok" ]]; then
# !! Don't know how to fix SuSE...
:
elif [[ $(abrv "in*stall" $FUNC) == "ok" ]]; then
# Install a new, previously not installed package.
PKG=$(echo $ARGS | awk -F . '{ print $1 }')
EXT=$(echo $ARGS | awk -F . '{ print $2 }')
if [[ -z $EXT ]]; then
zypper install $ARGS
fi
elif [[ $(abrv "li*st" $FUNC) == "ok" ]]; then
if [[ -z $PARAM ]]; then
# zypper --xmlout search --installed-only -v
zypper packages --installed-only
else
zypper packages --installed-only | grep --color=always $PARAM
fi
elif [[ $(abrv "pu*rge" $FUNC) == "ok" ]]; then
# Remove/uninstall a package and all its configuration files.
zypper rm --clean-deps $ARGS
elif [[ $(abrv "rei*nstall" $FUNC) == "ok" ]]; then
# Reinstall an already installed package. Most of its configuration
# files will possibly be overwritten.
# !! zypper does not really have a "reinstall", so instead the package
# !! is force installed with "-f" (which in this case means "overwrite").
zypper install -f $ARGS
elif [[ $(abrv "rem*ove" $FUNC) == "ok" || $FUNC == "rm" ]]; then
# Remove/uninstall a package.
zypper rm $ARGS
elif [[ $(abrv "upd*ate" $FUNC) == "ok" ]]; then
# Update information about new packages.
zypper refresh
elif [[ $(abrv "upg*rade" $FUNC) == "ok" ]]; then
# Upgrade all packages.
zypper update
else
echo '-Can not understand what function "'$FUNC'" means...'
fi
elif [[ $DIST == "FEDORA" || $DIST == "CENTOS" || $DIST == "REDHAT" || $DIST == "RHEL" ]]; then
if [[ $(abrv "gr*aph" $FUNC) == "ok" ]]; then
# Graphical user interface.
yum >2 /dev/null &
elif [[ $(abrv "au*toremove" $FUNC) == "ok" ]]; then
# Remove no longer needed packages
yum autoremove
elif [[ $(abrv "di*st" $FUNC) ]]; then
echo $DIST
elif [[ $(abrv "fi*x" $FUNC) == "ok" ]]; then
yum clean all
yum check
if [[ $PARAM == "all" ]]; then
rm /var/lib/__db*
rpm --rebuilddb
rpmdb_verify Packages
fi
elif [[ $(abrv "in*stall" $FUNC) == "ok" ]]; then
# Install a new, previously not installed package.
PKG=$(echo $ARGS | awk -F . '{ print $1 }')
EXT=$(echo $ARGS | awk -F . '{ print $2 }')
if [[ -z $EXT ]]; then
yum install $ARGS
fi
elif [[ $(abrv "li*st" $FUNC) == "ok" ]]; then
if [[ -z $PARAM ]]; then
yum show-installed
else
yum show-installed | grep --color=always $PARAM
fi
elif [[ $(abrv "pu*rge" $FUNC) == "ok" ]]; then
# Remove/uninstall a package and all its configuration files.
# Remove/uninstall a package.
# !! erase is exactly the same as remove.
yum erase $ARGS
elif [[ $(abrv "rei*nstall" $FUNC) == "ok" ]]; then
# Reinstall an already installed package. Most of its configuration
# files will possibly be overwritten.
yum reinstall $ARGS
elif [[ $(abrv "rem*ove" $FUNC) == "ok" || $FUNC == "rm" ]]; then
# Remove/uninstall a package.
yum remove $ARGS
elif [[ $(abrv "upd*ate" $FUNC) == "ok" ]]; then
# Update information about new packages.
yum check-update $ARGS
elif [[ $(abrv "upg*rade" $FUNC) == "ok" ]]; then
# Upgrade all packages.
yum update $ARGS
yum upgrade $ARGS
else
echo '-Can not understand what function "'$FUNC'" means...'
fi
else
echo "-Unsupported dist - $DIST"
fi
#echo "DIST: $DIST"
echo