-
Notifications
You must be signed in to change notification settings - Fork 4
/
aupdate
executable file
·57 lines (48 loc) · 1.53 KB
/
aupdate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
set -euo pipefail
# Run apt update && apt upgrade
#
# This script exists to take advantage of sudo apt update being configured to
# run passwordless. Unlike the previous alias, this command will not prompt for
# a sudo password unless there are packages to upgrade.
#
# Sample /etc/sudoers.d/apt-update:
# # Allow members of sudo to run apt update passwordless
# %sudo ALL=(ALL) NOPASSWD: /usr/bin/apt update
# %sudo ALL=(ALL) NOPASSWD: /usr/bin/apt-get update
# %sudo ALL=(ALL) NOPASSWD: /usr/bin/aptitude update
#
run() {
echo >&2 "+ $*"
"$@"
}
# usage: faketty COMMAND_STRING
#
# Make a command think stdout is a tty.
#
# Useful for commands like apt that disable colors and print warnings when
# their output is captured.
#
# NOTE 1: do not pass untrusted input to script, it will be run by a shell.
# NOTE 2: this may make it easier to observe input/output via strace, so don't
# use faketty where secrets may be input or output.
#
faketty() {
if [ $# -ne 1 ]; then
echo >&2 "usage: faketty COMMAND_STRING"
return 1
fi
script -qefc "$1" /dev/null
}
# Run apt update and tee output to variable and to terminal.
# Use faketty to make apt think stdout is a tty so it keeps color.
# We pass -n to sudo to ensure that it never prompts for a password, given that
# we're wrapping i/o with script.
{
output=$(faketty 'set -x; sudo -n apt update' | tee /dev/fd/3)
} 3>&1
if grep '^All packages are up to date.' <<< "$output" >/dev/null; then
exit
fi
run apt list --upgradeable
run sudo apt upgrade "$@"