Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save organicaudio/7d105f4cea0aaafcad15eedd352fa422 to your computer and use it in GitHub Desktop.
Save organicaudio/7d105f4cea0aaafcad15eedd352fa422 to your computer and use it in GitHub Desktop.
Edit et al. - system-wide use of Editor (invoking sudo when necessary)
###################################################################
# EDIT FILE WITH EDITOR AND SUDO (IF REQUIRED)
# (for use in ~/.bash_alias)
#
# INSTALL:
# - paste in your ~/.bash_aliases
# - name file 'edit' to maximize usage
# - chmod u+x edit
#
# USAGE: edit <file to edit>
#
####################################################################
edit () {
# Check for EDITOR before continuing
if [[ ! -v EDITOR ]]; then
echo "ERROR Unable to execute command!"
echo "\$EDITOR has not been been declared in environment variables or with select-editor."
echo ""
return 1
fi
# Path Variables
_filePATH=$(basename "$1")
_dirPATH=$(cd "$(dirname "$1")"; pwd -P)
_fullPATH=$(realpath "$1")
# Print summary before action
echo "EDITING FILE"
echo "------------------------"
echo "File - " $_filePATH
echo "Dir Path - " $_dirPATH
echo "File Path - " $_fullPATH
echo "------------------------"
sleep 1
# test file exists & read/writable by user
if [[ -f "$_fullPATH" ]] && [[ -r "$_fullPATH" && -w "$_fullPATH" ]]; then
echo "File exists and is Read/Writable"
sleep 1
$EDITOR "${_fullPATH}"
# test file does not exist
# if true = calls prompt
elif [[ ! -f "${_fullPATH}" ]]; then
# call with a prompt string or use a default
read -p "File does not exist - Create new [y/n]? " -n 1 -r
echo ""
# response NO = ends function cleanly
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
# handle exits from shell or function but don't exit interactive shell
# otherwise > creates new file as original non-root user
# response YES = creates file and opens editor
else
echo $_fullPATH
if [ ! -w "${_dirPATH}" ]; then
echo "Creating file $filePATH as sudo - $USER has insufficient write permissions for $_dirPATH"
sleep 1
sudo touch ${_fullPATH}
sudo $EDITOR ${_fullPATH}
# return 1
else
echo "Creating file $filePATH as $USER - in $_dirPATH"
sleep 1
touch ${_fullPATH}
$EDITOR ${_fullPATH}
fi
fi
# file exists but not read/uritable bu user
# invokes sudo to continue
else
echo "Opening file $_filePATH as sudo - $USER has insufficient write permissions for $_fullPATH"
sleep 2
sudo $EDITOR "${_fullPATH}"
fi
}
#!/usr/bin/env bash
#################################################################
# EDIT FILE WITH EDITOR AND SUDO (IF REQUIRED)
# (bash funciton)
#
# INSTALL:
# - save file in system $PATH (eg. /usr/local/bin)
# - name file 'edit' to maximize usage
# - chmod u+x edit
#
# USAGE: edit <file to edit>
#
#################################################################
function func_editfile () {
# Check for EDITOR before continuing
if [[ ! -v EDITOR ]]; then
echo "ERROR Unable to execute command!"
echo "\$EDITOR has not been been declared in environment variables or with select-editor."
echo ""
return 1
fi
# Path Variables
filePATH=$(basename "$1")
dirPATH=$(cd "$(dirname "$1")"; pwd -P)
fullPATH=$(realpath "$1")
# Print summary before action
echo "EDITING FILE"
echo "------------------------"
echo "File - " $filePATH
echo "Dir Path - " $dirPATH
echo "File Path - " $fullPATH
echo "------------------------"
sleep 1
# test file exists & read/writable by user
if [[ -f "$fullPATH" ]] && [[ -r "$fullPATH" && -w "$fullPATH" ]]; then
echo "File exists and is Read/Writable"
sleep 1
$EDITOR "${fullPATH}"
# test file does not exist
# if true = calls prompt
elif [[ ! -f "${fullPATH}" ]]; then
# call with a prompt string or use a default
read -p "File does not exist - Create new [y/n]? " -n 1 -r
echo ""
# response NO = ends function cleanly
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
# handle exits from shell or function but don't exit interactive shell
# otherwise > creates new file as original non-root user
# response YES = creates file and opens editor
else
echo $fullPATH
if [ ! -w "${dirPATH}" ]; then
echo "Creating file $filePATH as sudo - $USER has insufficient write permissions for $dirPATH"
sleep 1
sudo touch ${fullPATH}
sudo $EDITOR ${fullPATH}
# return 1
else
echo "Creating file $filePATH as $USER - in $dirPATH"
sleep 1
touch ${fullPATH}
$EDITOR ${fullPATH}
fi
fi
# file exists but not read/uritable bu user
# invokes sudo to continue
else
echo "Opening file $filePATH as sudo - $USER has insufficient write permissions for $fullPATH"
sleep 2
sudo $EDITOR "${fullPATH}"
fi
}
exec func_editfile
@organicaudio
Copy link
Author

TL;DR a must have shell script for any avid shell & ssh users!

What does it do?

The script minimizes use of sudo to only where absolutely needed order to maintain original user file permissions

Script logic:

  1. File exists and is read/writable by user > edit as user

  2. File exists but is not read/writable by user > edit as sudo

  3. File does not exist and destination is read/writable by user > confirm prompt, touch path, and edit as user (no to prompt exit's gracefully)

  4. File does not exist and destination is not read/writable by user > confirm prompt, touch path, and edit as sudo (no to prompt exit's gracefully)

  5. $EDITOR has no been set in ENV or by select-editor > exit 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment