linux交互式脚本编写,谢烟客---------Linux之bash脚本编程---用户交互

本文详细介绍了 Bash 脚本中 read 命令的用法,包括如何从标准输入读取数据并将其拆分为多个字段,以及如何通过选项进行超时设置和提示信息定制。并通过示例展示了如何使用 read 命令来实现用户交互式的脚本。

系统管理部分一:

分区、创建、查看、调整、挂载、文件系统的结构、硬链接、软链接

脚本"语法错误"非逻辑错误检测:

# bash -n script.sh

单独执行,脚本执行每个代码

# bash -x script.sh

+        程序在执行

没有+    程序过程中应该输出的信息

脚本的格式

+++++++++++++++++++++++++++++++++非格式,用于分割++++++++++++++++++++++++++++++++++

#!/bin/bash     ##魔数

# Version: major.minor.release (主版本呈,次版本呈,发行号)

# Author:      ##作者

# Description:   ##对脚本的描述信息

+++++++++++++++++++++++++++++++++非格式,用于分割++++++++++++++++++++++++++++++++++

#号开头为注释

read命令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、一行从标准输入读入后,切割此行成字段,对位保存字段至变量中[root@localhost ~]# read name

hello obama!

[root@localhost ~]# echo $name

hello obama!

[root@localhost ~]# read name

obama

[root@localhost ~]# echo $name

obama

2、对位保存字段释义,如果多余的位,变量为空[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[root@localhost ~]# printf "Enter a username: "; read name

Enter a username: obama

[root@localhost ~]# echo $name

obama

[root@localhost ~]#

4、避免用户不输入,堵塞在此处,给出超时。此时变量为空[root@localhost ~]# read -t 5 -p 'Enter a name: ' name

Enter a name: [root@localhost ~]# echo $name

[root@localhost ~]#

脚本示例:

提示用户输入一个设备文件,存在则显示磁盘信息。

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、检测语法错误[root@localhost scripts]# bash -n test.sh

3、给x权限[root@localhost scripts]# chmod +x test.sh

4、给一个路径测试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]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值