Skip to content
No description or website provided.
Shell
Branch: master
Clone or download
Latest commit 2e09200 Oct 29, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE First public release Oct 21, 2019
README.md Better docs. Oct 22, 2019
SHOUT.sh Fix comment Oct 29, 2019

README.md

SHOUT.sh

SYNOPSYS

converts ALL_CAPS commands to sudo all_caps

USAGE

Source SHOUT.sh (or stick it into your bashrc). Then when you type:

$ YUM -y update

It will be converted to:

$ sudo yum -y update

If you use this in your .bashrc you should probably reserve it for interactive shells, doing something along the lines of:

if [[ x$PS1 != x ]]; then
 . SHOUT.sh
fi

IMPLEMENTATION NOTES

  • The present implementation is full of bash-isms and will likely not run on any other shell.

  • The code runs instead of any not-found command invoked as an all caps verb. (So /usr/bin/CAT is not affected, but CAT and /USR/BIN/CAT are.)

  • It runs as a command_not_found_handle by pre-chaininig itself to any preexisting handle, which gets renamed to command_not_found_handle_orig. So things like automatic search of installable commands, which on Fedora is implementaed as a not-found handler (and which I find terminally irritating, but still) keep working. Also, in the rare case where an all-caps command actually exists, real commands will not be obscured.

  • OTOH, this cute prechaining trick may fail spectacularly and in unforeseen ways if other parts of your .bashrc & co. are also playing tricks with said handle.

  • General Caveat emptor cautions apply.

HISTORY

From an original implementation seen on twitter (and HN?): https://github.com/jthistle/SUDO.

Alas, the original implementation is not particularly fast:

# Interesting, BUT...
# SUDO - shout at bash to su commands
# Distributed under GNU GPLv2, @jthistle on github

shopt -s expand_aliases

IFS_=${IFS}
IFS=":" read -ra PATHS <<< "$PATH"

# OUCH! ... this is bound to take forever.
for i in "${PATHS[@]}"; do
	for j in $( ls "$i" ); do
		if [ ${j^^} != $j ] && [ $j != "sudo" ]; then
			alias ${j^^}="sudo $j"
		fi		
	done
done

alias SUDO='sudo $(history -p !!)'

IFS=${IFS_}

# end SUDO

This prompted me to tweet along the lines of:

# this might be an alternative

function command_not_found_handle () {
    local allcaps=$(echo $1 | grep -E '^[A-Z0-9_-]+$')
    if [[ x$allcaps != x ]]; then
	   local cmd=$(echo $1 | tr A-Z a-z)
	   shift
	   sudo $cmd "$@"
    else
	   return 127
    fi
}

Which provoked a fruitful tweet exchange (which I did not know was at all possible) viz. https://twitter.com/saruspete/status/1149281131612770304.

It's been sitting in my .bashrc for a few months, with no issues. I do not use it heavily though, because I do SU - a lot.

I omitted the alias SUDO='sudo $(history -p !!)' thing because I introduced a shorter, more useful shortcut that maps ^V to "recall last command go to BOL, upcase first word". This is actually better than an alis because

  • it's shorter
  • It allows you to back off before hitting CR. With 'sudo !!' it's easy to shoot your own foot especially if you (like me) are merging different histories from different terminal windows. (This can be achieved with: PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND". It's far from flawless, though)

LICENSE

GNU GENERAL PUBLIC LICENSE Version 2

You can’t perform that action at this time.