shell脚本笔记

367 阅读1分钟

多数程序命令行的退出快捷键ctrl+D

基本操作

帮助

man [command]
info [command]
whatis [command]

系统和安全信息

uname -a 
who
history
last 
lastb
date +%F\ %T 或 date +%F' '%T
datectl

Utils

bc

定时任务和服务

ctrontab -l
systemctl
systemctl start [servicename]
journalctl

资源管理

free -h  或 cat /proc/meminfo
cat /proc/cpuinfo
top 或htop
fdisk -l
du -sh
df -h
ll
ls

网络

ip address 或ifconfig
ip route
lsof -i :[端口] 或 netstat -aln
nc #端口连通性  nc -zv [ipOrDomain] [port]
telnet
ss

安装与文件操作

curl -OkL 或 wget
dpkg
apt
tar -xzvf
unzip
cp
mv
chown
chmod
rm -r
mkdir -p
pwd
tail -f
head
cat
less
more
wc   word count可以通过ls -l|grep ^-|wc -l计算目录下文件数量
tee  实现重定向到文件(即 > )的同时仍能通过管道 (|)传给接下来的命令

shell

逻辑

# 条件
if [condition1]; then
    ...
elif [condition2]; then
    ...
else
    ...
fi
# 多重分支
case $variable in 
  "case1")
    ...
  ;;
  "case2")
    ...
  ;;
  *)
    ...
  ;;
esac
# 循环
## 终止循环break,跳出本次循环continue
## 当满足condition时循环,一直到不满足conditon,  若conditon为true或:则为无限循环
while [condition]
do
    ...
done
或者
## 不满足conditon时循环,当满足condition时,终止循环
until [ condition ]
do
    ...
done
或者
for var in condition1 condition2 condition3 ...
do
    ...
done
或者
for (( 初始值;限制值;执行步长))
do
    ......
done

test条件判断

-eq           //等于
-ne           //不等于
-gt            //大于 (greater than)
-lt            //小于 (less than)
-ge            //大于等于
-le            //小于等于

规则

在命令中如果要使用命令,则使用反引号`command`或者$(command)的形式 如 echo `ls -l`
字符串拼接 如  content=${var}", world"
read 从键盘读取变量