diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2015-10-01 16:39:30 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2015-10-01 16:39:30 +1300 |
commit | 51b5b2e15188ef54120d0ae9032d2e28f7246617 (patch) | |
tree | 175392cadb17de71e4af5249ffe56703d06b01ad /bin | |
parent | Move man pages into man1 subdir (diff) | |
download | dotfiles-51b5b2e15188ef54120d0ae9032d2e28f7246617.tar.gz dotfiles-51b5b2e15188ef54120d0ae9032d2e28f7246617.zip |
Add sue(8)
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/sue | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/bin/sue b/bin/sue new file mode 100755 index 00000000..cd20fa5c --- /dev/null +++ b/bin/sue @@ -0,0 +1,67 @@ +#!/usr/bin/env bash + +# +# sue(1) -- Run sudoedit(8) with an appropriate user on a set of files +# +# Author: Tom Ryder +# Copyright: 2015 +# License: Public domain +# + +# Name self +self=sue + +# Define a function to show usage +usage() { + printf '%s: usage: %s FILE1 [FILE2 ...]\n' \ + "$self" "$self" +} + +# Test the first argument +case $1 in + + # Give help on stdout if requested + -h|--help|-\?) + usage + exit + ;; + + # If no file was given, give help on stderr and bail + '') + usage >&2 + exit 1 + ;; +esac + +# Pull arguments into an array +declare -a files +files=("$@") + +# Iterate through the files and check they all have the same owner +user= +for file in "${files[@]}" ; do + + # Use stat(1) to get the file owner + if ! file_owner=$(stat -c %U "$file") ; then + printf '%s: Failed to run stat(1) on file %s\n' \ + "$self" "$file" >&2 + exit 1 + fi + + # If this is the first file, we'll use its owner as our user + if [[ -z $user ]] ; then + user=$file_owner + + # If not, and the user we're going to use and this file's owner don't + # match, bail with an error + elif [[ $user != $file_owner ]] ; then + printf '%s: Files do not share a common owner\n' \ + "$self" >&2 + exit 1 + fi +done + +# If we got this far, there's at least one file and all the files are owned by +# the same user; we can safely edit them +exec sudoedit -u "$user" "${files[@]}" + |