1. 程式人生 > >謝煙客---------Linux之bash腳本編程---用戶交互

謝煙客---------Linux之bash腳本編程---用戶交互

linux編程

系統管理部分一:

分區、創建、查看、調整、掛載、文件系統的結構、硬鏈接、軟鏈接


腳本"語法錯誤"非邏輯錯誤檢測:

# 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]#


本文出自 “Reading” 博客,請務必保留此出處http://sonlich.blog.51cto.com/12825953/1955162

謝煙客---------Linux之bash腳本編程---用戶交互