Array learning in shell

Source: Internet
Author: User

Because the application of shell is not too long and has never touched on arrays in actual projects, we need to convert something like a = 1, 2, 4, and so on today. We used awk to retrieve them separately. So today I want to see if there is a better and more concise method-just think of arrays.

First solve the problem in the example, such

   my_num="one,two,three,four"

Split it:

OLD_IFS="$IFS" IFS="," arr=($my_num) IFS="$OLD_IFS" for s in ${arr[@]} do     echo "$s" done

Output:

onetwothreefour


The following is a detailed knowledge about Arrays:

I. arrays and strings

Array [3] = "a B c" echo $ Array [1] Output B,

Str = "a B c" Array = ($ str) echo $ Array [1]. Similarly, output B. Note that brackets of the right value in Array = ($ str) cannot be missing.

The above is what we often call putting strings in an array. It can also be understood as a dynamic array, which is much simpler than C and C ++.

You can use the following three methods to calculate the string length:

Echo "$ str" | awk '{print length ($0 )}'

Expr length "$ str"

Echo "$ str" | wc-c

However, if the value obtained in the third method is 1 more, the terminator may be included.

There are three methods to judge whether a string is null:
If ["$ str" = ""]
If [x "$ str" = x]
If [-z "$ str"]
2. Other types of Arrays
Arr = (123 34 3 5) echo $ arr, output 123, equivalent to echo $ {arr [0]} here;
Echo $ {arr [1]}, output 34. Note the format. Except for the first address, {} must be added; otherwise, an error will occur.
3. Length and traversal of Arrays
Length = $ {# array [@]} ---- length of the array. The length here is the maximum subscript.
Echo $ {array [@]} ------- output all elements of the array
Array [1] = 5 -------------- assigning values to an element of the array is the same as the syntax of Standard c.
Echo $ {array [@]: 1: 2} --- output values of array [0] and array [1]
Echo $ {array [@]: 2} ----- output the value after the third element of the array
Echo $ {array [@]: 2} ----- the output array subscript is less than 2
$ Echo $ {# array [3]} ---- get the length of element 3
Unset array -------------- clear array
Array = ------------------ clear the array and assign the array Null Value

Method 1 for Traversing strings
For (I = 0; I <$ {# array [@]}; I ++ ))
Do
Echo $ {array [$ I]}
Done

Method 2
Str = "a -- m"
For I in $ str
Do
Echo $ I
Done

Traverse with while
Len =$ {# arr [@]}
I = 0
While [$ I-lt $ len]
Do
Echo $ {arr [$ I]}
Let I ++
Done

4. Others:
Array [3] = "a B c" and Array [3] = a B c are equivalent. If Array [3] = "a" bc "", to search for ", awk cannot be found, single quotes
Yes, that is, double quotation marks cannot be found. We can use the string truncation method to determine whether double quotation marks are available. The string truncation method is as follows:
$ {Varible # * string} captures the string after the last string from left to right.
$ {Varible # * string} captures the string after the first string from left to right.
$ {Varible % string *} captures the string after the last string from the right to the left
$ {Varible % string *} captures the string after the first string from the right to the left.
Replace the preceding string with ", and then judge the length of the string. If it is equal, it means no. If it is not equal, it means double quotation marks.
The method of changing the array separator is invalid. This double quotation mark is a built-in symbol of shell.

Common awk search methods, such:
Find = 'echo {$ {arr [$ I]} | awk' BEGIN {FS = ""} {for (I = 1; I <= NF; I ++) if ($ I = ";") print $ I }'';
You can also use the math and index methods, such as: flag = 'echo {$ line} | awk '{print match ($0, "channel ")}''
The default delimiter of the array is space. To change the default delimiter, use the following method:
Str = "abd # ddd # ff"; str2 = ($ (echo $ str | tr ''#'' | tr-S ''));


Reference: http://www.cppblog.com/zhangyq/archive/2010/10/08/126537.aspx? Opt = admin

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.