#for循环:
for var_Name in 列表; do
语句1
语句2
...
done 写一个脚本:添加10个用户,user101-user110:
#!/bin/bash
for i in {101..110}; do
useradd user$i
echo "useradd user$i"
done
bash –n 脚本文件 检查语法错误(变量等输入错误不提示,无错误无显示)
[root@VM_168_102_centos ~]# bash -n for_test1.sh
for_test1.sh: line 7: syntax error: unexpected end of file
[root@VM_168_102_centos ~]#
bash –x 显示脚本运行的每一步骤(帮助分析脚本)
[root@VM_168_102_centos ~]# bash -x for_test1.sh
+ for i in '{101..110}'
+ useradd user101
useradd: user 'user101' already exists
+ echo 'useradd user101'
useradd user101
+ for i in '{101..110}'
+ useradd user102
useradd: user 'user102' already exists
+ echo 'useradd user102'
useradd user102
+ for i in '{101..110}'
+ useradd user103
useradd: user 'user103' already exists
+ echo 'useradd user103'
useradd user103
+ for i in '{101..110}'
+ useradd user104
useradd: user 'user104' already exists
+ echo 'useradd user104'
useradd user104
+ for i in '{101..110}'
+ useradd user105
useradd: user 'user105' already exists
+ echo 'useradd user105'
useradd user105
+ for i in '{101..110}'
+ useradd user106
useradd: user 'user106' already exists
+ echo 'useradd user106'
useradd user106
+ for i in '{101..110}'
+ useradd user107
useradd: user 'user107' already exists
+ echo 'useradd user107'
useradd user107
+ for i in '{101..110}'
+ useradd user108
useradd: user 'user108' already exists
+ echo 'useradd user108'
useradd user108
+ for i in '{101..110}'
+ useradd user109
useradd: user 'user109' already exists
+ echo 'useradd user109'
useradd user109
+ for i in '{101..110}'
+ useradd user110
useradd: user 'user110' already exists
+ echo 'useradd user110'
useradd user110
[root@VM_168_102_centos ~]# tail /etc/passwd | cut -c 2
s
s
s
s
s
s
s
s
e
e
[root@VM_168_102_centos ~]# tail /etc/passwd | cut -c 2-4
ser
ser
ser
ser
ser
ser
ser
ser
est
est
[root@VM_168_102_centos ~]# tail /etc/passwd | cut -c 2,5
s1
s1
s1
s1
s1
s1
s1
s1
eu
eu
#sort命令:依据不同的数据类型进行排序
[root@VM_168_102_centos ~]# cat sort.sh
q
s
w
d
f
v
A
R
T
1
4
6
78
2
[root@VM_168_102_centos ~]# sort sort.sh
1
2
4
6
78
A
R
T
d
f
q
s
v
w
sort -f:排序时,忽略字符大小写
[root@VM_168_102_centos ~]# sort sort.sh
1
2
4
6
78
A
R
T
d
f
q
s
v
w
[root@VM_168_102_centos ~]# sort -f sort.sh
1
2
4
6
78
A
d
f
q
R
s
T
v
w
sort -f:按数值大小进行排序
[root@VM_168_102_centos ~]# sort -n sort.sh
A
R
T
d
f
q
s
v
w
1
2
4
6
78
#uniq命令:移除连续重复的行
[root@VM_168_102_centos ~]# cat uniq.sh
a
a
a
b
f
b
b
c
e
[root@VM_168_102_centos ~]# uniq uniq.sh
a
b
f
b
c
e
uniq -c:显示每行重复的次数
[root@VM_168_102_centos ~]# uniq -c uniq.sh
3 a
1 b
1 f
2 b
1 c
1 e
uniq -d:仅显示连续重复的行
[root@VM_168_102_centos ~]# cat uniq.sh
a
a
a
b
f
b
b
c
e
[root@VM_168_102_centos ~]# uniq -d uniq.sh
a
b
uniq -u:显示不连续重复的行
[root@VM_168_102_centos ~]# cat uniq.sh
a
a
a
b
f
b
b
c
e
[root@VM_168_102_centos ~]# uniq -u uniq.sh
b
f
c
e