Installationsprogram

Här diskuterar vi skal, kommandon och klassiska linuxverktyg.
Användarvisningsbild
Osprey
Inlägg: 6310
Blev medlem: 06 apr 2008, 00:09
OS: Ubuntu
Ort: Göteborg/Mölndal/Falkenberg
Kontakt:

Installationsprogram

Inlägg av Osprey »

Jag har skrivit ett litet script som "sammanfogar" installationsprogrammen apt, apt-get, apt-cache, dpkg och så vidare i Ubuntu, Debian, Mint med SuSE, OpenSuSE, SLES, SLED inom SuSE och RHEL, CentOS och Fedora inom Redhat.

Tanken är den att det mesta är ju samma så länge man kör Linux, men just installationsprogrammen och det där med att hålla ordning på alla paket och saker man har installerat, skiljer sig stort. Dels genom skillnaden mellan ".deb" mot ".rpm", men sedan har ju rpm-baserade system olika verktyg också....

Så jag tänkte att det här borde man ju ganska enkelt kunna "snickra ihop ett paraply för", och det är där jag är....

Själv har jag Mint, CentOS och OpenSuSE på burken här och har testat så gott det går, men har inte hunnit/haft möjlighet att testa allting i scriptet....

Scriptet som finns här nedan, kallar jag för "pak" och om ni hinner med så får ni gärna hjälpa till genom att testa och sköta om all pakethantering via det, istället för apt, apt-get, yum, zypper etc....

Kanske kan det bli ett script som vi så småningom kör upstream, för att få ett enhetligt installationsverktyg i Linux...

Kör scriptet och kom med synpunkter, jag är medveten om att det än så länge finns en del att fixa till.... ;) :D ;D

Kod: Markera allt

#! /bin/bash
#
echo
if [[ -z $1 || $1 == "-h" || $1 == "--help" || $1 == "help" ]]; then
	cat << EOD
 pak {graphic|install|key|list|purge|reinstall|removei|rmlock|update|upgrade} [package]

     graphic    - start graphical interface, like synaptic, yast or yum, depending on distribution
     autoremove - remove packages that are no longer needed
     dist       - show the current dist
     help       - show this
     install    - install a named package
     list       - list installed packages, if you are searching for a specific package, use this as parameter
     purge      - remove a package and all its configuration files, if possible
     key        - add key, just for Ubuntu derived systems so far (not tested for anything else)
     reinstall  - reainstall a package, some differences between distibutins here
     remove     - remove a package, but leave its configuration files (in some distributons)
     rmlock     - remove the system wide lock for installation
     update     - update all package and repository info (Debian based) or perform "update/upgrade" (RedHat)
     upgrade    - upgrade all packages with info from "update" (Debian), upgrade everything (SuSE) or remove obsolete (RedHat)

     All functions can be abbreviated to the least number of characters that uniquely identifies them. Often this means
     two characters but some functions have to be specified with three characters. The only exception is remove, that are
     also accepted as "rm", on honor to SuSE fans and to follow old Un*x standard. However, I have determined to not support
     "up" as the eqivalence to "upgrade", due to the risk of likelihood of confusion with update and upgrade.

     The actual dist is determined automatically and currently Mint, Ubuntu, Debian, OpenSuSE, SLES, SLED, Fedora, CentOS and
     RedHat is supported. If you have a different dist, but the installation management is similar to any of these, you can
     override the normal way of dist determination by defining FORCEDIST.
     For example:

         If you for example are running Knoppix:

         export FORCEDIST=debian
         ...and then run this program...

 Example:
     pak list
     pak list zip
     pak install coreutils
     pak remove windows

EOD
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
		echo "no"
		return 1
	fi
}
###################################################################################################################################################
if [[ -z $FORCEDIST ]]; then
	source /etc/os-release
	DIST=$(echo $ID | awk -F \- '{ print $1 }')
else
	DIST=$(echo $FORCEDIST | tr '[:upper:]' '[:lower:]')
