From b79fc0e5e82d18bb3d2c7ed95ad4b01057ee2880 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 1 Aug 2016 10:06:07 +1200 Subject: Change sue(8) to POSIX sh --- bin/sue | 70 +++++++++++------------------------------------------------------ 1 file changed, 12 insertions(+), 58 deletions(-) diff --git a/bin/sue b/bin/sue index 260a19d9..8c4b8910 100755 --- a/bin/sue +++ b/bin/sue @@ -1,62 +1,16 @@ -#!/usr/bin/env bash - -# -# sue(8) -- 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 'USAGE: %s FILE1 [FILE2 ...]\n' \ - "$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 - -# Iterate through the files and check they all have the same owner +#!/bin/sh +# Run sudoedit(8) with an appropriate user on a set of files user= for file ; 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 + file_owner=$(stat -c %U -- "$file") || exit + case $user in + "$file_owner"|'') + user=$file_owner + ;; + *) + printf >&2 'sue: Files do not share a common owner\n' + exit 1 + ;; + esac 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" -- "$@" -- cgit v1.2.3