常用 shell 工作脚本重点总结
一、常用的工作脚本
1.批量创建文件 — 创建/shi 目录,并用 for i in 方式在/shi 目录下面循环批量创建 10 个文件
方法 1:
[root@localhost ~]# ls /
没有 shi 目录
[root@localhost ~]# ls /shi
ls: cannot access /shi: No such file or directory
[root@localhost ~]# cat a.sh
#!bin/bash
mkdir -p /shi && cd /shi
for i in {1..10}
do
touch shi-$i.txt
done
[root@localhost ~]# sh a.sh
[root@localhost ~]# ls /shi
shi-10.txt shi-1.txt shi-2.txt shi-3.txt shi-4.txt shi-5.txt shi-6.txt shi-7.txt shi-8.txt shi-9.txt
方法 2:
在/a 目录下批量创建文件
[root@localhost ~]# ls /a
空
[root@localhost ~]# cat a.sh
#!/bin/bash
for i in `seq 1 10`
do
touch /a/file$i.txt
done
[root@localhost ~]# sh a.sh
[root@localhost ~]# ls /a
file10.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt
注意 seq 格式:
格式 1: seq 初始值 步长值 结束值
格式 2: seq 初始值 结束值 #默认步长值是 1
2.批量创建用户 用 for i in 循环批量创建 10 个系统用户(shi-N)并设置密码,密码和用户名相同。
[root@localhost ~]# seq -w 10
01
02
03
04
05
06
07
08
09
10
[root@localhost ~]# cat a.sh