shell script day08- arrays and strings

Array: a contiguous variable, a contiguous memory storage space; too many variables to solve the problem with the use of a class variable, do not need to define a plurality of outside variables, but in a manner to define an array.

  1. array name

  2. Index

  3. Value

First, how to define the array:

  declare -l defined integer (statement)

  declare -a definition array array

  nested arrays defined declare -A

Second, how to array assignment

  array=0

  1.array [0] = 1 (in a computer language, the index is generally calculated from the start; index numbers are not necessarily: key-valuearray [jack] = 3m; shell in the array can be assigned hopping)

  2.array=(/var/log/*log)

    Do traversal

    array=(a c n e)

  3.array= ('jack=3m''rose=2m'...)

  4. be assigned by the read command

    read -t delay "Specify Description" variable desired value input array -a -p

    read -a a b c d e

Third, how to call value

  } $ {Variable name

  {$ Array name [index]} it is defined that the index value is seen index corresponding

  $ {Name} called by default array index value = $ array 0 is

  {$ Array name [*]} or {$ array name [@]} All values ​​in the array call

    

a=(/var/log/*.log)
for i in{0...7};do
    if [ $[$i/2] -eq 0]
        line='wc -l ${a[$i]} | cat -d ' '-f1'
        sum+=$line
done
echo $sum

  1. Call the array length of an array

  lenth { "variable"}

  Variable $ {# [*]} or $ [variable # [@]]

  2. The method of adding an array of special value

  $ [Variable name [$ {variable name [@]}] = 'value']

Fifth, on an array of other operations:

  Array slice

  $ {Variable name [@]: Offset: the number of values ​​required to remove}

   Delete unset $ {variable name [index]}

    Note: deleting a value corresponding to the index

Six String

  printf

  name=jack

  heigth=180

  python ($ s, height of% d)% (name, heigth)

    %s

    %d

    %f

    % [Num] s that specify the width of the string placeholder

    % - [num] s behalf of Left

1. string sections

  Slice Array: $ [variable @] [: Offset: number of values]

  String: $ [Variable name: Offset: number of values]

      $ [Variable name: - Number of values] [Note: Be sure to add a space after the colon]

        $ [Variable name: word] // default assignment

        read"sasas":test

        [-z $test]&&test=abc

2. Take the string based on the mode

  From left to right

Guess you like

Origin www.cnblogs.com/ljx1/p/11391174.html