Shell Foundation V: Array Preliminary

Source: Internet
Author: User

First, an array overview

Arrays: Data Structures

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

Declaring an array:declare-e arrayname

Initialization assignment:arrayname= ("STRING1" "STRING2" ... )

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

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

get 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


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


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

3. Select random bits from all the students

Shell Foundation V: 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.