1 # systemctl(1) completion -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1+
4 # This file is part of systemd.
6 # Copyright 2010 Ran Benita
8 # systemd is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 2.1 of the License, or
11 # (at your option) any later version.
13 # systemd is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 local mode=$1; shift 1
23 systemctl $mode --full --no-legend "$@" 2>/dev/null
26 __systemd_properties() {
28 { __systemctl $mode show --all;
29 @rootlibexecdir@/systemd --dump-configuration-items; } |
30 while IFS='=' read -r key value; do
31 [[ $value ]] && echo "$key"
36 local w word=$1; shift
38 [[ $w = "$word" ]] && return
42 __filter_units_by_property () {
43 local mode=$1 property=$2 value=$3 ; shift 3
46 IFS=$'\n' read -rd '' -a props < \
47 <(__systemctl $mode show --property "$property" -- "${units[@]}")
48 for ((i=0; $i < ${#units[*]}; i++)); do
49 if [[ "${props[i]}" = "$property=$value" ]]; then
55 __filter_units_by_properties () {
56 local mode=$1 properties=$2 values=$3 ; shift 3
58 local props i j conditions=()
59 IFS=$'\n' read -rd '' -a props < \
60 <(__systemctl $mode show --property "$properties" -- "${units[@]}")
61 IFS=$',' read -r -a properties < <(echo $properties)
62 IFS=$',' read -r -a values < <(echo $values)
63 for ((i=0; i < ${#properties[*]}; i++)); do
64 for ((j=0; j < ${#properties[*]}; j++)); do
65 if [[ ${props[i]%%=*} == ${properties[j]} ]]; then
66 conditions+=( "${properties[j]}=${values[j]}" )
70 for ((i=0; i < ${#units[*]}; i++)); do
71 for ((j=0; j < ${#conditions[*]}; j++)); do
72 if [[ "${props[i * ${#conditions[*]} + j]}" != "${conditions[j]}" ]]; then
76 if (( j == ${#conditions[*]} )); then
82 __get_all_units () { { __systemctl $1 list-unit-files; __systemctl $1 list-units --all; } \
83 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; }
84 __get_template_names () { __systemctl $1 list-unit-files \
85 | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; }
87 __get_active_units () { __systemctl $1 list-units \
88 | { while read -r a b; do echo " $a"; done; }; }
89 __get_startable_units () {
90 # find startable inactive units
91 __filter_units_by_properties $1 ActiveState,CanStart inactive,yes $(
92 { __systemctl $1 list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient | \
93 { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }
94 __systemctl $1 list-units --state inactive,failed | \
95 { while read -r a b c; do [[ $b == "loaded" ]] && echo " $a"; done; }
98 __get_restartable_units () {
99 # filter out masked and not-found
100 __filter_units_by_property $1 CanStart yes $(
101 __systemctl $1 list-unit-files --state enabled,disabled,static | \
102 { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }
103 __systemctl $1 list-units | \
104 { while read -r a b; do echo " $a"; done; } )
106 __get_failed_units () { __systemctl $1 list-units \
107 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
108 __get_enabled_units () { __systemctl $1 list-unit-files \
109 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
110 __get_disabled_units () { __systemctl $1 list-unit-files \
111 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
112 __get_masked_units () { __systemctl $1 list-unit-files \
113 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
114 __get_all_unit_files () { { __systemctl $1 list-unit-files; } | { while read -r a b; do echo " $a"; done; }; }
118 { machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; } | \
119 { while read a b; do echo " $a"; done; }
123 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
124 local i verb comps mode
127 [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global
128 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall --now
129 --quiet -q --privileged -P --system --user --version --runtime --recursive -r --firmware-setup
130 --show-types -i --ignore-inhibitors --plain --failed'
131 [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root
132 --preset-mode -n --lines -o --output -M --machine'
135 if __contains_word "--user" ${COMP_WORDS[*]}; then
137 elif __contains_word "--global" ${COMP_WORDS[*]}; then
143 if __contains_word "$prev" ${OPTS[ARG]}; then
150 comps=$(__systemctl $mode -t help)
153 comps=$(__systemctl $mode --state=help)
156 comps='fail replace replace-irreversibly isolate
157 ignore-dependencies ignore-requirements flush'
160 comps='all control main'
163 comps=$(compgen -A directory -- "$cur" )
167 comps=$(compgen -A hostname)
170 comps=$(__systemd_properties $mode)
173 comps='full enable-only disable-only'
176 comps='short short-full short-iso short-iso-precise short-precise short-monotonic short-unix verbose export json
177 json-pretty json-sse cat'
180 comps=$( __get_machines )
183 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
187 if [[ "$cur" = -* ]]; then
188 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
193 [ALL_UNITS]='is-active is-failed is-enabled status show cat mask preset help list-dependencies edit set-property revert'
194 [ENABLED_UNITS]='disable'
195 [DISABLED_UNITS]='enable'
196 [REENABLABLE_UNITS]='reenable'
197 [FAILED_UNITS]='reset-failed'
198 [STARTABLE_UNITS]='start'
199 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
200 [ISOLATABLE_UNITS]='isolate'
201 [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload'
202 [RESTARTABLE_UNITS]='restart reload-or-restart'
203 [TARGET_AND_UNITS]='add-wants add-requires'
204 [MASKED_UNITS]='unmask'
206 [ENVS]='set-environment unset-environment'
207 [STANDALONE]='daemon-reexec daemon-reload default
208 emergency exit halt hibernate hybrid-sleep kexec list-jobs
209 list-sockets list-timers list-units list-unit-files poweroff
210 reboot rescue show-environment suspend get-default
212 [FILE]='link switch-root'
213 [TARGETS]='set-default'
216 for ((i=0; i < COMP_CWORD; i++)); do
217 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
218 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
219 verb=${COMP_WORDS[i]}
224 if [[ -z $verb ]]; then
227 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
228 comps=$( __get_all_units $mode )
231 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
232 comps=$( __get_enabled_units $mode )
235 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
236 comps=$( __get_disabled_units $mode;
237 __get_template_names $mode)
240 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
241 comps=$( __get_disabled_units $mode;
242 __get_enabled_units $mode;
243 __get_template_names $mode)
246 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
247 comps=$( __get_startable_units $mode;
248 __get_template_names $mode)
251 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
252 comps=$( __get_restartable_units $mode;
253 __get_template_names $mode)
256 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
257 comps=$( __filter_units_by_property $mode CanStop yes \
258 $( __get_active_units $mode ) )
261 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
262 comps=$( __filter_units_by_property $mode CanReload yes \
263 $( __get_active_units $mode ) )
266 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
267 comps=$( __filter_units_by_property $mode AllowIsolate yes \
268 $( __get_all_units $mode ) )
271 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
272 comps=$( __get_failed_units $mode )
275 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
276 comps=$( __get_masked_units $mode )
279 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
280 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \
281 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
282 comps=$( __systemctl $mode list-unit-files --type target --all \
283 | { while read -r a b; do echo " $a"; done; } )
285 comps=$( __get_all_unit_files $mode )
289 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
292 elif __contains_word "$verb" ${VERBS[JOBS]}; then
293 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
295 elif __contains_word "$verb" ${VERBS[ENVS]}; then
296 comps=$( __systemctl $mode show-environment \
297 | while read -r line; do echo " ${line%%=*}=";done )
300 elif __contains_word "$verb" ${VERBS[FILE]}; then
301 comps=$( compgen -A file -- "$cur" )
303 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
304 comps=$( __systemctl $mode list-unit-files --type target --full --all \
305 | { while read -r a b; do echo " $a"; done; } )
308 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
312 complete -F _systemctl systemctl