Shell-07 with an array of strings

Shell-07 with an array of strings

Array

        Array that white is a period of continuous variables, a contiguous memory storage space

        Resolution: The problem of excessive variable; the variable of the same, we do not need to define multiple names, but in an array of ways to define; (list)

        Array name index

        How to define the array:

               declare -i is defined integer (statement)

               declare -a definition array array

               nested arrays defined declare -A

                      Classes were good cloud [1] = cloud [1] = the cloud three classes [1] = XX

                      [Such a data set of values, not commonly used in bash]

        [Python and shell]               

array=1

              How to array assignment:

                     Array=()

  1. array [0] = A (in computer languages, typically the index starts from 0; index numbers are not necessarily: key-value array [A] = 1; shell in the array assignment may hop - hop assignment)
  2. array = (/ var / log / *. log) ----- of each file corresponds to an array value

array=(a b c d)

  1. array=(‘a=187’c=’188’b=’170’)
  2. Assignments via the read command

DESCRIPTION specified delay -p read -t 1 -a variable desired value input array

read -a a b c d e

              How the transfer of functions:

                     } $ {Variable name

                     {$ Array name [index]} which index is defined, it is to see the value of the index corresponding to the specified

                     $ {Name} default calling array index = value of $ array 0 (best not to write)

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

       Exercise:

                     /Var/log/*.log statistics the number of rows, the index is an even number of files and

                      wc -l /var/log/*.log

                      a=(/var/log/*.log)

                      declare -I Line=0

                      declare -I sum=0

                      sum=0

                            for i in {0..7};do

                                   if [ $[$i % 2 ]-eq 0 ]

                                          line=`wc -l  ${a[$i]} | cut -d “ ” -f1`

                                          sum=sum+$line

                                   be

                            done

                            echo $sum

 

Call the array array length:

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

Add special way an array of values:

         $ {Variable name [$ {# variable name [@]}]} = "value" variable or $ {name [$ {# variable name [*]}]} = "value"

About an array of other operations:

Slice array: $ {variable name [@]: Offset: number of values ​​that need to remove}

a=(/var/log/*.log)

                                   Echo $ {a [*]: 2: 4}

                          $ {Variable name [@]: Offset}

To delete an array of values: unset $ {variable name [index]}

                          Delete is the value of the index, with the index is not deleted;

                          [Know the value, how to view the index? ]

Modify the value: coverage that is modified

                          a[3]=c  a[3]=2

Exercise:

 

  1. 10 generates a random number, wherein the value of the maximum printing

#!/bin/bash

declare -a a

declare -i max=0

 

for i in `seq 0 9`;do

        a[$i]=$RANDOM

        [ ${a[$i]} -gt $max ]&&max=${a[$i]}

done

echo $ {a [@]}

echo $max  

  1. And sorting randomly generated dozens
  2.  

 

String:

       printf --- formatted output

       name=zqq high=180 

python print(%s,%d)%(name,high)

Placeholder ----% s% d% f

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

% - [num] s designated Left

 

Represents a left alignment% -10s

% 20d 20 characters aligned to the right

After a representative of decimal places% -10.1f

printf "%-10s %-10s %-10s\n" No Name Height

printf "%-10s %-10s %-10d\n" 1 zqq 180

printf "%-10s %-10s %-10d\n" 2 qqq 190

printf "%-10s %-10s %-10f\n" 3 www 188.11

String sections:

       Slice Array: $ {variable name []: Offset: number of values}

       Slice string: $ {variable name: Offset: number of values} - Tangent

                              $ {Variable name: - number of values} - fanqie spaces after the colon to

                              $ {Variable name: -word} - default assignment of meaning

                                   read "sasasa" test

                                   [ -z $test ] && test=123

Take substrings based on the mode:

       From right to left

       $ {Test # pattern} delete pattern matching the first string to be

       $ {Test ## pattern} match the pattern to remove all the strings

       [root@localhost ~]# a=123qwe123wsx

[Root @ localhost ~] # b = $ {a # * 1} -------- removed 1

[root@localhost ~]# echo $b

23qwe123wsx

[root@localhost ~]# a=/var/log/anaconda/anaconda.log/16.sh

[root@localhost ~]# b=${a##*/}

[root@localhost ~]# echo $b

16.sh

       [root@localhost ~]# a=/var/log/anaconda/anaconda.log/16.sh

[root@localhost ~]# b=${a#*/}

[root@localhost ~]# echo $b

var / log / anaconda / anaconda.log / 16.sh

[root@localhost ~]# url=http://192.168.72.100:8080

[root@localhost ~]# b=${url##*:}

[root@localhost ~]# echo $b

8080

From left to right

$ {Variable name% pattern}

$ {Variable name %% pattern}

[root@localhost ~]# url=http://192.168.72.100:8080

[root@localhost ~]# echo ${url%:*}

http://192.168.72.100

[root@localhost ~]# echo ${url%%:*}

http

[root@localhost ~]# url=http://192.168.72.100:8080

[root@localhost ~]# url1=${url#*//}

[root@localhost ~]# url2=${url1%:*}

[root@localhost ~]# echo $url2

192.168.72.100