fi
FUNC=$1
shift
ARGS=$@
#
if [[ $DIST == "linuxmint" || $DIST == "ubuntu" || $DIST == "debian" ]]; then
	APT=$(which apt)
	if [[ $(abrv "gr*aph" $FUNC) == "ok" ]]; then
		# Graphical user interface.
		synaptic 2> /dev/null &
	elif [[ $(abrv "au*toremove" $FUNC) == "ok" ]]; then
		# Remove no longer needed packages
		apt-get autoremove
	elif [[ $(abrv "di*st" $FUNC) == "ok" ]]; 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
		TO_PURGE=$(dpkg -l|grep ^rc|awk '{ print $2 }')
		if [[ ! -z $TO_PURGE ]]; then
			aptitude purge $TO_PURGE
		fi
		TO_DEINSTALL=$(dpkg --get-selections | grep deinstall | cut -f1)
		if [[ ! -z $TO_DEINSTALL ]]; then
			dpkg --purge $TO_DEINSTALL
		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
			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 $APT ]]; then
			if [[ -z $ARGS ]]; then
				apt list --installed
			else
				apt list --installed | grep $ARGS
			fi
		else
			if [[ -z $ARGS ]]; then
				dpkg --get-selections
			else
				dpkg --get-selections | grep --color=always $ARGS
			fi
		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 "rml*ock" $FUNC) == "ok" ]]; then
		PID=$(lsof /var/lib/dpkg/lock 2> /dev/null | tail -1 | awk '{ print $2 }')
		if [[ ! -z $PID ]]; then
			echo
			echo "-The following process is locking yum"
			echo
			ps -o pid,user,command $PID | tail -1
			echo
			read -p "-Do you want to kill it? [y/N]: " IN
			if [[ $IN == "y" || $IN == "Y" ]]; then
				echo "Killing $PID"
				kill $PID
			else
				exit
			fi
		fi
		rm /var/lib/dpkg/lock
		echo
	elif [[ $(abrv "se*arch" $FUNC) == "ok" ]]; then
		apt-cache search $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 == "opensuse" || $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
		zypper verify
		zypper install-new-recommends
	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 $ARGS ]]; then
			# zypper --xmlout search --installed-only -v
			zypper packages --installed-only
		else
			zypper packages --installed-only | grep --color=always $ARGS
		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 "rml*ock" $FUNC) == "ok" ]]; then
		zypper removelock
	elif [[ $(abrv "se*arch" $FUNC) == "ok" ]]; then
	      :
	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) == "ok" ]]; then
		echo $DIST
	elif [[ $(abrv "fi*x" $FUNC) == "ok" ]]; then
		yum clean all
		yum check
		if [[ $ARGS == "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
		else
			rpm -i $ARGS
		fi
	elif [[ $(abrv "li*st" $FUNC) == "ok" ]]; then
		if [[ -z $ARGS ]]; then
			yum show-installed
		else
			yum show-installed | grep --color=always $ARGS
		fi
	elif [[ $(abrv "pu*rge" $FUNC) == "ok" ]]; then
		# Remove/uninstall a package and all its configuration files, plus unneeded packages.
		yum autoremove $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 "rml*ock" $FUNC) == "ok" ]]; then
		PID=$(cat /var/run/yum.pid)
		LINES=$(ps -o pid,user,command $PID | wc -l)
		if [[ $LINES -gt 1 ]]; then
			echo
			echo "-The following process is locking yum"
			echo
			ps -o pid,user,command $PID | tail -1
			echo
			read -p "-Do you want to kill it? [y/N]: " IN
			if [[ $IN == "y" || $IN == "Y" ]]; then
				echo "Killing"
				kill $PID
			else
				exit
			fi
			echo
		fi
		rm /var/run/yum.pid
	elif [[ $(abrv "se*arch" $FUNC) == "ok" ]]; then
		yum search $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
[EDIT] Ja ok jag blev själv först... :) Jag ska lägga till en kontroll av parametrar/options via getopt. Det är varken snyggt eller proffsigt så som det ser ut nu..... :)
Senast redigerad av 2 Osprey, redigerad totalt 2 gång.
Agera genom att ta och behålla initiativet, ta de risker detta kräver...
http://www.enargo.com/it/
OpenVMS Shark - i Linux finns inte SYS$CMKRNL...
Användarvisningsbild
johanre
Serveradmin
Inlägg: 3888
Blev medlem: 22 okt 2006, 09:13
OS: Ubuntu
Utgåva: 22.04 Jammy Jellyfish LTS
Ort: Malmö

Re: Installationsprogram

Inlägg av johanre »

Jag testar gärna men paketen heter ju olika inom de olika distributionerna, hur hanterar du det?

Sent from my MI 6 using Tapatalk
Användarvisningsbild
Osprey
Inlägg: 6310
Blev medlem: 06 apr 2008, 00:09
OS: Ubuntu
Ort: Göteborg/Mölndal/Falkenberg
Kontakt:

Re: Installationsprogram

Inlägg av Osprey »

Paketens namn är inga problem som jag ser det, där får man helt enkelt veta vad det paketet man ska installera heter...
Agera genom att ta och behålla initiativet, ta de risker detta kräver...
http://www.enargo.com/it/
OpenVMS Shark - i Linux finns inte SYS$CMKRNL...
Skriv svar

Återgå till "Terminalforum"