man hier 描述文件系统目录结构
dmesg 检测到的 硬件和启动信息
cat /proc/devices 当前配备的设备
sysctl -a 显示所有系统限制
make(){ echo make4 return 4 } show(){ echo show1 return 1 } make echo $? show echo $?
执行结果:
make4
4
show1
1
检测某地址是否可通
ping -c 1 $1 |grep "100% packet loss"|wc -l
ping -c 1 $1 //只ping一次
if [ $? == 0 ];then
echo yes
else
echo no
fi
关于 dd 命令
dd if=/dev/zero of=/home/mnt/disk bs=1M count=10
/dev/zero 是一个字符设备,会不断返回零值字节。
块大小可以使用的单位 k M G
一个命令结果的查询
if netstat -tnpl | grep -q nginx;then echo "$NAME (pid ) already running." exit 1 fi
一个命令是否存在的查询
command_exists() { command -v "$@" > /dev/null 2>&1 }
一个段落文字的输出
cat <<-EOF If you would like to use Docker as a non-root user, you should now consider adding your user to the "docker" group with something like: sudo usermod -aG docker $your_user Remember that you will have to log out and back in for this to take effect! EOF
一个命令的结果
if netstat -tnpl | grep -q nginx;then echo "$NAME (pid ) already running." exit 1 fi
true命令,什么都不做,设置 退出码为0
false命令,什么都不做,设置 退出码为1
fuer 命令。
fuser 可以显示出当前哪个程序在使用磁盘上的某个文件、挂载点、甚至网络端口,并给出程序进程的详细信息.
fuser -um /dev/sda2
显示使用某个文件的进程 这个命令在umount的时候很有用,可以找到还有哪些用到这个设备
fuser -m -k -i readme.txt
杀掉打开 readme.txt文件的进程
fuser -v -n tcp 80
查看哪些进程使用 tcp 80端口
一个awk的用法,ps -ef 获取第一列是mysql(user) 的进程pid号
ps -ef |grep -v grep |awk '{if($1=="mysql"){print $2}}'
给PATH变量加路径
[root@localhost home]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@localhost home]# PATH=$PATH:/home/local [root@localhost home]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/home/local
获取参数选项
https://segmentfault.com/a/1190000010171506
#!/bin/bash usage() { echo "Usage:" echo " test.sh [-j JAVA_DIR] [-m MAVEN_DIR]" echo "Description:" echo " JAVA_DIR, the path of java." echo " MAVEN_DIR, the path of maven." exit -1 } upload="false" while getopts 'h:j:m:u' OPT; do case $OPT in j) JAVA_DIR="$OPTARG";; m) MAVEN_DIR="$OPTARG";; u) upload="true";; h) usage;; ?) usage;; esac done echo $JAVA_DIR echo $MAVEN_DIR echo $upload
信号的捕捉和处理
[root@localhost ~]# cat 1.sh #!/bin/bash trap "echo 'Sorry! I have trapped Ctrl-C'" SIGINT for (( i = 0; i < 10; i++ )); do echo hello${i} sleep 1 done
[root@localhost ~]# sh 1.sh
hello0
hello1
^CSorry! I have trapped Ctrl-C
hello2
hello3
^CSorry! I have trapped Ctrl-C
hello4
hello5
hello6
hello7
hello8
^CSorry! I have trapped Ctrl-C
hello9
#!/bin/bash
make(){
echo "we have trapped ctrl + c"
}
trap make SIGINT
for (( i = 0; i < 10; i++ )); do
echo hello${i}
sleep 1
done
捕获一个脚本的推出,trap 后面加上EXIT 就行
[root@localhost ~]# cat 1.sh #!/bin/bash make(){ echo "we exit" } trap make EXIT echo "start" sleep 1 echo "end" [root@localhost ~]# sh 1.sh start end we exit
[root@localhost ~]# cat 1.sh #!/bin/bash make(){ echo "we exit" } trap make EXIT echo "start" sleep 1 echo "end" [root@localhost ~]# sh 1.sh start end we exit
trap -- SIGINT (--、- 都可以恢复信号的默认行为)
文件内容赋值变量
read _RET < "${path}" || _RET="$ERROR" while read line do cmd done <FILE