记录我常用的 shell 命令。
基础
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
文件描述符
Shell 中描述符一共有12个
0
标准输入
1
标准输出
2
错误输出
3-9
空白描述符
exec 3>&1 4>&2 1>> bash.log 2>&1
说明:复制标准输出到3
,错误输出到4
,把3、4
保存在 bash.log
中
compgen
- compgen 是 bash 中自动完成命令补全的内置命令
compgen
由bash
包提供,按两次TAB
键即可调用该命令
$ type compgen
compgen is a shell builtin
$ compgen --help
compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
Display possible completions depending on the options.
Intended to be used from within a shell function generating possible
completions. If the optional WORD argument is supplied, matches against
WORD are generated.
Exit Status:
Returns success unless an invalid option is supplied or an error occurs.
# 列出所有用户
compgen -u
# 列出当前用户可以运行的所有命令
compgen -c
# 列出所有别名
compgen -a
# 列出所有可以运行的函数
compgen -A function
# 查看 Shell 保留关键字
compgen -k
complete
$ type complete
complete is a shell builtin
$ complete --help
complete: complete [-abcdefgjksuv] [-pr] [-DEI] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [name ...]
Specify how arguments are to be completed by Readline.
...
说明:
# auto complete command container log
_log() {
local pre cur opts
pre=${COMP_WORDS[COMP_CWORD-1]}
cur=${COMP_WORDS[COMP_CWORD]}
opts="-f -r -t -w -o --output -v --version -h --help" #补全选项
COMPREPLY=( $( compgen -W "$opts" -- $cur ) )
}
complete -F _log log
说明:
-
_log
函数名一般以下划线 _
开头,该函数接收三个参数:要补全的命令名
、当前光标所在的词
、当前光标所在的词的前一个词
,生成的补全结果需要存储到 COMPREPLY
变量中,以待 bash
获取
-
类似的方法可以在 /etc/bash_completion.d/
或 ~/.bash_completion
找到使用示例
-
在脚本中加载如上脚本,bash
在遇到 log
这个词时,调用 _log
函数生成补全内容
$ log # 按两次 TAB
logger login loginctl logname logout logrotate logsave
$ log - # 按两次 TAB,以下显示 opts 中的提示
-f --help --output -t --version
-h -o -r -v -w
shopt
- shopt : Set and unset shell options,是 set 命令的超集
shopt --help
shopt: shopt [-pqsu] [-o] [optname ...]
Set and unset shell options.
Change the setting of each shell option OPTNAME. Without any option
arguments, list each supplied OPTNAME, or all shell options if no
OPTNAMEs are given, with an indication of whether or not each is set.
Options:
-o restrict OPTNAMEs to those defined for use with `set -o'
-p print each shell option with an indication of its status
-q suppress output
-s enable (set) each OPTNAME
-u disable (unset) each OPTNAME
Exit Status:
Returns success if OPTNAME is enabled; fails if an invalid option is
given or OPTNAME is disabled.
函数
Shell函数返回值,一般有3种方式:
#!/bin/bash
g_var=
function testresult()
{
echo "args $1"
g_var=$1
return 0
}
result=$(testresult 1)
echo "return is $?"
echo "echo is $result"
testresult 1
echo "argv is g_var=$g_var"
$ bash test.sh
return is 0
echo is args 1
args 1
argv is g_var=1
条件判断
if
if [ "x${p1}" != "x" ]; then
echo "Param1 不为空"
else
echo "Param1 为空或未定义"
fi
test
test -e filename
:检查文件是否存在。如果文件存在则返回 1,如果文件不存在则返回 0。
test -d filename
:检查文件是否为目录。如果文件是目录,则返回 0;如果文件不是目录,则返回 1。
test -f filename
:检查文件是否为普通文件。如果文件是常规文件,则返回 0;如果文件不是常规文件,则返回 1。
test -s filename
:检查文件是否为空文件。如果文件不是空的,则返回 0;如果文件是空的,则返回 1。
test -r filename
:检查文件是否可读。如果文件可读,则返回 0;如果文件不可读,则返回 1。
test -w filename
:检查文件是否可写。如果文件可写,则返回 0;如果文件不可写,则返回 1。
test -x filename
:检查文件是否可执行。如果文件可执行,则返回 0;如果文件不可执行,则返回 1。
du
du -lh
du -sh *
进入占用空间比较大的文件夹,然后再使用 du -sh *
查看根目录下每个文件夹的大小
查找最占空间的10个文件
du -a /data | sort -n -r | head -n 10
或
cd /data
du -hsx * | sort -rh | head -10
或
du -ah . | sort -n -r | head -n 10
- du : 计算出单个文件或者文件夹的磁盘空间占用
- sort : 对文件行或者标准输出行记录排序后输出
- head : 输出文件内容的前面部分
查找最大占用空间的目录
du -h --max-depth=1 /data/
du -sh .
du -h -d 1
du -h -d 1 | sort -hr | head -3
EOF
cat > abc.txt << EOF
some txt
EOF
- 内容中若包含
反引号
、美元符号$
,切不期望进行解析处理,可以将第一个 EOF
改为 \EOF
cat > abc.txt << \EOF
some txt $PATH
EOF
find
查找大于10M的文件
find / -type f -size +10000000c -exec du -sh {} \;
dos2unix
find -type f | xargs dos2unix -o
chmod
find ./ -type f -name "*.sh" | xargs -i chmod +x {}
mv
find . -type f -name "*.yaml.j2" | sed 's/\.yaml\.j2//1' | xargs -i mv {}.yaml.j2 {}.yaml
find . -type f -name "*.j2" | sed 's/\.j2//1' | xargs -i mv {}.j2 {}
将当前目录下,以.conf
结尾的文件,更名为.conf.bak
find ./ -type f -name "*.conf" | sed 's/\.conf//1' |xargs -i mv {}.conf {}.conf.bak
find -type f -name "*.repo"| sed 's/\.repo//1' | xargs -i mv {}.repo {}.repo.bak
清理日志
for i in `find . -name "*.log"`; do cat /dev/null >$i; done
pstree
查看进程tree
pstree [PID] [USER]
pstree <pid>
pstree root
sudo
sudo -u xiexianbin -s bash -l -c "env;whoami"
使用 xiexianbin
用户执行命令,并加载环境变量
- -u 指定用户
- -s –shell=SHELL:启动指定shell
- -l -,–login:使用登录shell,使用此参数,系统环境变量和home目录都会设置为目标用户的,未指定目标用户则默认是root
- -c –command=COMMAND:变更账号后,执行COMMAND指令,然后退回原用户
fping
fping -aAD -l -e -s -f floatingip-list.txt
fping -aAD -l -e -s -i25 -f floatingip-list.txt 2>&1 >> floatingip.log
fping -aAD -l -e -s -f floatingip-list.txt 2>&1 >> floatingip.log
fping -aADmles -i25 -f floatingip-list.txt 2>&1 >> floatingip.log
if
if 判断字符串
-z
(zero)判断 string 是否是空串,空为 true
-n
(non-zero)判断 string 是否是非空串,非空为 true
s=""
if [ -z "$s" ]; then
echo "empty str"
fi
if [ -n "$s" ]; then
echo "no empty str"
fi
lsof
查看依赖:
lsof | grep libssl | awk '{print $1}'| sort | uniq
查看端口连接:
lsof -i:3306
查看文件使用:
lsof /var/lib/mysql/aria_log_control
iostat
iostat
iostat -x 1
iotop
iotop
iperf
iperf 是一个网络性能测试工具
查看多播:
yum install iperf -y
iperf -s -u -B 224.0.55.55 -i 1
iperf -c 224.0.55.55 -u -T 32 -t 3 -i 1
top
top
perf top
for
for pid in `ps -ef | grep rabbitmq | awk '{print $2}'`; do kill -9 $pid; done
for ip in ${JOB_IP[@]}; do
rcmd root@$ip "source $CTRL_DIR/install.rc; gen_job_cert"
done
diff
diff -rq abc/ def/
print
print
是 ksh
的内置命令
print
输出会自动换行,printf
不会自动换行
print
中不能使用占位符
printf
printf
是 bash 的内置命令
printf
和 C 的 printf 功能一样
- 格式
printf '输出类型输出格式' 内容
输出类型:
%ns
输出字符串,输出n个字符
%ni
输出整数,指输出n个数字
%m.nf
输出浮点数,m 个整数位数和 n 个小数位数。如 %8.2f
代表共输出8位数,其中2是小数,6是整数
输出格式:
-
\a
输出警告声音
-
\b
输出退格键,即Backspace键
-
\f
清除屏幕
-
\n
换行
-
\r
回车,也就是Enter键
-
\t
水平输出制表符,也就是Tab键
-
\v
垂直输出制表符,也就是Tab键
-
示例
printf '%s\t %s\t %s\t %s\n' $(cat user.txt)
解析
ping
nslookup ## 安装 yum install bind-utils
dig
ping -b "224.0.1.103"
获取本机公网IP
curl ifconfig.me
curl ip.xiexianbin.cn
获取本机的私有IP地址
hostname -I
网络相关
traceroute
traceroute xiexianbin.cn
xargs
使用 -d
指定切分符
xargs -d '\n' echo
F&Q
运行shell 报错 not found
[[ : not found
sh
不支持的语法,使用 bash
执行