系统管理部分一:
分区、创建、查看、调整、挂载、文件系统的结构、硬链接、软链接
脚本"语法错误"非逻辑错误检测:
# bash -n script.sh
单独执行,脚本执行每个代码
# bash -x script.sh
+ 程序在执行
没有+ 程序过程中应该输出的信息
脚本的格式
+++++++++++++++++++++++++++++++++非格式,用于分割++++++++++++++++++++++++++++++++++
#!/bin/bash ##魔数
# Version: major.minor.release (主版本呈,次版本呈,发行号)
# Author: ##作者
# Description: ##对脚本的描述信息
+++++++++++++++++++++++++++++++++非格式,用于分割++++++++++++++++++++++++++++++++++
#号开头为注释
read命令
1、查看所有内建命令 [[email protected] ~]# enable -a enable . ... enable read ##内建命令 2、获取帮助 [[email protected] ~]# 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、一行从标准输入读入后,切割此行成字段,对位保存字段至变量中
[[email protected] ~]# read name hello obama! [[email protected] ~]# echo $name hello obama! [[email protected] ~]# read name obama [[email protected] ~]# echo $name obama
2、对位保存字段释义,如果多余的位,变量为空
[[email protected] ~]# read a b c hello obama! [[email protected] ~]# echo $a hello [[email protected] ~]# echo $b obama! [[email protected] ~]# echo $c [[email protected] ~]#
3、等待用户输入命令
语法: read -p ‘PROMPT‘ name
相当于: echo -n "PROMPT" ; read name
[[email protected] ~]# printf "Enter a username: "; read name Enter a username: obama [[email protected] ~]# echo $name obama [[email protected] ~]#
4、避免用户不输入,堵塞在此处,给出超时。此时变量为空
[[email protected] ~]# read -t 5 -p ‘Enter a name: ‘ name Enter a name: [[email protected] ~]# echo $name [[email protected] ~]#
脚本示例:
提示用户输入一个设备文件,存在则显示磁盘信息。
1、脚本
#!/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、检测语法错误
[[email protected] scripts]# bash -n test.sh
3、给x权限
[[email protected] scripts]# chmod +x test.sh
4、给一个路径测试
1、正确路径 [[email protected] 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、错误路径 [[email protected] scripts]# ./test.sh Enter a disk special file: how No such file. [[email protected] scripts]# echo $? 1 [[email protected] scripts]#
时间: 2024-10-11 06:35:16