Puff---------Linux bash scripting---user interaction

Source: Internet
Author: User

System Administration Section One:

partitioning, creating, viewing, resizing, mounting, file system structure, hard links, soft links


Script "syntax error" non-logical error detection:

# bash-n Script.sh


Executed separately, the script executes each code

# bash-x Script.sh

+ Program in execution

No + information that should be output during the program


Format of the script

+++++++++++++++++++++++++++++++++ non-format for splitting ++++++++++++++++++++++++++++++++++

#!/bin/bash # #魔数

# Version:major.minor.release (main version presented, minor version presented, issue number)

# Author: # #作者

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

+++++++++++++++++++++++++++++++++ non-format for splitting ++++++++++++++++++++++++++++++++++

#号开头为注释


Read command

1. View all built-in commands [[email protected] ~]# enable-aenable .... Enable read # #内建命令2, get help [[email protected] ~]# assist Readread a line F Rom the standard input and split it into field. After a row has been read from the normal input, cut the line into fields with white space characters, save the field to the variable ****** for a special scene, a scene that requires people to participate **********read [ OPTIONS ....]    [Name ...] -P "PROMPT" # # hint-t Timeout # # Timeout long, in seconds read-p "Enter a name:" Name equals: Echo-n "Enter a name:"; Read name


Using the example

1, a row from the standard input read, cut this line into a field, the bit to save the field into the variable

[email protected] ~]# Read Namehello obama! [Email protected] ~]# echo $namehello obama! [email protected] ~]# read Nameobama[[email protected] ~]# echo $nameobama

2, the bit to save the field interpretation, if the extra bit, the variable is empty

[email protected] ~]# read a b chello obama! [[email protected] ~]# echo $ahello [[email protected] ~]# echo $bobama! [Email protected] ~]# echo $c [[email protected] ~]#

3. Wait for user input command

Syntax: read-p ' PROMPT ' name

Equivalent: Echo-n "PROMPT"; Read name

[[email protected] ~]# printf "Enter a username:"; Read NameEnter a username:obama[[email protected] ~]# echo $nameobama [[email protected] ~]#

4, to avoid user input, plug in here, give a timeout. The variable is empty at this time

[[email protected] ~]# read-t 5-p ' Enter a name: ' NameEnter a name: [[email protected] ~]# echo $name [[email protected] ~]#


Script Example:

Prompts the user to enter a device file, which displays the disk information.

1. Scripts

#!/bin/bash# version:0.0.1# author:lcc.org# desc:read testingread-t 5-p ' Enter a disk special file: ' diskfile[-n ' $ Diskfile "] | | Exit 1 # # does not exist, then exit if Fdisk-l |        Fgrep "Disk $diskfile" >/dev/null 2>&1then fdisk-l $diskfile # # condition The execution status result is 0 exit 0else        echo "No such file." # # Condition Execution status result is not 0 exit 1fi

2. Detect syntax errors

[Email protected] scripts]# bash-n test.sh

3. Give x permission

[Email protected] scripts]# chmod +x test.sh

4. Give a path test

1. correct path [[Email protected] scripts]# ./test.shenter a disk special file:  /dev/sdadisk /dev/sda: 128.8 gb, 128849018880 bytes255 heads, 63  sectors/track, 15665 cylindersunits = cylinders of 16065 * 512  = 8225280 bytesSector size  (logical/physical):  512 bytes / 512  bytesI/O size  (Minimum/optimal): 512 bytes / 512 bytesdisk  identifier: 0x000777f3   device boot      start          End      Blocks    Id  System/dev/sda1   *            1          64       512000   83   linuxpartition 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 / SOLARIS2, error path [[email protected] scripts]#  ./test.shenter a disk&Nbsp;special file: howno such file. [[email protected] scripts]# echo $?1[[email protected] scripts]#


This article is from the "Reading" blog, make sure to keep this source http://sonlich.blog.51cto.com/12825953/1955162

Puff---------Linux bash scripting---user interaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.