说明

以下记录是平时工作中bash 相关使用的点滴记录,会持续更新

bash 中使用数组

1
sites=("www.a.com www.b.com www.c.com www.d.com" "www.e.cn www.f.cn")   
n_sites=${#sites[*]} #获取一维sites的数组长度   
for ((i=0;i<$n_sites;i++))
do   
    inner_sites=(${sites[$i]}) #将一维sites字符串赋值到数组   
    n_inner_sites=${#inner_sites[*]} #获取二维sites的数组长度   
    for ((j=0;j<$n_inner_sites;j++));   
    do       
        echo ${inner_sites[$j]} #回显site   
    done   
done

find命令使用小结

1
find . -name "name" -exec cp {} \;  
find 结合 -exec, grep使用  
find .(path) -name "name" -exec grep "part" {} \;  
find 结合 xargs tar 结合使用  
find . -name "name" | xargs tar -cvf tar_name.tar.gz

解决文件名是空格的问题

1
find . -name "name" -print0 | xargs -0 tar -cvf tar_name.tar.gz

date

1
date +'%Y-%m-%d %H:%M:%S'  
date -d 'time' +%s

top

1
top -b 以文本方式(不加,则写入到文本中会出现医疗之外的字符)  
top -n num top 只运行 num 次

uptime

1
uptime 列出cpu平均 负载均衡 等价于 cat /proc/loadavg  
mysql 多限定条件  
where condition and condition  
mysql limit m, n 是指记录m处 向后查找 n条记录

crontab 实现秒级监控

1
1. \* \* \* \* \* /bin/date  
2. \* \* \* \* \* sleep 20; /bin/date  
3. \* \* \* \* \* sleep 40; /bin/date

virtual box 安装增强功能

1
只要加virtual box 提供的ISO 加载到 系统即可 ,进入系统 /media 进行安装即可

nohup

1
nohup shell_script & 始终在后台运行程序用户的密码

usermod 更改用户名(不能修改当前用户)

1
usermod -Inewuser -d /home/newuser -m olduser  
可以使用root修改 /etc/passwd 然后修改

shell 中printf 使用

1
printf "%s,%s" str1 str2 (变量之间可以需要空格,格式串之前不需要空格

awk 中分隔符的指定

1
FS="[sp1sp2]" (分隔符之间不需要有间隔符,若需要转移只要加上\即可,所有分隔符使用 []"

安装 ibus

1
sudo apt-get install ibus ibus-clutter ibus-gtk ibus-gtk3 ibus-qt4  
im-switch -s ibus  
sudo apt-get install ibus-pinyin  
ibus-setup

ip to num(python)

1
print socket.ntohl(struct.unpack\_from('=L', socket.inet_aton(sys.argv[2]))[0])

num to ip (python)

1
print socket.inet_ntoa(struct.pack('=L', socket.htonl(string.atoi(sys.argv[2]))))

time to timestamp (python)

1
print (int(time.mktime(time.strptime(sys.argv[2], "%Y-%m-%d"))))

timestamp to time (python)

1
print (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(sys.argv[2]))))

shell生成随机字符串

1
date +%s%N | md5sum | head -c 10

svn 相关操作

1
svn co -r版本号 snv_address  
svn status
svn export rep-url
svn diff
svn ci fils -m"x"

svn 出现 错误

1
先删掉 .svn文件(重要) 也可以先svn co  
再进行 svn add  
在svn ci

查看哪个端口 当前有哪些连接,有哪些状态

1
netstat -nap  
lsof -i: port
tcpdump

脚本中命令最好都加上 -v 参数

1
grep -v -w -r  
rm -i -r -f -v  
cp -v

dos2unix

1
dos2unix 将dos文件格式中的^M去除

xargs使用注意

1
xargs -I 后面变量重命名最好用 {} 即 xargs -I{} cmd... {}

tr 转换大小写

1
echo "dsd" | tr "[a-z]" "[A-Z"

cut 按分隔符分割且取给定的字段

1
echo "ds:ds" | cut -f1 -d':'

磁盘,网络io监控工具

1
htop, top, netstat , tcpdump, free, du, dstat, iostat

在目录下 tar 文件注意

1
不要用 tar -czf name.tar.gz ./*  (源文件一定要指明) ./* 会将 name.tar.gz 一起打包

字符串和数字比较直接使用 = 号进行比较

1
if [ $str = "walter" ]; then
fi

if [ $str = 1 ]; then
fi

pushd, pod 多用于脚本递归目录时使用

1
pushd "$PWD" 1>/dev/null
cd $d 
do_recur_function
popd

shell中获取进程ID

1
echo \$\$ \[\$\$ 即是 进程ID]

shell编程

1
和其他编程一样,少用global variables, 函数内部使用local variables

find 使用

1
find . -ctime +n (创建时间在n+1 天以上)  -ctime n(创建时间在n 天) ctime -n (创建时间在n天以内)

bash set 使用

1
set -e 程序运行中有一处失败,程序就立即退出执行, -x 程序运行过程debug模式,输出信息

shell 加密文件

1
gzexe filename

()的使用

1
$() 是命令替换
eg: f=$(ls) : f 是本目录下文件的集合
date=$(date +"%Y-%m-%d %H-%m-%S")

${} 的使用

1
${} 是变量替换
A=B
echo ${A}B : AB
最常用的用途是变量替换
file=/dir1/dir2/dir3/my.file.txt
${file#*/}:拿掉第一条 / 及其左邊的字串:dir1/dir2/dir3/my.file.txt
${file##*/}:拿掉最后一条 / 及其左邊的字串:my.file.txt
${file#*.}:拿掉第一個 .  及其左邊的字串:file.txt
${file##*.}:拿掉最后一個 .  及其左邊的字串:txt
${file%/*}:拿掉最后一条 / 及其右邊的字串:/dir1/dir2/dir3
${file%%/*}:拿掉第一条 / 及其右邊的字串:(空值)
${file%.*}:拿掉最后一個 .  及其右邊的字串:/dir1/dir2/dir3/my.file
${file%%.*}:拿掉第一個 .  及其右邊的字串:/dir1/dir2/dir3/my
截取字符串的n个字符
${file:n:m}

记忆的方法为:

1
\# 是去掉左邊(在键盘上 # 在 $ 之左边)
% 是去掉右邊(在键盘上 % 在 $ 之右边)

$(()) 用来整数运算的

1
eg: a = 1
    b = 2
    c = $((a+b))

tcpdump 打印收到包的详细信息

1
tcpdump -Avvvs 1500 -i eth0 src port 80[3306] 返回
tcpdump -Avvvs 1500 -i eth0 dst port 80[3306] 请求

当前用户已另外一用户执行脚本无需输入密码

1
在 sudoer 中添加 user1 ALL=(user2) NOPASSWD: /home/user2/bin/test.sh
使用 sudo -u user2 /home/user2/bin/test.sh

查看二进制文件

1
2
hd 用来cat 二进制文件
bvi 用来编辑 二进制文件 (首先 apt-get install bvi)

查看系统的发行版信息

1
lsb_release -a

使用 bash 的严格模式[非官方]

1
2
set -euo pipefail
IFS=$'\n\t'

参考:http://redsymbol.net/articles/unofficial-bash-strict-mode/

vim 中快速分屏

  • ctrl-w v 竖屏
  • ctrl-w s 横屏

bash 设置时区

  • 首先查看 /etc/timezone 下的时区,也可以使用 date -R
  • 修改时区
  • tzselect(设置完后需要export. eg:TZ=’America/Anguilla’; export TZ)
  • 仅限于RedHat Linux 和 CentOS
    timeconfig
  • 适用于Debian
    dpkg-reconfigure tzdata
  • 复制相应的时区文件,替换系统时区文件;或者创建链接文件
    cp /usr/share/zoneinfo/$主时区[Asia]/$次时区[Shanghai] /etc/localtime

参考:
http://www.cnblogs.com/h2appy/archive/2008/11/27/1342029.html

tmux 使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1. kill -9 tmux  # 干掉tmux session
2. tmux attach # attach之前的 session
3. prefix + : 输入 source-file ~/.tmux.conf 更改热更新

# 下面是我的配置
set -g prefix C-q
unbind C-b

# Status bar
# colors
set -g status-bg black
set -g status-fg white

# alignment
set-option -g status-justify centre

# spot at left
set-option -g status-left '#[bg=black,fg=green][#[fg=cyan]#S#[fg=green]]'
set-option -g status-left-length 20

# window list
setw -g automatic-rename on
set-window-option -g window-status-format '#[dim]#I:#[default]#W#[fg=grey,dim]'
set-window-option -g window-status-current-format '#[fg=cyan,bold]#I#[fg=blue]:#[fg=cyan]#W#[fg=dim]'

# spot at right
set -g status-right '#[fg=green][#[fg=cyan]%Y-%m-%d %H:%M:%S#[fg=green]]'

# select window
# up
bind-key k select-pane -U
# down
bind-key j select-pane -D
# left
bind-key h select-pane -L
# right
bind-key l select-pane -R

tmux 中 vim powerline 显示不对解决

1
2
3
在 .bashrc 中增加
export TERM="screen-256color"
alias tmux="tmux -2"

留言

Jun 28 2015