shell snippet
06 June 2018

shell color

Black='\033[0;30m'
Red='\033[0;31m'
Green='\033[0;32m'
BrownOrange='\033[0;33m'
Blue='\033[0;34m'
Purple='\033[0;35m'
Cyan='\033[0;36m'
LightGray='\033[0;37m'
DarkGray='\033[1;30m'
LightRed='\033[1;31m'
LightGreen='\033[1;32m'
Yellow='\033[1;33m'
LightBlue='\033[1;34m'
LightPurple='\033[1;35m'
LightCyan='\033[1;36m'
White='\033[1;37m'
NC='\033[0m'

echo -e ${Red}text${NC}

inotify

while true; do
    inotifywait --exclude XXX -rqe modify /path/to/dir_or_file;
    do_something
    sleep 3   # 决定 sleep 多久重新监听 inotify(这里不 sleep 会有问题问题,比如一个目录同时更新了 3 个文件,那个 `do_something` 就会被执行 3 次)
done

lsof

lsof -i tcp:9999

redis del by pattern

# 密码有 "!" 要先转义 "\!"
redis-cli -h 127.0.0.1 -a "xxxx#\!xxx#%*" keys * | xargs redis-cli -h 127.0.0.1 -a "xxxx#\!xxx#%*" del

ps kill

ps -aux | grep NAME | awk '{print $2}' | xargs kill

ps kill remote

# "$2" 要先转义成 "\$2"
ansible all -i /path/to/ip_file -m shell -a "ps -aux | grep inotify | awk '{print \$2}' | xargs kill"