Character replacement:

       $ {String variable name / pattern / replacement}

       The first pattern matching is not replaced by a regular expression pattern [,] is a wildcard file

       [root@localhost ~]# url=http://192.168.72.100:8080

[root@localhost ~]# echo ${url/192.168/10.6}

http://10.6.72.100:8080

[root@localhost ~]# url=http://192.168.72.100:8080

[root@localhost ~]# echo ${url/[1-9]*.*.*.*[0-9]/www.baidu.com}

http://www.baidu.com

$ {Variable name // pattern / replacement string}

All matches of pattern, and replace

$ {String variable name / # pattern / replacement}

Specifies the pattern must be the first line

[root@localhost ~]# a=`cat /etc/passwd|grep root`

[root@localhost ~]# echo $a

root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost ~]# echo ${a/#root/rooter}

rooter:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost ~]# echo ${a/#r??t/rooter}

rooter:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost ~]# echo ${a/\/bin\/bash//sbin\/nologin}

root:x:0:0:root:/root:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin

$ {String variable name /% pattern / replacement}

Specifies the end of the line must be replaced before the pattern

[root@localhost ~]# a=`cat /etc/passwd|grep root`

[root@localhost ~]# echo ${a/%\/?bin\/*/aaa}

root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:aaa

[root@localhost ~]# echo $a

root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin

/ Separator, not replaced (#% other special characters, and different sed)

Find and delete

       Replacement, the replacement string is not specified, shall be deleted

       $ {Variable name / pattern}

       [root@localhost ~]# a=`cat /etc/passwd|grep root`

[root@localhost ~]# echo $a

root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost ~]# echo ${a/%operator*n/}

root:x:0:0:root:/root:/bin/bash

$ {Variable name // pattern}

$ {Variable name / # pattern}

$ {Variable name /% pattern}

Replace the case:

       tr replacement case

       tr [[:lower:]] [[:upper:]] < filename

       $ {Variable name} --- ^^ replace capital

       {,,} $ ---- replaced variable name lowercase

tr [:lower:] [:upper:] < /etc/passwd

       [root@localhost ~]# a=$(cat /etc/passwd | head -1)

[root@localhost ~]# echo $a

root:x:0:0:root:/root:/bin/bash

[root@localhost ~]# echo ${a^^}

ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH

[root@localhost ~]# b=${a^^}

[root@localhost ~]# echo $b

ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH

[root@localhost ~]# echo ${b,,}

root:x:0:0:root:/root:/bin/bash

Variable assignment:

       1 variable name = $ {variable name 2: -word} There is no space! ! !

       Enter a default value of the action, not the variable 2 values, the default value assigned to a variable word 1

       [root@localhost ~]# echo $b

ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH

[root@localhost ~]# c=${b:-qaz}

[root@localhost ~]# echo $b

ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH

[root@localhost ~]# echo $c

ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH

[root@localhost ~]# e=${f:-qaz}

[root@localhost ~]# echo $f

      

[root@localhost ~]# echo $e

gas

       1 $ {variable name = variable name 2: + word} unusual

       2 In the case of variable value, only the word default value assigned to the variable 1

       [root@localhost ~]# h=1

[root@localhost ~]# echo $h

1

[root@localhost ~]# g=${h:+qqq}

[root@localhost ~]# echo $g

qqq

[root@localhost ~]# echo $h

1

$ {Variable 2: = word}

       If the variable has no value, it will be assigned to the word variable has a value, the original value of the use

1 variable name = $ {variable 2:? Error_info}

       If the variable value is not 2, the output error error_infor

Script references how other variables in the file to achieve?

       filename1

       vim filename1

              A=1

              B=2

       :wq

       vim filename1.sh

              ./ full path / filename which source / full path / filename

              C=$[$a+$b]

              Echo $ c

bash filename1.sh

Exercise:

       Modified hostname, hastname define a file name = xxx, hostname.sh

 

Two commands:

       mktemp to create temporary files and directories temp-- / tmp

                      You get to avoid duplicate file names; mktemp name .XXXXXX (XX represents the random numbers, random letters and numbers)

                            The default is to create a random file

                     -d is randomly created directory

                     File=`mktemp a.XXX`

       install copy and delete files use the same cp

                     Pros: You can specify permissions to copy files

                     Usage: intall a directory file b b // copied a file to the directory, and restores the initial permissions (file directory 755 644) -t -d source project to create a directory on the exchange -m specify permissions (mask) -o -g group specified belongs Specifies the owning group

 

Exercise:

 

Ldd View all library programs

Create a directory

       Chroot switching system root

       /--/dev/sda1

       The original operating system shell program switches to a new directory to 1, and then place the appropriate boot program file required in this directory.

 

How to remove the full path to the command

How to Read libraries --- ldd

How to copy read out by ldd library files to the target directory

       No matter how must overwrite

Extended:

              Start principle of the system: virtualization ready knowledge

                     Reading system - read the disk: in advance that the driver must be loaded disk

                     Mount the virtual root (img virtual root file ramdisk)

                     After the virtual root file hang in grub1.5 load the disk drive;

By chroot temporary "root" switch to the ramdisk above upper disk

              Operating System: vmlinuz kernel, ramdisk.img

 

 

 

Guess you like

Origin www.cnblogs.com/KAJIA1/p/11392374.html