Shell-array usage

Source: Internet
Author: User

From: http://www.jb51.net/article/34323.htm

Note: The subscript of the array in Shell starts from 0 by default.

1. Place the string in an array to obtain its length.

Copy the Code as follows:
#! /Bin/bash
STR = "a B -- n d"
Array = ($ Str)
Length =$ {# array [@]}
Echo $ Length

For (I = 0; I <$ length; I ++ ))
Do
Echo $ {array [$ I]}
Done

 

Execution result:
[Oracle @ 99bill-as9 array] $ sh length. Sh
4
A

-- N
D

2) print the string:

Copy the Code as follows:
#! /Bin/bash
STR = "a B C"
For I in $ Str
Do
Echo $ I
Done
Or:
#! /Bin/bash
STR = "a B C"
Array = ($ Str)
For (I = 0; I <$ {# array [@]}; I ++ ))
Do
Echo $ {array [$ I]}
Done

Execution result:
A

C

2. When a string is separated by other characters

Copy the Code as follows:
#! /Bin/bash

Str2 = "A # B # C"
A = ($ (echo $ str2 | tr '# ''' | tr-S ''))
Length =$ {# A [@]}

For (I = 0; I <$ length; I ++ ))
Do
Echo $ {A [$ I]}
Done
# Echo $ {A [2]}

 

Execution result:
A

C

3. Other operations on Arrays

Copy the Code as follows:
#! /Bin/bash
STR = "a B -- N dd"
Array = ($ Str)
Length =$ {# array [@]}

# Ouput The first array element outputs the first element of the array.
Echo $ Array

# Use subscript way access array access to array elements using the following method
Echo $ {array [1]}

# Output the array to output this array
Echo $ {array [@]}

# Output in the array subscript for 3 the length of the element output the length of the element marked as 3 in the array
Echo $ {# array [3]}

# Output in the array subscript 1 to 3 element: Elements marked as 1 to 3 in the output Array
Echo $ {array [@]: 1: 3}

# Output in the array subscript greater than 2 elements output the element whose subscript is greater than 2
Echo $ {array [@]: 2}

# Output in the array subscript less than 2 elements output the element whose subscript is less than 2 in the array
Echo $ {array [@]: 2}

Execution result:
A

A B -- N dd
2
B -- N dd
-- N dd
A B

4. Access a string through traversal (separated by spaces by default. When strings are separated by other separators, refer to 2)

Copy the Code as follows:
#! /Bin/bash
STR = "A -- M"
For I in $ Str
Do
Echo $ I
Done

Execution result:
A
-- M

5. How to use echo to output a string STR = "-n". Because-N is a parameter of ECHO, the General echo "$ Str" method cannot be output.

The solution can be:

Copy the Code as follows:
Echo x $ STR | SED's/^ x //'
Echo-ne "$ STR \ n"
Echo-e "$ STR \ n \ c"
Printf "% s \ n" $ STR (this can also be done)

Detailed source reference: http://www.jb51.net/article/34323.htm

 

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.