"Shell Basics" 11, array preliminary

Source: Internet
Author: User

One, array

array: is a data structure,

data series, continuous multiple data, can be used to get related elements of the index

Declaring an array:

Declare-a arrayname # can not be declared in advance

Initialization assignment:

arrayname= ("STRING1" "STRING2" ... ) #使用空格隔开

Arrayname= ("STRING1" [4]= "STRING2" ...)

arrayname[0]= "STRING1" arryname[3]= "STRING3"

gets all the elements in the array: ${arraryname[@],${arrayname[*]}

Gets the total number of valid elements in the array:${#ARRAYNAME [@]},${#ARRAYNAME [*]}

Gets the length of the string in an element:${#ARRAYNAME [INDEX]} (index=0,1,2,3 ...)


Cases:

[[email protected] shell]# echo ${color[0]}red[[email protected] shell]#  echo ${color[1]}bule[[email protected] shell]# echo ${color[2]}gree[[email  protected] shell]# echo  $color [2] red[2][[email protected] shell]# echo   $color           #数组不指定引用其第几个元素默认是引用第一个元素red [[email  protected] ~]# x= (nihao  "s b"  [4]= ' Ruozhi ') [[Email protected] ~]# echo  ${x[5]}                [ [Email protected] ~]# echo ${x[2]}[[email protected] ~]# echo ${x[1]}s  b[[email protected] ~]# echo ${x[4]}ruozhi[[email protected] ~]# x= ( 333 22 4444 555555555) [[email protected] ~]# echo ${#x [1]}2[[email  Protected] ~]# echo ${#x [3]}9 


Two, bash pseudo-random number generator

$RANDOM

[[email protected] ~]# echo $RANDOM 4852[[email protected] ~]# echo $RANDOM 3161[[email protected] ~]# echo $RANDOM 23935[[em AIL protected] ~]# echo $[random%40]26[[email protected] ~]# echo $[random%40]21[[email protected] ~]# echo $[random%40]16


Practice:

1. Print 99 multiplication table

[[Email protected] shell]# cat 12.sh#!/bin/bashn= (' 1 '   ' 2 '   ' 3 '   ' 4 '   ' 5 '   ' 6 '   ' 7 '   ' 8 '   ' 9 ') for x in $ (seq 0 8);d o     for y in $ (seq 0  $x);d o        echo -e  -n  "${n[y]}x${n[x]}=$[${n[x]}*${n[y]}]\t"         done    echodone[[email protected] shell]# bash 12.sh1x1=11x2=2   2x2= 41x3=3   2x3=6   3x3=91x4=4   2x4=8   3x4=12   4x4=161x5=5   2x5=10  3x5=15  4x5=20  5x5=251x6=6    2x6=12  3x6=18  4x6=24  5x6=30  6x6=361x7=7    2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=491x8=8    2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=641x9= 9   2x9=18  3x9=27  4x9=36  5x9=45  6x9=54   7x9=63  8x9=72  9x9=81

2. Generate N random numbers (n>5) by script, sort them from small to large (bubble sort)


3. Select random bits from all the students

Three, string processing

1. String slicing

${#var} :

Returns the length of the string variable var

${var:offset} :

returns the string variable var , starting with the character after the first offset (excluding the first offset character), to the last part, the value of offset 0 to ${#var}-1

${var:-lengh}:

Take the rightmost few characters of a string, using spaces or parentheses

${var:offset:number}:

Returns the string variable var, starting with the character specifier (excluding the first offset character) from the first, the part of the length number

${var:offset:-lengh}:

Skips the offset character from the leftmost side until the rightmost lengh character of the string (the middle of the end of the header isnot supported until bash4.2 )

Intercept the Hello string: # var= ' Hello world! ' # echo ${var:0:5}hello Intercept World string: # echo ${var:5:-1}world Intercept last character: # echo ${var: ( -1)} #使用空格隔开也可以!

${#var}:

[Email protected] ~]# name= "Xie June" [[email protected] ~]# arr1= (str1 str22 str333) [[email protected] ~]# echo $namexie J  Un[[email protected] ~]# echo $arr 1str1[[email protected] ~]# echo $ #name0name [[email protected] ~]# echo ${#name}7[[email Protected] ~]# echo ${#arr1} #数组返回的是第一个元素的长度4

${var:offset} :

[[email protected] ~]# echo ${name:0} #从0开始xie jun[[email protected] ~]# echo ${name:1}ie jun[[email protected] ~]# Ech o ${name:5} #空格也算一个字符un [[email protected] ~]# echo ${name:-5}xie jun[[email protected] ~]# echo ${name:-5} #使用负值要使用空格, A negative value indicates a right-to-left number e jun[[email protected] ~]# echo ${arr1:1}tr1[[email protected] ~]# echo ${arr1[1]:1}tr22

${var:offset:number}:

[[email protected] ~]# echo ${name:1:1}i[[email protected] ~]# echo ${name:3:3}ju[[email protected] ~]# echo ${name:3:10}j  Un[[email protected] ~]# echo ${name:10:10}[[email protected] ~]# echo ${name:-1:10} #没使用空格, invalid Xie Jun[[email protected] ~]# echo ${name: -1:10}n[[email protected] ~]# echo ${name: -5:10}e June

2. String interception

${var#*word}: where word can be any of the specified characters

Function:

from left to right, find the var variable stored in the string, the first occurrence of Word, delete all characters from the beginning of the string to the first occurrence of Word characters

${var##*word}: Ibid.,

The difference is that all content (that is, greedy mode deletion) is removed from the beginning of the string to the last character specified by Word .

${var%word*}: where word can be any of the specified characters;

Function:

from right to left, find the var variable stored in the string, the first occurrence of word, delete the last character of the string to the left to the first occurrence of all characters between word characters;

${var%%word*}: Ibid.,

Just Delete all characters from the rightmost character of the string to the left to the last occurrence of Word characters (greedy mode);

Attention:

wildcard characters are supported and regular expressions are not supported

[Email protected] ~]# str= "I very Very love Linux" [[E-mail protected] ~]# echo ${str#*e}ry very love Linux[[email protect ED] ~]# echo ${str##*e}linux[[email protected] ~]# echo ${str%e*}i very very lov[[email protected] ~]# echo ${str%%e*}i v

3. String Lookup substitution

${var/pattern/substi}:

Finds the string in the string represented by Var, the first time it is matched to by pattern, and replaces it with Substi

${var//pattern/substi}:

Finds the string represented by Var, all strings that can be matched to by pattern, replaced with substi (global substitution)

${var/#pattern/substi}

Finds the string in the string represented by Var, in which the beginning of the line is matched by the pattern, to replace the Substi

${var/%pattern/substi}

Finds the string in the string represented by Var, where the end of the line is matched by the pattern, to replace the Substi

${var/pattern[/]}: finds the string represented by Var, deleting the string to which the first pattern matches

Attention:

wildcard characters are supported and regular expressions are not supported

[[email protected] ~]# echo ${str/e.y/sb}       #不支持正则表达式i  very very love linux[[email protected] ~]# echo ${str/e?y/sb}        #支持通配符i  vsb very love linux[[email protected] ~]#  echo ${str/e[a-z]y/sb}i vsb very love linux[[email protected] ~]#  echo ${str//e?y/sb}i vsb vsb love linux[[email protected] ~]#  echo ${str/#i/sb}sb very very love linux[[email protected] ~]# echo  ${str/%i/sb}i very very love linux[[email protected] ~]# echo ${ str/%ux/sb}i very very love linux[[email protected] ~]# echo ${str/%u/ Sb}i very very love linux[[email protected] ~]# echo ${str/%x/sb}i  very very love linux[[email protected] ~]# echo ${str/%?/sb}i very very love  linuxsb# Find Delete (replace with empty) [[email protected] ~]# str= "I very very love linux" [[ email protected] ~]# echo ${str/%linux}i very very love[[email  protected] ~]# echo ${str/%linux/}i very very love[[email protected] ~ ]# echo ${str/i/}very very love linux[[email protected] ~]# echo  ${str/i v/}ery very love linux[[email protected] ~]# echo ${str/i  v}ery very love linux[[email protected] ~]# echo ${str/v}i ery  very love linux[[email protected] ~]# echo ${str//v}i ery ery  loe linux

4. Character Case Conversion

${var^^}: convert all lowercase letters in VAR to uppercase

${var,,}: convert all uppercase letters in Var to lowercase

[[email protected] ~]# echo ${str^^}i VERY VERY love Linux[[email protected] ~]# echo ${str,,}i VERY VERY Love LINUX

5. Assigning values to variables

${var:-value}: returns value if Var is empty or not set, otherwise returns VAR

${var:+value}: returns value if Var is not empty, otherwise returns VAR as a null value

${var:=value}: if Var is empty or not set, return value and assign value to Var; otherwise, return the value of Var

${var:?error_info}: if Var is empty or not set, the Error_info is printed at the current terminal, otherwise the value of VAR is returned

[[email protected] ~]# echo  $stri  very very love linux[[email  protected] ~]# echo  $XJ [[email protected] ~]# echo ${str:-xiejun}i  Very very love linux[[email protected] ~]# echo ${xj:-xiejun}xiejun[[email  protected] ~]# echo ${str:xiejun}i very very love linux[[email  PROTECTED] ~]# ECHO ${STR:+XIEJUN}XIEJUN[[EMAIL PROTECTED] ~]# ECHO ${XJ: Xiejun}[[email protected] ~]# echo ${xj:+xiejun}[[email protected] ~]# echo  ${str:=xiejun}i very very love linux[[email protected] ~]# echo  ${xj:=xiejun}xiejun[[email protected] ~]# echo  $XJJ [[email protected] ~]#  echo ${xjj:?xiejun}-bash: xjj: xiejun[[email protected] ~]# echo  $XJJ


"Shell Basics" 11, array preliminary

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.