Read of Linux built-in commands

Source: Internet
Author: User

I. The role of Read

Read to save the user's keyboard input string in the following variables

[[email protected] scripts]# read Num1[[email protected] scripts]# echo $Num 1[[email protected] scripts]# read Num123[[ema Il protected] scripts]# echo $Num 123


You can enter any information to the script through read, or you can read multiple values from the command line to the variable

[[email protected] scripts]# Read Num Name Year1 arvin 2015[[email protected] scripts]# echo $Num $Name $Year 1 Arvin 2015

----------------------------------------------

[[email protected] scripts]# Read Num Name Year1 arvin 2014[[email protected] scripts]# echo $Num 1[[email protected] scripts]# echo $NameArvin [[email protected] scripts]# echo $Year #这里把最后两个字符传递给了变量Year


Second, read common parameters

-P (): Added hint message # Another echo-n after displaying the information, does not break the line

-T: Specifies the time-out period, in which the read command does not interact, and the timeout exits


Iii. Examples and Exercises


Example 1:

#!/bin/bash#read–p "Input a character:" Charcase $Char in[0-9]) echo "a digit.";;        [A-z]) echo "A lower.";;        [A-z]) echo "A Upper.";;        [[:p UNCT:]]) echo "A punction.";; *) echo "A special char."; Esac


Example 2 :

#!/bin/bash#read-p "Do you agree [Yes|no]?:" Yesnocase $YesNo iny| y| [Yy]es] echo "agreed, proceed.";; n| n| [Nn]o] echo "disagreed, can ' t proceed."; *) echo "Invalid input.";; Esac

Example 3: Write a script
1. Display the following menu to the user:
m| M) show memory usages;
d| D) show disk usages;
q| Q) quit
2. If the user selects the first item, the memory usage information is displayed;
If the second item is selected, the disk mount and usage information is displayed;
If it is the third item, exit and show opt-out;
Any other content that describes the error options;

#!/bin/bash#cat << eofm| M) Show Memory usages;d| D) Show Disk usages;q| Q) quiteofread-t 20-p "Press a choice:" Keyif [-Z $Key];    Then echo "No choice,quit." Key=q Exit 3ficase $Key inm| M) free-m;; d| D) Du-lh;; q|    Q) echo "Quit." Exit 5;;    *) echo "Invalid input." ;; Esac


Exercise 1

#!/bin/bash# date: 2015-04-13# description:# version: 1.0if [ ! -e  /backup ];then     #这里可以用 [ -d /backup ] | |  mkdir /backup  substitution     mkdir /backupfiread -t 20 -p  "Press a command{gzip|bzip2|xz}:"  Command     #read, 20 seconds timeout, assign command value command= $1if [ -z $1 ]; then     #如果脚本给定的是空值, the default input gzip     Command=gzipficase  $Command  ingzip)     tar czfp /backup/ backupfile-' date +%f-%h-%m-%s ' .tar.gz /etc/    ret=$?    ;; bzip2)     tar cjfp /backup/backupfile-' date +%f-%h-%m-%s '. tar.bz2 /etc /    ret=$?    ;; XZ)     tar cjfp /backup/backupfile-' date +%f-%h-%m-%s '. tar.xz /etc/    Ret=$?    ;; *)     echo  "Usage: ' basename $0 ' {gzip | bzip2 | xz }."     exit 5    ;; esac    [  $Ret  -eq 0 ] && echo  "backup etc  successfully. ($Command) "     #通过每个循环分支的返回值

This article is from the "Arvin Lau" blog, be sure to keep this source http://64314491.blog.51cto.com/2784219/1631952

Read of Linux built-in commands

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.