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
10 systemctl $mode --full --no-legend --no-pager "$@" 2>/dev/null
13 __systemd_properties() {
14 @rootlibexecdir@/systemd --dump-bus-properties
18 local w word=$1; shift
20 [[ $w = "$word" ]] && return
24 __filter_units_by_properties () {
25 local mode=$1 properties=$2; shift 2
30 IFS=$',' read -r -a p < <(echo "Names,$properties")
32 readarray -t props < \
33 <(__systemctl $mode show --property "Names,$properties" -- "${units[@]}")
35 for ((i=0; i < ${#props[*]}; i++)); do
36 if [[ -z ${props[i]} ]]; then
37 if (( count == n )) && [[ -n $names ]]; then
44 if [[ ${props[i]%%=*} == 'Names' ]]; then
49 if (( count == n )) && [[ -n $names ]]; then
54 __get_all_units () { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
55 | { while read -r a b; do echo " $a"; done; }; }
56 __get_non_template_units() { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
57 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; }
58 __get_template_names () { __systemctl $1 list-unit-files "$2*" \
59 | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; }
60 __get_active_units () { __systemctl $1 list-units "$2*" \
61 | { while read -r a b; do echo " $a"; done; }; }
63 __get_not_masked_unit_files() {
64 # filter out masked, not-found, or template units.
65 __systemctl $1 list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$2*" | \
66 { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }
69 __get_startable_units () {
70 __filter_units_by_properties $1 ActiveState=inactive,CanStart=yes $(
71 { __get_not_masked_unit_files $1 $2
72 # get inactive template units
73 __systemctl $1 list-units --state inactive,failed "$2*" | \
74 { while read -r a b c; do [[ $b == "loaded" ]] && echo " $a"; done; }
77 __get_restartable_units () {
78 # filter out masked and not-found
79 __filter_units_by_properties $1 CanStart=yes $(
80 { __get_not_masked_unit_files $1 $2
81 __get_active_units $1 $2
85 __get_stoppable_units () {
86 # filter out masked and not-found
87 __filter_units_by_properties $1 ActiveState=active,CanStop=yes $(
88 { __get_not_masked_unit_files $1 $2
89 __get_active_units $1 $2
93 __get_reloadable_units () {
94 # filter out masked and not-found
95 __filter_units_by_properties $1 ActiveState=active,CanReload=yes $(
96 { __get_not_masked_unit_files $1 $2
97 __get_active_units $1 $2
101 __get_failed_units () { __systemctl $1 list-units "$2*" \
102 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
103 __get_enabled_units () { __systemctl $1 list-unit-files "$2*" \
104 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
105 __get_disabled_units () { __systemctl $1 list-unit-files "$2*" \
106 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
107 __get_masked_units () { __systemctl $1 list-unit-files "$2*" \
108 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
109 __get_all_unit_files () { { __systemctl $1 list-unit-files "$2*"; } | { while read -r a b; do echo " $a"; done; }; }
113 { machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; } | \
114 { while read a b; do echo " $a"; done; }
118 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
119 local i verb comps mode
122 [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global
123 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall --now
124 --quiet -q --system --user --version --runtime --recursive -r --firmware-setup
125 --show-types -i --ignore-inhibitors --plain --failed --value --fail --dry-run --wait'
126 [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root
127 --preset-mode -n --lines -o --output -M --machine --message'
130 if __contains_word "--user" ${COMP_WORDS[*]}; then
132 elif __contains_word "--global" ${COMP_WORDS[*]}; then
138 if __contains_word "$prev" ${OPTS[ARG]}; then
145 comps=$(__systemctl $mode -t help)
148 comps=$(__systemctl $mode --state=help)
151 comps='fail replace replace-irreversibly isolate
152 ignore-dependencies ignore-requirements flush'
155 comps='all control main'
158 comps=$(compgen -A directory -- "$cur" )
162 comps=$(compgen -A hostname)
165 comps=$(__systemd_properties)
168 comps='full enable-only disable-only'
171 comps='short short-full short-iso short-iso-precise short-precise short-monotonic short-unix verbose export json
172 json-pretty json-sse json-seq cat with-unit'
175 comps=$( __get_machines )
178 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
182 if [[ "$cur" = -* ]]; then
183 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
188 [ALL_UNITS]='cat mask'
189 [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert'
190 [ENABLED_UNITS]='disable'
191 [DISABLED_UNITS]='enable'
192 [REENABLABLE_UNITS]='reenable'
193 [FAILED_UNITS]='reset-failed'
194 [STARTABLE_UNITS]='start'
195 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
196 [ISOLATABLE_UNITS]='isolate'
197 [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload'
198 [RESTARTABLE_UNITS]='restart reload-or-restart'
199 [TARGET_AND_UNITS]='add-wants add-requires'
200 [MASKED_UNITS]='unmask'
202 [ENVS]='set-environment unset-environment import-environment'
203 [STANDALONE]='daemon-reexec daemon-reload default
204 emergency exit halt hibernate hybrid-sleep
205 suspend-then-hibernate kexec list-jobs list-sockets
206 list-timers list-units list-unit-files poweroff
207 reboot rescue show-environment suspend get-default
208 is-system-running preset-all'
209 [FILE]='link switch-root'
210 [TARGETS]='set-default'
211 [MACHINES]='list-machines'
214 for ((i=0; i < COMP_CWORD; i++)); do
215 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
216 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
217 verb=${COMP_WORDS[i]}
222 if [[ -z $verb ]]; then
225 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
226 comps=$( __get_all_units $mode "$cur" )
229 elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then
230 comps=$( __get_non_template_units $mode "$cur" )
233 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
234 comps=$( __get_enabled_units $mode "$cur" )
237 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
238 comps=$( __get_disabled_units $mode "$cur";
239 __get_template_names $mode "$cur")
242 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
243 comps=$( __get_disabled_units $mode "$cur";
244 __get_enabled_units $mode "$cur";
245 __get_template_names $mode "$cur")
248 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
249 comps=$( __get_startable_units $mode "$cur" )
252 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
253 comps=$( __get_restartable_units $mode "$cur" )
256 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
257 comps=$( __get_stoppable_units $mode "$cur" )
260 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
261 comps=$( __get_reloadable_units $mode "$cur" )
264 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
265 comps=$( __filter_units_by_properties $mode AllowIsolate=yes \
266 $( __get_all_units $mode "$cur" ) )
269 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
270 comps=$( __get_failed_units $mode "$cur" )
273 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
274 comps=$( __get_masked_units $mode "$cur" )
277 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
278 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \
279 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
280 comps=$( __systemctl $mode list-unit-files --type target --all "$cur*" \
281 | { while read -r a b; do echo " $a"; done; } )
283 comps=$( __get_all_unit_files $mode "$cur" )
287 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
290 elif __contains_word "$verb" ${VERBS[JOBS]}; then
291 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
293 elif __contains_word "$verb" ${VERBS[ENVS]}; then
294 comps=$( __systemctl $mode show-environment \
295 | while read -r line; do echo " ${line%%=*}="; done )
298 elif __contains_word "$verb" ${VERBS[FILE]}; then
299 comps=$( compgen -A file -- "$cur" )
302 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
303 comps=$( __systemctl $mode list-unit-files --type target --full --all "$cur*" \
304 | { while read -r a b; do echo " $a"; done; } )
307 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
311 complete -F _systemctl systemctl