Shell programming two--read, array,

Source: Internet
Author: User
Tags ming

First, the use of Read

Function: Read input of keyboard

Usage:

  ①, simple shell read;

  

  Note: If you do not add a variable after read, the default assignment is to reply

[[email protected] opt]# Read (no add variable name after read, default assignment to reply)  123456 [email protected] opt]# echo $REPLY 123456

  ②, read from the script;

[email protected] opt]# lltotal1756-rw-r--r--.1Root root158May +  +: thehosts-rwxr-xr-x.1Root root1783106May -  -: $putty-0.62. tar.gz-rw-r--r--.1Root root3May +  +: thexx-rwxr-xr-x.1Root root -June5  +: thexx.sh[[email protected]alhost opt]# vim zz.sh[[email protected] opt]# cat zz.sh #!/bin/bashecho "qin shu ru ni de ming zi" Read nameecho "Huan Yin ni: $name"[email protected] opt]# chmod+x zz.sh [[email protected] opt]#./zz.sh qin shu ru ni de Ming Zimachinehuan Yin Ni:machine[[email protected] opt]#

You can also get the following wording:

[ email protected] opt]# vim zz.sh [[email protected] opt]# Cat zz.sh #!/bin/"qin shu ru ni De Ming zi" " " name (note the use of read-p, do not wrap the direct keyboard input). c12> "Huan Yin ni: $name"[[email protected] opt]#. /

Second, the use of arrays array

1, the definition of the array; xx= (AA bb cc DD)

2. View an element inside the array; echo ${xx[2]} (Note: The subscript is starting from 0)

3. View all the elements inside the array; echo ${xx[*]} or Echo ${xx[@]} * @

3. Check the volume label of the array (all subscript); echo ${!xx[*]} or Echo ${!xx[@]} !

4. View the number of elements inside the array; echo ${#xx [*]} or echo ${#xx [@]} #

Forexample:

  

//definition of an array[Email protected] opt]# mm=(AA bb cc dd) (no commas and semicolons between elements) //view an element in an array[Email protected] opt]# echo ${xx[0]}aa[[email protected] opt]# echo ${xx[3]}dd[[email protected] opt]# echo ${xx[4]} (array subscript is starting from 0) [email protected] opt]#//View all the elements inside the array (*/# all can)       [Email protected] opt]# echo ${xx[*]}AA bb cc dd[[email protected] opt]# echo ${XX[@]}AA bb cc dd[[email protected] opt]#//View all subscripts of an array (! ) [Email protected]host opt]# Echo ${!xx[*]}0 1 2 3[email protected] opt]# echo ${!xx[@]}0 1 2 3[email protected] opt]#//view the number of elements inside the array (#) [Email protected] opt]# echo ${#xx [*]}4[ [email protected] opt]# echo ${#xx [@]}4[email protected] opt]#

Shell programming two--read, array,

Related Article

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.