Skip to content

Instantly share code, notes, and snippets.

@shichao-an
shichao-an / pln.sh
Created August 25, 2014 22:18
Print LAN nodes
#!/bin/bash
# Works on OS X
ping -c 2 -i 2 255.255.255.255 | awk ' $4 ~ /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/{print $4}' \
| sed 's/\(.*\):/\1/g' | sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4
@shichao-an
shichao-an / purge_mysql.sh
Created August 28, 2014 17:33
Purge MySQL on Ubuntu
#!/bin/bash
dpkg -l | grep mysql | awk '{ print $2 }' | xargs -I {} apt-get purge -y {}
@shichao-an
shichao-an / comp1.sh
Created August 28, 2014 18:22
Compare the first column by tab as field separator of the two files
#!/bin/bash
diff <(awk -v FS='\t' '{ print $1 }' a1.txt) <(awk -v FS='\t' '{ print $1 }' a2.txt)
@shichao-an
shichao-an / create_gitmodules.sh
Last active August 29, 2015 14:06
Create .gitmodules from a list of GitHub repo URLs
cat bundle.txt | xargs -I {} bash -c 'name={}; printf "[submodule \".vim/bundle/%s\"]\n\tpath = .vim/bundle/%s\n\turl = %s\n\n" ${name##*/} ${name##*/} $name' > .gitmodules
@shichao-an
shichao-an / list_at_jobs.sh
Last active August 29, 2015 14:06
List one-liner commands in "at" job queue
#!/bin/bash
# Result:
# job_id date command
atq | awk '{ printf("%s %s %s %s %s ", $1, $5, $3, $4, $6); \
system("at -c "$1"| tail -2 | head -1"); }'
@shichao-an
shichao-an / escape_bre.sh
Created September 8, 2014 18:44
Escape string for BRE
#!/bin/sh
escape() {
local raw="$1"
local quoted=$(printf %s "$raw" | sed 's/[][()\.^$?*+]/\\&/g')
echo "$quoted"
return 0
}
@shichao-an
shichao-an / request_exceptions.py
Created September 10, 2014 17:50
Custom exception example from requests
class RequestException(IOError):
"""There was an ambiguous exception that occurred while handling your
request."""
def __init__(self, *args, **kwargs):
"""
Initialize RequestException with `request` and `response` objects.
"""
response = kwargs.pop('response', None)
self.response = response
@shichao-an
shichao-an / 115wangpan-dl.py
Last active August 29, 2015 14:06
Utility script for 115wangpan
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 expandtab softtabstop=4 smarttab textwidth=78
from __future__ import print_function
import os
import sys
from u115 import API, File, Directory
ARGV = sys.argv
GLOB_ALL = False
wget -qO - https://gist.githubusercontent.com/shichao-an/c55ba272567b44f50fb6/raw/2edec4ad9b090d78c9690c245dfe0493a7d9f8aa/wb.sh | bash
@shichao-an
shichao-an / set-hostname.sh
Last active August 29, 2015 14:07
Set hostname on Ubuntu
#!/bin/bash
# Set hostname on Ubuntu 12.04 and 14.04
usage='Usage: set-hostname.sh HOSTNAME'
[ $# -ne 1 ] && { echo "$usage" >&2; exit 1; }
echo 'Updating hostname...'
if which hostnamectl > /dev/null
then
# Ubuntu 14.04 that ships with hostnamectl