系统管理部分一:
分区、创建、查看、调整、挂载、文件系统的结构、硬链接、软链接
脚本"语法错误"非逻辑错误检测:
# bash -n script.sh
单独执行,脚本执行每个代码
# bash -x script.sh
+ 程序在执行
没有+ 程序过程中应该输出的信息
脚本的格式
+++++++++++++++++++++++++++++++++非格式,用于分割++++++++++++++++++++++++++++++++++
#!/bin/bash ##魔数
# Version: major.minor.release (主版本呈,次版本呈,发行号)
# Author: ##作者
# Description: ##对脚本的描述信息
+++++++++++++++++++++++++++++++++非格式,用于分割++++++++++++++++++++++++++++++++++
#号开头为注释
read命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
1、查看所有内建命令
[root@localhost ~]
# enable -a
enable
.
...
enable
read
##内建命令
2、获取帮助
[root@localhost ~]
# help read
Read a line from the standard input and
split
it into fields.
一行从标准输入读入后,以空白字符切割此行成字段,对位保存字段至变量中
******用于特殊场景,需要人参与的场景**********
read
[OPTIONS....] [name...]
-p
"PROMPT"
## 提示
-t TIMEOUT
## 超时时长,单位为 秒
read
-p
"Enter a name: "
name 相当于:
echo
-n
"Enter a name: "
;
read
name
|
使用示例
1、一行从标准输入读入后,切割此行成字段,对位保存字段至变量中
1
2
3
4
5
6
7
8
|
[root@localhost ~]
# read name
hello obama!
[root@localhost ~]
# echo $name
hello obama!
[root@localhost ~]
# read name
obama
[root@localhost ~]
# echo $name
obama
|
2、对位保存字段释义,如果多余的位,变量为空
1
2
3
4
5
6
7
8
9
|
[root@localhost ~]
# read a b c
hello obama!
[root@localhost ~]
# echo $a
hello
[root@localhost ~]
# echo $b
obama!
[root@localhost ~]
# echo $c
[root@localhost ~]
#
|
3、等待用户输入命令
语法: read -p 'PROMPT' name
相当于: echo -n "PROMPT" ; read name
1
2
3
4
5
|
[root@localhost ~]
# printf "Enter a username: "; read name
Enter a username: obama
[root@localhost ~]
# echo $name
obama
[root@localhost ~]
#
|
4、避免用户不输入,堵塞在此处,给出超时。此时变量为空
1
2
3
4
|
[root@localhost ~]
# read -t 5 -p 'Enter a name: ' name
Enter a name: [root@localhost ~]
# echo $name
[root@localhost ~]
#
|
脚本示例:
提示用户输入一个设备文件,存在则显示磁盘信息。
1、脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/bin/bash
# Version: 0.0.1
# Author: Lcc.org
# Desc: read testing
read
-t 5 -p
'Enter a disk special file: '
diskfile
[ -n
"$diskfile"
] ||
exit
1
## 不存在,则退出
if
fdisk
-l |
fgrep
"Disk $diskfile"
>
/dev/null
2>&1
then
fdisk
-l $diskfile
## 条件的执行状态结果为0
exit
0
else
echo
"No such file."
## 条件的执行状态结果不为0
exit
1
fi
|
2、检测语法错误
1
|
[root@localhost scripts]
# bash -n test.sh
|
3、给x权限
1
|
[root@localhost scripts]
# chmod +x test.sh
|
4、给一个路径测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
1、正确路径
[root@localhost scripts]
# ./test.sh
Enter a disk special
file
:
/dev/sda
Disk
/dev/sda
: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors
/track
, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical
/physical
): 512 bytes / 512 bytes
I
/O
size (minimum
/optimal
): 512 bytes / 512 bytes
Disk identifier: 0x000777f3
Device Boot Start End Blocks Id System
/dev/sda1
* 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2
64 12813 102400000 8e Linux LVM
/dev/sda3
12813 14118 10489811 83 Linux
/dev/sda4
14119 15665 12426277+ 5 Extended
/dev/sda5
14119 15424 10490413+ 82 Linux swap / Solaris
2、错误路径
[root@localhost scripts]
# ./test.sh
Enter a disk special
file
: how
No such
file
.
[root@localhost scripts]
# echo $?
1
[root@localhost scripts]
#
|
本文转自 lccnx 51CTO博客,原文链接:http://blog.51cto.com/sonlich/1955162,如需转载请自行联系原作者