Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function kdesudo () (
- ## kdesudo replacement
- ## Copyleft 08/28/2018 - Joseph Pollock - JPmicrosystems
- ## Last modified 05/25/2019
- ## Usage: kdesudo [-t "Dialog Title"] [-p "Dialog Prompt"] command and arguments
- ## Just like sudo, it only prompts if sudo permission has timed out
- ## Won't work if your command is -t or -p
- ## and the corresponding flag isn't specified - BUT SERIOUSLY?
- ## NOTE: use of () instead of {} puts function body in a subshell
- ## so all variables are local
- ## NOTE: Can fail on race condition between sudo test and sudo run
- ## NOTE: Does *not* handle embedded blanks in the command
- ## It does handle them in the arguments to the command
- ##source ~/bin/bash_trace ## debug TRACE
- prompt=" "
- while (( $# )) ## Process optional function arguments
- do
- case "$1" in
- "-t")
- shift
- if [[ -n "$1" ]] ## Ignore empty string
- then
- kcmd=("${kcmd[@]}" "--title" "$1")
- ##echo "kcmd [${kcmd[@]}]" ## debug
- fi
- shift
- ;;
- "-p")
- shift
- [[ -n "$1" ]] && prompt="$1" ## Ignore empty string
- shift
- ;;
- *) ## Stop looking for kdesudo args as soon as any other argument is present
- break
- ;;
- esac
- done
- if (( ! $# )) ## There has to be some command to run
- then
- return 2
- fi
- args=("$@")
- ##echo "args [${args[@]}]" ## debug
- sudo -n true &> /dev/null ## Returns 0 if sudo runs without password
- ##false ## debug Returns 1 for testing when sudo hasn't timed out
- if (( ! $? )) ## if sudo is already activated - not timed out
- then
- sudo -- "${args[@]}" ## then just run the command
- else ## Otherwise display password dialog
- kdialog --password "$prompt" "${kcmd[@]}" | sudo -S -- "${args[@]}"
- fi
- rc=$?
- [[ $rc == 141 ]] && rc=0 ## ignore a broken pipe
- return $rc
- )
Add Comment
Please, Sign In to add comment