天天看點

linux更新一個軟體包,使用apt-get在Ubuntu/Debian系統中更新單個軟體包的方法

本文介紹在Ubuntu/Debian系統中使用apt-get更新單個軟體包,更新軟體包的最常用方法是運作指令sudo apt-get upgrade,但是這樣做的結果是所有已安裝的軟體包都将更新到配置的Ubuntu存儲庫中可用的最新版本。

linux更新一個軟體包,使用apt-get在Ubuntu/Debian系統中更新單個軟體包的方法

使用apt-get在Ubuntu/Debian中更新單個軟體包

1、要在任何基于Ubuntu/Debian的系統中使用apt-get(Debian和Ubuntu最實用的apt-get指令詳解)更新單個軟體包,請使用以下syntax:

sudo apt-get install --only-upgrade packagename

這适用于所有Ubuntu、Debian和Linux Mint系統,将packagename替換為要更新的程式包的名稱即可。

2、請參閱下面的示例以更新ubuntu-keyring:

$ sudo apt-get install --only-upgrade ubuntu-keyring

Reading package lists… Done

Building dependency tree

Reading state information… Done

The following packages will be upgraded:

ubuntu-keyring

1 upgraded, 0 newly installed, 0 to remove and 190 not upgraded.

Need to get 22.4 kB of archives.

After this operation, 4,096 B of additional disk space will be used.

Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 ubuntu-keyring all 2018.09.18.1~18.04.0 [22.4 kB]

Fetched 22.4 kB in 1s (34.5 kB/s)

(Reading database … 261497 files and directories currently installed.)

Preparing to unpack …/ubuntu-keyring_2018.09.18.1~18.04.0_all.deb …

Unpacking ubuntu-keyring (2018.09.18.1~18.04.0) over (2018.02.28) …

Setting up ubuntu-keyring (2018.09.18.1~18.04.0) …

在Ubuntu/Debian/Linux Mint中更新多個軟體包

1、要更新多個包,請用空格分隔它們:

sudo apt-get install --only-upgrade thunderbird cinnamon cinnamon-common

2、如果要更新所有已安裝的軟體包,請使用以下指令:

sudo apt-get upgrade

使用腳本在Ubuntu中更新單個程式包/多個程式包

1、我們可以建立一個簡單的腳本,每當你想在Ubuntu或Debian系統中使用apt-get更新單個包時,我們就會将包名稱作為參數傳遞。使用以下内容在/usr/local/bin/myupgrade中建立腳本:

#!/bin/bash

pakage_names="$@"

[[ -z $pakage_names ]] && { echo "Usage: $(basename $0) package1 package 2 package.."; exit 1; }

# Check if package is already installed

for package in ${pakage_names[@]}; do

if dpkg -s "$package" 2>/dev/null | grep -q Status.*installed; then

echo "Attempting to upgrade $package"

sudo apt-get --only-upgrade -y install $package

else

echo "Package $package is not installed, install it (y/n): "

read selection

if [[ $selection == "y" ]] || [[ $selection == "Y" ]]; then

sudo apt-get -y install $package

else

echo "Okay!, next time"

fi

fi

done

2、給腳本一個執行位:

sudo chmod +x /usr/local/bin/myupgrade

3、必須将參數傳遞給腳本才能更新包:

$ myupgrade

Usage: myupgrade package1 package 2 package..

4、要更新單個包,請在末尾傳遞單個參數

$ myupgrade  util-linux

Attempting to upgrade util-linux

[sudo] password for jmutai:

Reading package lists… Done

Building dependency tree

Reading state information… Done

Suggested packages:

util-linux-locales

The following packages will be upgraded:

util-linux

1 upgraded, 0 newly installed, 0 to remove and 182 not upgraded.

Need to get 902 kB of archives.

After this operation, 0 B of additional disk space will be used.

Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 util-linux amd64 2.31.1-0.4ubuntu3.3 [902 kB]

Fetched 902 kB in 2s (518 kB/s)

(Reading database … 261497 files and directories currently installed.)

Preparing to unpack …/util-linux_2.31.1-0.4ubuntu3.3_amd64.deb …

Unpacking util-linux (2.31.1-0.4ubuntu3.3) over (2.31.1-0.4ubuntu3.2) …

Setting up util-linux (2.31.1-0.4ubuntu3.3) …

Processing triggers for mime-support (3.60ubuntu1) …

Processing triggers for ureadahead (0.100.0-20) …

Processing triggers for systemd (237-3ubuntu10.9) …

Processing triggers for man-db (2.8.3-2ubuntu0.1) …

5、對于多個包,請提供以空格分隔的名稱:

$ myupgrade nplan openssl perl

6、如果尚未安裝軟體包,腳本将詢問你是否要安裝它,按“Y”或“y”确認:

$ myupgrade elinks

Package elinks is not installed, install it (y/n)

y

Reading package lists… Done

Building dependency tree

Reading state information… Done

The following additional packages will be installed:

elinks-data libfsplib0 liblua5.1-0 libtre5

Suggested packages:

elinks-doc tre-agrep

The following NEW packages will be installed:

elinks elinks-data libfsplib0 liblua5.1-0 libtre5

0 upgraded, 5 newly installed, 0 to remove and 169 not upgraded.

Need to get 1,062 kB of archives.

至此,操作完成。

相關主題