2012-10-12 11:15:29 +00:00
# Create a new directory and enter it
2012-11-01 11:27:12 +00:00
function md( ) {
2012-10-12 11:15:29 +00:00
mkdir -p " $@ " && cd " $@ "
}
2012-11-15 12:31:00 +00:00
# Find shorthand
function f( ) {
find . -name " $1 "
}
2012-10-12 11:15:29 +00:00
# Get gzipped file size
function gz( ) {
echo "Original size (bytes): "
cat " $1 " | wc -c
echo "Gzipped size (bytes): "
gzip -c " $1 " | wc -c
}
# Test if HTTP compression (RFC 2616 + SDCH) is enabled for a given URL.
# Send a fake UA string for sites that sniff it instead of using the Accept-Encoding header. (Looking at you, ajax.googleapis.com!)
function httpcompression( ) {
encoding = " $( curl -LIs -H 'User-Agent: Mozilla/5 Gecko' -H 'Accept-Encoding: gzip,deflate,compress,sdch' " $1 " | grep '^Content-Encoding:' ) " && echo " $1 is encoded using ${ encoding #* } " || echo " $1 is not using any encoding "
}
# Escape UTF-8 characters into their 3-byte format
function escape( ) {
printf "\\\x%s" $( printf " $@ " | xxd -p -c1 -u)
echo # newline
}
# Decode \x{ABCD}-style Unicode escape sequences
function unidecode( ) {
perl -e " binmode(STDOUT, ':utf8'); print \" $@ \" "
echo # newline
}
# Get a character’ s Unicode code point
function codepoint( ) {
perl -e " use utf8; print sprintf('U+%04X', ord(\" $@ \")) "
echo # newline
2012-10-19 11:16:29 +00:00
}
# Extract archives of various types
function extract( ) {
if [ -f $1 ] ; then
local dir_name = ${ 1 %.* } # Filename without extension
case $1 in
*.tar.bz2) tar xjf $1 ; ;
*.tar.gz) tar xzf $1 ; ;
*.tar.xz) tar Jxvf $1 ; ;
*.tar) tar xf $1 ; ;
*.tbz2) tar xjf $1 ; ;
*.tgz) tar xzf $1 ; ;
*.bz2) bunzip2 $1 ; ;
2012-11-09 08:31:06 +00:00
*.rar) unrar x $1 $2 ; ;
2012-10-19 11:16:29 +00:00
*.gz) gunzip $1 ; ;
*.zip) unzip -d$dir_name $1 ; ;
*.Z) uncompress $1 ; ;
*) echo " ' $1 ' cannot be extracted via extract() " ; ;
esac
else
echo " ' $1 ' is not a valid file "
fi
}
2012-10-31 12:38:56 +00:00
# Print nyan cat
# https://github.com/steckel/Git-Nyan-Graph/blob/master/nyan.sh
# If you want big animated version: `telnet miku.acm.uiuc.edu`
function nyan( ) {
e = '\033'
RESET = " $e [0m "
BOLD = " $e [1m "
CYAN = " $e [0;96m "
RED = " $e [0;91m "
YELLOW = " $e [0;93m "
GREEN = " $e [0;92m "
echo
echo -en $RED '-_-_-_-_-_-_-_'
echo -e $RESET $BOLD ',------,' $RESET
echo -en $YELLOW '_-_-_-_-_-_-_-'
echo -e $RESET $BOLD '| /\_/\\' $RESET
echo -en $GREEN '-_-_-_-_-_-_-'
echo -e $RESET $BOLD '~|__( ^ .^)' $RESET
echo -en $CYAN '-_-_-_-_-_-_-'
echo -e $RESET $BOLD '"" ""' $RESET
echo
}
2012-11-09 10:05:30 +00:00
# Creates an SSH key and uploads it to the given host
2012-11-12 12:10:05 +00:00
# Based on https://gist.github.com/1761938
add-ssh-host( ) {
2012-11-09 10:05:30 +00:00
username = $1
hostname = $2
identifier = $3
if [ [ " $identifier " = = "" ] ] || [ [ " $username " = = "" ] ] || [ [ " $hostname " = = "" ] ]
then
echo "Usage: configure_ssh_host <username> <hostname> <identifier>"
else
if [ ! -f " $HOME /.ssh/ $identifier .id_rsa " ] ; then
ssh-keygen -f ~/.ssh/$identifier .id_rsa -C " $USER $( date +'%Y/%m%/%d %H:%M:%S' ) "
fi
if ! grep -Fxiq " host $identifier " " $HOME /.ssh/config " ; then
echo -e " Host $identifier \n\tHostName $hostname \n\tUser $username \n\tIdentityFile ~/.ssh/ $identifier .id_rsa " >> ~/.ssh/config
fi
ssh $identifier 'mkdir -p .ssh && cat >> ~/.ssh/authorized_keys' < ~/.ssh/$identifier .id_rsa.pub
tput bold; ssh -o PasswordAuthentication = no $identifier true && { tput setaf 2; echo "SSH key added." ; } || { tput setaf 1; echo "Failure" ; } ; tput sgr0
2012-11-28 08:41:42 +00:00
_ssh_reload_autocomplete
2012-11-09 10:05:30 +00:00
fi
}
2012-11-15 06:30:03 +00:00
# Upload current directory to special directory on my hosting
function yay( ) {
server = "locum"
dir = ` basename " $( pwd ) " `
remote = " ~/projects/yay/ $dir "
url = " http://yay.sapegin.me/ $dir / "
2012-11-19 18:51:45 +00:00
tar cp --exclude '.git' --exclude 'node_modules' . | gzip | ssh $server "mkdir -p " $remote "; gzip -cd | tar x -C " $remote ""
2012-11-15 06:30:03 +00:00
echo " Current directory uploaded to $url . "
if command -v pbcopy >/dev/null 2>& 1; then
echo -n " $url " | pbcopy
echo "URL copied to clipboard."
fi
2012-11-19 20:38:53 +00:00
}
# Setup syncronization of current Git repo with GitHub repo of the same name
2012-11-27 14:04:43 +00:00
# USAGE: git-github [repo]
2012-11-19 20:38:53 +00:00
function git-github( ) {
user = "sapegin"
2012-11-27 14:04:43 +00:00
repo = ${ 1 - ` basename " $( pwd ) " ` }
2012-11-19 20:38:53 +00:00
git remote add origin " git@github.com: $user / $repo .git "
git push -u origin master
}
# Setup syncronization of current Git repo with Bitbucket repo of the same name
2012-11-27 14:04:43 +00:00
# USAGE: git-bitbucket [repo]
2012-11-19 20:38:53 +00:00
function git-bitbucket( ) {
user = "sapegin"
2012-11-27 14:04:43 +00:00
repo = ${ 1 - ` basename " $( pwd ) " ` }
2012-11-27 21:04:14 +00:00
git remote add origin " git@bitbucket.org: $user / $repo .git "
2012-11-19 20:38:53 +00:00
git push -u origin master
}
2012-11-24 03:42:54 +00:00
# Install/update all NPM tasks used in grunt.js in current folder
function npm-grunt( ) {
if [ ! -f "grunt.js" ] ; then
echo "grunt.js not found."
return
fi
npm update grunt -g
tasks = ( ` grep -oP "(?<=loadNpmTasks\(['\"])[^'\"]+" grunt.js` )
for task in " ${ tasks [@] } "
do
2012-11-27 21:04:14 +00:00
npm install $task -g
2012-11-24 03:42:54 +00:00
npm link $task
done
}
2012-11-27 14:24:59 +00:00
# Find files with Windows line endings (and convert then to Unix in force mode)
# USAGE: crlf [--force]
function crlf( ) {
[ " $1 " = = "--force" ] && force = 1 || force = 0
for file in $( find . -type f -not -path "*/.git/*" -not -path "*/node_modules/*" | xargs file | grep ASCII | cut -d: -f1) ; do
grep -q $'\x0D' " $file " && echo " $file " && [ $force ] && dos2unix " $file "
done
}