Shell Programming-Basic syntax

Source: Internet
Author: User
Tags array length

Tag: element represents article string output shard for form ber

The role of variables

Forbidden spaces on both sides of the equals sign

For loop
#语法一 for  variable in value 1 value 2 value 3     : Do       block done     # #语法二 the for  variable ' command ' do     block done    # #语法三 for  (initial value; loop control ; Variable) do      program block done      

e.g. Output 3 times Uptime

#!/bin/bash for    i in 1 2 3 4 5     does        echo "$i-->$ (uptime)"    done  

e.g. bulk unzip all. tar.gz files under the current folder

#!/bin/bash for    i in ' ls./*.tar.gz '      does         tar-zxvf $i >/dev/null     Done  

e.g. seeking 1~100 and

#!/bin/bash    #注意变量赋值的时候, = There is absolutely no space on either side  sum=0 for  ((i=1; i<=100; i++)) do       sum=$ (($sum + $i)    ) Done    echo "1+2+3+...+100= $sum"

Array
    • Definition of an array

 The attribute of an array is a set of identical data types

Let's look at two types of data: One is a numeric type, the other is a string type, and although the shell itself is a weak type, it can be so distinguished.

  array of numeric types: A pair of parentheses represents an array, and the elements in the array are separated by a "space".

As an example:

Arr_number= (1 2 3 4 5);

  array of string types: Similarly, use a pair of parentheses to represent the array, where the elements in the array are enclosed in double or single quotation marks, and also with "spaces."

arr_string= ("abc" "EDF" "SSS"); or arr_string= (' abc ' EDF ' SSS ');

    • Operation of the array

We use the numeric type array arr_number= (1 2 3 4 5) as the source array for the relevant explanation: Get the array length, read the value of a subscript, assign a value to a subscript, delete, assign and replace, and traverse.

The shell looks to get the value of a variable, starting with the $ character, such as: $a or ${a}.

  Get array length

arr_length=${#arr_number [*]} or ${#arr_number [@]}, i.e. form: ${#数组名 [@/*]} to get the length of the array.

  Reads a value of a subscript

ARR_INDEX2=${ARR_NUMBER[2]}, i.e. form: ${array name [subscript]}

  Assign a value to a subscript

Here are two questions to ask:

    The first question is , what happens if the subscript element already exists?

    A : the underlying value is modified to be the new specified value.

For example: arr_number[2]=100, the array is modified to (1 2 100 4 5)

    The second question is , if the specified subscript already exceeds the size of the current array, such as the size of the arr_number above is 5, specify an subscript of 10 or 11 or greater than 5 of any value?

    A : the newly assigned value is appended to the end of the array.

For example: arr_number[13]=13, the array is modified to (1 2 100 4 5 13)

  Delete operation

Clear an element: unset arr_number[1], this clears the array labeled 1;

Empties the entire array: unset arr_number;

  Shard Access

The Shard Access form is: ${array name [@ or *]: start subscript: End subscript}, note that the value of the end subscript element is not included.

For example: ${arr_number[@]:1:4}, where Shard Access starts with subscript 1 and the number of elements is 4.

  Mode substitution

Form: ${array name [@ or *]/mode/new Value}

For example: ${arr_number[@]/2/98}

  Traversal of an array

Array traversal We use the FOR statement to demonstrate:

For V in ${arr_number[@]}; Do

Echo $v;

Done

Shell Programming-Basic syntax

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.