Created
September 27, 2010 03:03
-
-
Save mattmccray/598538 to your computer and use it in GitHub Desktop.
Some (somewhat) useful shell scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Wrap sudo to handle aliases and functions | |
| # Wout.Mertens@gmail.com | |
| # | |
| # Comments and improvements welcome | |
| sudo () | |
| { | |
| local c o t parse | |
| # Parse sudo args | |
| OPTIND=1 | |
| while getopts xVhlLvkKsHPSb:p:c:a:u: t; do | |
| if [ "$t" = x ]; then | |
| parse=true | |
| else | |
| o="$o -$t" | |
| [ "$OPTARG" ] && o="$o $OPTARG" | |
| fi | |
| done | |
| shift $(( $OPTIND - 1 )) | |
| # If no arguments are left, it's a simple call to sudo | |
| if [ $# -ge 1 ]; then | |
| c="$1"; | |
| shift; | |
| case $(type -t "$c") in | |
| "") | |
| echo No such command "$c" | |
| return 127 | |
| ;; | |
| alias) | |
| c="$(type "$c"|sed "s/^.* to \`//;s/.$//")" | |
| ;; | |
| function) | |
| c=$(type "$c"|sed 1d)";\"$c\"" | |
| ;; | |
| *) | |
| c="\"$c\"" | |
| ;; | |
| esac | |
| if [ -n "$parse" ]; then | |
| # Quote the rest once, so it gets processed by bash. | |
| # Done this way so variables can get expanded. | |
| while [ -n "$1" ]; do | |
| c="$c \"$1\"" | |
| shift | |
| done | |
| else | |
| # Otherwise, quote the arguments. The echo gets an extra | |
| # space to prevent echo from parsing arguments like -n | |
| # Note the lovely interactions between " and ' ;-) | |
| while [ -n "$1" ]; do | |
| c="$c '$(echo " $1"|sed -e "s/^ //" -e "s/'/'\"'\"'/")'" | |
| shift | |
| done | |
| fi | |
| # Run the command with verbose options | |
| echo Executing sudo $o -- bash -x -v -c "$c" >&2 | |
| command sudo $o bash -xvc "$c" | |
| else | |
| echo sudo $o >&2 | |
| command sudo $o | |
| fi | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function vhost { | |
| if [ $# -lt 1 ]; then | |
| vhost help | |
| return | |
| fi | |
| case $1 in | |
| "help" ) | |
| echo | |
| echo "VHost Creation Util" | |
| echo | |
| echo "Usage:" | |
| echo " vhost help (Show this message)" | |
| echo " vhost list (Show the /etc/hosts file)" | |
| echo " vhost new URI PATH (Creates a hosts entry and vhost in Apache)" | |
| echo | |
| echo "Example:" | |
| echo " [sudo] vhost new my-app.local /Path/To/MyApp/WwwRoot" | |
| echo | |
| ;; | |
| "list" ) | |
| echo "" | |
| echo "Current Hosts File:" | |
| echo "" | |
| cat /etc/hosts | |
| echo "" | |
| ;; | |
| "new" ) | |
| if [ $# -lt 3 ]; then | |
| echo | |
| echo "You must specify the SITE_ROOT as the 3rd parameter." | |
| echo | |
| vhost help | |
| return | |
| fi | |
| if [[ $2 && $2 != *.local ]]; then | |
| echo | |
| echo "Invalid URI: $2" | |
| echo | |
| echo "(URI names must end in '.local')" | |
| echo | |
| vhost help | |
| return | |
| fi | |
| echo "Creating $2 at $3" | |
| echo | |
| echo " Updating hosts file..." | |
| echo "127.0.0.1 $2" >> /etc/hosts | |
| echo " Updating apache config..."a | |
| echo "" >> /etc/apache2/extra/httpd-vhosts.conf | |
| echo "<VirtualHost *:80>" >> /etc/apache2/extra/httpd-vhosts.conf | |
| echo " ServerAdmin darthapo@gmail.com" >> /etc/apache2/extra/httpd-vhosts.conf | |
| echo " DocumentRoot $3" >> /etc/apache2/extra/httpd-vhosts.conf | |
| echo " ServerName $2" >> /etc/apache2/extra/httpd-vhosts.conf | |
| echo " ErrorLog /private/var/log/apache2/$2-error_log" >> /etc/apache2/extra/httpd-vhosts.conf | |
| echo " CustomLog /private/var/log/apache2/$2-access_log common" >> /etc/apache2/extra/httpd-vhosts.conf | |
| echo "</VirtualHost>" >> /etc/apache2/extra/httpd-vhosts.conf | |
| echo "" >> /etc/apache2/extra/httpd-vhosts.conf | |
| echo " Restarting apache..." | |
| apachectl restart | |
| echo "Done." | |
| ;; | |
| * ) | |
| vhost help | |
| ;; | |
| esac | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment