Created
February 29, 2012 06:10
-
-
Save zhasm/1938402 to your computer and use it in GitHub Desktop.
bash_functions
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
#!/bin/bash | |
function psg() | |
{ | |
/bin/ps aux | grep -v 'grep' |grep -i $1 | |
} | |
function len() | |
{ | |
string=$1 | |
echo ${#string} | |
} | |
function pgrep() { | |
ps aux | grep -v grep | grep -i $1 | |
} | |
function pkill() { | |
local pid | |
pid=$(ps ax | grep $1 | grep -v grep | awk '{ print $1 }') | |
kill -9 $pid | |
echo -e "Killed $1 (process $pid)\n" | |
} | |
#safe remove, mv the files to .Trash with unique name | |
#and log the acction | |
function rm() | |
{ | |
if [ -d $HOME/.local/share/Trash/files ];then | |
trash="$HOME/.local/share/Trash/files" | |
else | |
trash="$HOME/.Trash" | |
fi | |
log="" #/var/log/trash.log" | |
stamp=`date "+%Y-%m-%d %H:%M:%S"` #current time | |
while [ -f "$1" ] || [ -d "$1" ]; do | |
#remove the possible ending / | |
file=`echo $1 |sed 's#\/$##' ` | |
pure_filename=`echo $file |awk -F / '{print $NF}' |sed -e "s#^\.##" ` | |
if [ `echo $pure_filename | grep "\." ` ]; then | |
new_file=` echo $pure_filename |sed -e "s/\([^.]*$\)/$RANDOM.\1/" ` | |
else | |
new_file="$pure_filename.$RANDOM" | |
fi | |
trash_file="$trash/$new_file" | |
mv "$file" "$trash_file" | |
if [ -w $log ]; then | |
echo -e "[$stamp]\t$file\t=>\t[$trash_file]" |tee -a $log | |
else | |
echo -e "[$stamp]\t$file\t=>\t[$trash_file]" | |
echo 'not writeble log file' | |
fi | |
shift #increment the loop | |
done | |
} | |
function mountiso() | |
{ | |
sudo umount -f /media/cdrom | |
sudo mount -o loop -t iso9660 "$1" /media/cdrom | |
} | |
function pt() { | |
# to check if a port has been used | |
if [ $# -lt 1 ]; then | |
echo "Usage: pt <PortNumber> #to check if a port has been used" | |
return | |
fi | |
sudo lsof -i :$* | |
} | |
function md(){ | |
#make dir and enter | |
if [ -f "$1" ] ; then | |
echo "File $1 exists~" | |
return | |
fi | |
if [ ! -d "$1" ] ;then | |
mkdir -p "$1" | |
cd "$1" | |
else | |
echo "$1 exists~" | |
fi | |
} | |
function tt() { | |
#compress on file/folder to *.tgz | |
filename=`echo "$1" | sed -e 's@/$@@' `".tgz" | |
tar czvf $filename "$1" | |
} | |
function xx() { | |
if [ -f $1 ] ; then | |
case $1 in | |
*.tar.bz2) tar xvjf $1 ;; | |
*.tar.gz) tar xvzf $1 ;; | |
*.tar.xz) tar Jxvf $1 ;; | |
*.bz2) bunzip2 $1 ;; | |
*.rar) unrar x $1 ;; | |
*.gz) gunzip $1 ;; | |
*.tar) tar xvf $1 ;; | |
*.tbz2) tar xvjf $1 ;; | |
*.tgz) tar xvzf $1 ;; | |
*.zip) unzip $1 ;; | |
*.Z) uncompress $1 ;; | |
*.7z) 7z x $1 ;; | |
*) echo "don't know how to extract '$1'..." ;; | |
esac | |
else | |
echo "'$1' is not a valid file!" | |
fi | |
} | |
function upper() { echo ${@^^}; } | |
function lower() { echo ${@,,}; } | |
#sudo kill pkill | |
function k() { | |
cfm=Y | |
if [ ! -z "$1" ]; then | |
if [[ ! -z "$1" ]]; then | |
/bin/ps aux | grep -v grep | grep -i "$1" | |
read -p "killall? [Yn]" cfm | |
case $cfm in | |
'') | |
/bin/ps aux |grep -v grep | grep -i "$1" |awk '{print $2}' |xargs sudo kill -9 | |
;; | |
'Y') | |
/bin/ps aux |grep -v grep | grep -i "$1" |awk '{print $2}' |xargs sudo kill -9 | |
;; | |
'y') | |
/bin/ps aux |grep -v grep | grep -i "$1" |awk '{print $2}' |xargs sudo kill -9 | |
;; | |
*) | |
echo "aborted..." | |
;; | |
esac | |
else | |
echo "no such process." | |
fi | |
fi | |
} | |
function rand() | |
{ | |
#provide a 16-digit random string | |
options='abc' | |
option_count=${#options} | |
length=16 | |
choice=$((RANDOM % option_count)) | |
chosen=${options:choice:1} | |
function a() | |
{ | |
#the length must be divided by 2 | |
openssl rand -hex $((length / 2 )) | |
} | |
function b(){ | |
#a trailing '; echo' is required for line break | |
#by @Gokuu (http://fanfou.com/Gokuu) | |
head /dev/urandom | xxd -p | head -c $length; echo | |
} | |
function c() | |
{ | |
#by @bitstream (http://fanfou.com/wangcong) | |
printf "%04x%04x%04x%04x" $RANDOM $RANDOM $RANDOM $RANDOM | |
} | |
#the core of this function | |
$chosen | |
} | |
c() | |
{ | |
#an one-liner caculator | |
bc <<EOF | |
scale=3 | |
$* | |
EOF | |
} | |
#caculate the md5sum of a folder | |
function md5folder() | |
{ | |
limitSize='104857600' #100M | |
path=$1 | |
tmpmd5=$(mktemp md5sum.XXXXXX) | |
for file in `find $path| grep -v "\.git"` | |
do | |
if [ -f $file ] | |
then | |
cat $file >>$tmpmd5 | |
fi | |
done | |
md5sum $tmpmd5 | grep -oP "\b[a-f\d]{8}(?=[a-z\d]{24}\b)" | |
rm -f $tmpmd5 | |
} | |
function hc(){ | |
# one-liner caculator, with result in hexadecimal. | |
if [ $# -gt 0 ]; then | |
python -c "print '''0x%x''' % ($*)" | |
else | |
echo "Usage: $FUNCNAME <math expression> #caculator in hex" | |
fi | |
} | |
function isgd() | |
{ | |
url="$1" | |
if [ $# -gt 0 ]; then | |
shorten=`curl -s "http://is.gd/create.php?format=simple&logstats=1&url=$url"` | |
echo $shorten | |
echo $shorten | pbcopy #save to clipboard | |
else | |
echo "Usage: $FUNCNAME <URL_TO_SHORTEN>" | |
fi | |
} | |
#show full path of an file/folder | |
full() { | |
local i;for i in $*;do readlink -f $i;done | |
} | |
#convert epoch seconds to Date | |
sec2date(){ | |
sec="$1" | |
if [ $# -gt 0 ]; then | |
date=`date -d "@$sec" +"%Y-%m-%d %H:%M:%S +0800"` | |
echo $date | |
echo $date |tr -d "\n" |pbcopy | |
else | |
echo "Usage: $FUNCNAME <SECONDS>" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tar 可以自动识别压缩方式的