Thank you---------Linux Bash programming

Source: Internet
Author: User
Tags array definition rand

Array

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/A5/27/wKioL1m4vYHidlQVAAA4SIobYQM360.png "title=" Qq20170913130915.png "alt=" Wkiol1m4vyhidlqvaaa4siobyqm360.png "/>


Array name

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A5/27/wKioL1m4vcjgjKBsAAAWDCiEdu0044.png "title=" Qq20170913131032.png "alt=" Wkiol1m4vcjgjkbsaaawdciedu0044.png "/>


Array definition

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/A5/27/wKioL1m4vnmhNRQYAAAqYzc8dBI109.png "title=" Qq20170913131327.png "alt=" Wkiol1m4vnmhnrqyaaaqyzc8dbi109.png "/>


Array reference

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/A5/27/wKioL1m4vumy5DHGAAAOQaM2cH0019.png "style=" float : none; "title=" Qq20170913131516.png "alt=" Wkiol1m4vumy5dhgaaaoqam2ch0019.png "/>

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/06/77/wKiom1m4vxKTZ3QWAAAPXO4WFLc688.png "title=" Qq20170913131458.png "style=" Float:none; "alt=" Wkiom1m4vxktz3qwaaapxo4wflc688.png "/>


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A5/27/wKioL1m4vyDzyJvxAAAQZnkOBjA629.png "style=" float : none; "title=" Qq20170913131550.png "alt=" Wkiol1m4vydzyjvxaaaqznkobja629.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/06/77/wKiom1m4v3DwIzhOAAANVLkGOoU152.png "title=" Qq20170913131654.png "alt=" Wkiom1m4v3dwizhoaaanvlkgoou152.png "/>


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/06/77/wKiom1m4v0qDay9NAAAPWdRj_XQ262.png "style=" float : none; "title=" Qq20170913131555.png "alt=" Wkiom1m4v0qday9naaapwdrj_xq262.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/A5/27/wKioL1m4v3vg-pkTAAAQa-QshRk862.png "title=" Qq20170913131747.png "alt=" Wkiol1m4v3vg-pktaaaqa-qshrk862.png "/>


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A5/27/wKioL1m4vyGhOz6GAAAe0UWV24Q660.png "style=" float : none; "title=" Qq20170913131602.png "alt=" Wkiol1m4vyghoz6gaaae0uwv24q660.png "/>


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/06/77/wKiom1m4v0rDJhgbAAAZhuWIT8E599.png "style=" float : none; "title=" Qq20170913131612.png "alt=" Wkiom1m4v0rdjhgbaaazhuwit8e599.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/06/77/wKiom1m4wAzSYDjhAAAU2PjbwvQ807.png "style=" float : none; "title=" Qq20170913131923.png "alt=" Wkiom1m4wazsydjhaaau2pjbwvq807.png "/>


How array elements are assigned

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A5/27/wKioL1m4wa-C9uAVAAATBtnM1ZU581.png "style=" float : none; "title=" Qq20170913132633.png "alt=" Wkiol1m4wa-c9uavaaatbtnm1zu581.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/A5/27/wKioL1m4wa_CHY3zAAAqTlLmvZw375.png "style=" float : none; "title=" Qq20170913132642.png "alt=" Wkiol1m4wa_chy3zaaaqtllmvzw375.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/06/77/wKiom1m4wdjyfqtEAAAslo_Y7L0656.png "style=" float : none; "title=" Qq20170913132650.png "alt=" Wkiom1m4wdjyfqteaaaslo_y7l0656.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A5/27/wKioL1m4wfzjqZjkAAAzRhbbzH0252.png "title=" Qq20170913132823.png "alt=" Wkiol1m4wfzjqzjkaaazrhbbzh0252.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/A5/27/wKioL1m4wbCweTH1AAAbLLzd3R0606.png "style=" float : none; "title=" Qq20170913132705.png "alt=" Wkiol1m4wbcweth1aaabllzd3r0606.png "/>


Destroying an array

Unset Array_name[index]


Exercise: Generate 10 random numbers to save in an array, take out the maximum and minimum values

Exercise: Generate 10 random numbers in an array, and find the sum of all random numbers labeled even

Exercise: Write a script to define an array in which the elements in the array are all filenames ending in. Log in the/var/log directory, and the sum of the number of rows in the file to be labeled even

Exercise: Generate 10 random numbers, sort them

Example one: Generate 10 random numbers saved in the array, take out the maximum and minimum values #!/bin/bash#declare-a randdeclare-i max=declare-i min=for i in {0..9}; dorand[$i]= $RANDOMecho ${rand[$i]}if [$i-eq 0]; thenmax=${rand[$i]}min= $maxfiif [$max-lt ${rand[$i]}]; thenmax=${rand[$i]}fiif [$min-gt ${rand[$i]}]; thenmin=${rand[$i]}fidoneecho "Max: $max" echo "min: $min"
Example one: Generate 10 random numbers saved in an array, and the sum of all random numbers labeled as even is #!/bin/bash#declare-a randdeclare-i sum=0for i in {0..9}; dorand[$i]= $RANDOMecho ${rand[$i]}if [$[$i%2]-eq 0]; Thenlet sum+=${rand[$i]}fidoneecho "sum: $sum"
Exercise: Write a script to define an array in which the elements in the array are all file names ending with. Log in the/var/log directory, and the sum of the number of rows in the file whose subscript is even is counted #!/bin/bash#declare-a file_namefile_name= (/var  /log/*.log) # # array_name= ("Val1" "Val2" "Val3" ...) only a space in Val is necessary to write quotes Declare-i sum=0for i in $ (seq 0 ${#file_name [@]}); Dolet i-=1if [$[$i%2]-eq 0]; thensum=$[sum+$ (Wc-l < ${file_name[$i]})]fidoneecho $sum
Exercise: Generate 10 random numbers, sort them #!/bin/bash#declare-a randdeclare-i i=0while [$i-le 9]; dorand[$i]= $RANDOMlet i++doneecho "${rand[@]}" | Xargs-n 1 | Sort-n-R | Xargs


Character slices

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/A5/28/wKioL1m4x8_QZFC-AAAykzgA-OU765.png "title=" Qq20170913135311.png "alt=" Wkiol1m4x8_qzfc-aaaykzga-ou765.png "/>


To take a substring based on a pattern

From left to right #*string ##*string

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/06/77/wKiom1m4yGDQOYI0AAAItJ2J33I333.png "style=" float : none; "title=" Qq20170913135454.png "alt=" Wkiom1m4ygdqoyi0aaaitj2j33i333.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/06/77/wKiom1m4yQ_R6cpSAAAK_sgg-d0294.png "style=" float : none; "title=" Qq20170913135742.png "alt=" Wkiom1m4yq_r6cpsaaak_sgg-d0294.png "/>

From right to left%string*%%string*

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A5/28/wKioL1m4yOaTYriLAAALQJV7H6A515.png "style=" float : none; "title=" Qq20170913135749.png "alt=" Wkiol1m4yoatyrilaaalqjv7h6a515.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/A5/28/wKioL1m4yLCDVPYhAAAIw5cRuzs618.png "style=" float : none; "title=" Qq20170913135656.png "alt=" Wkiol1m4ylcdvpyhaaaiw5cruzs618.png "/>


Find replacement <[line header, tail] characters >

Replace with character, from left to right

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/06/78/wKiom1m4yxWRepCCAABAR2yYi_Q163.png "title=" Qq20170913140634.png "alt=" Wkiom1m4yxwrepccaabar2yyi_q163.png "/>

Replace with an empty

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/06/78/wKiom1m4yzjRR-SbAAAG545z9jg858.png "title=" Qq20170913140711.png "alt=" Wkiom1m4yzjrr-sbaaag545z9jg858.png "/>


Using the example

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/06/78/wKiom1m4zMWRAZEiAABZ8xr3ANA360.png "title=" Qq20170913141344.png "alt=" Wkiom1m4zmwrazeiaabz8xr3ana360.png "/>



character-Case conversions equivalent to TR ' A-Z ' A-Z

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A5/28/wKioL1m4zLii-OkrAAANM5mFJOc703.png "title=" Qq20170913141415.png "alt=" Wkiol1m4zlii-okraaanm5mfjoc703.png "/>


Using the example

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/A5/29/wKioL1m4zS6jlfgrAAAOCVz637I327.png "title=" Qq20170913141612.png "alt=" Wkiol1m4zs6jlfgraaaocvz637i327.png "/>


variable Default value

Not present, assignment

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/06/78/wKiom1m4zsjBtggNAAAtc-V3JMQ795.png "style=" float : none; "title=" Qq20170913142202.png "alt=" Wkiom1m4zsjbtggnaaatc-v3jmq795.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A5/29/wKioL1m4zp-R4IVCAAAplI9fupQ453.png "style=" float : none; "title=" Qq20170913142215.png "alt=" Wkiol1m4zp-r4ivcaaapli9fupq453.png "/>

Is present, the value is assigned

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/A5/29/wKioL1m4zvmD51p2AAAk60EyLIk592.png "title=" Qq20170913142352.png "alt=" Wkiol1m4zvmd51p2aaak60eylik592.png "/>

There is no error, there is a return var

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/06/79/wKiom1m4z5-i_nYBAAAN_ioENH0579.png "title=" Qq20170913142555.png "alt=" Wkiom1m4z5-i_nybaaan_ioenh0579.png "/>


Providing configuration for scripts

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A5/29/wKioL1m40HGDF47tAAAXFjkmjLw201.png "title=" Qq20170913143007.png "alt=" Wkiol1m40hgdf47taaaxfjkmjlw201.png "/>


Use example: To set the host name

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A5/29/wKioL1m40TPB96AMAAAR3Tk9gds160.png "title=" Qq20170913143315.png "alt=" Wkiol1m40tpb96amaaar3tk9gds160.png "/>

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/06/79/wKiom1m40YXzaj_6AAAc6baEPOw562.png "title=" Qq20170913143403.png "alt=" Wkiom1m40yxzaj_6aaac6baepow562.png "/>


Mkstemp command

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/A5/29/wKioL1m40iPidPO9AABHRl7jVzo074.png "title=" Qq20170913143721.png "alt=" Wkiol1m40ipidpo9aabhrl7jvzo074.png "/>


Install command

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/A5/29/wKioL1m40weiCYzSAAA7DJkG3cs033.png "title=" Qq20170913144110.png "alt=" Wkiol1m40weicyzsaaa7djkg3cs033.png "/>


Exercise: Write a script

1) Prompt the user to enter the name of an executable command

2) Get a list of all library files that this command depends on

3) Copy the command to a target directory (for example,/mnt/sysroot) under the corresponding path,

/bin/bash-/mnt/sysroot/bin/bash

/USR/BIN/PASSWD-/MNT/SYSROOT/BIN/PASSWD


4) Copy all the library files that this command relies on to the corresponding path under the target directory

/lib/ld-linux-x86_64.so.2-/mnt/sysroot/lib/ld-linux-x86_64.so.2


Further to:

Each time a copy completes a command without exiting, it prompts the user to type the new command to be copied and repeats the above function until the user enters quit

#!/bin/bash# version: 0.0.15# author: lcc.org# description:&nbsp, ..... Dir= "/mnt/sysroot" [ -d  $DIR  ] | |  install -d  $DIRfunction  cpcmd {cmd_path=$ (which --skip-alias $1) Local  cmdir=$ (echo  $cmd _path | sed  ' s,/$,, '  | sed -r  ' [email  Protected] (. */) ([^/]+) @\[email protected] ') [ -d  $DIR $cmdir ] | |  install -d  $DIR $cmdir[ -f  $DIR $cmd_path ] && echo -e   "\033[32m$dir$cmd_path\033[0m is exist"  | |  cp  $cmd _path   $DIR $cmd_path }function cp_lib {for i in $ ( ldd  $cmd _path | grep -o --color=auto  "/.*[[:space:]"  | tr -d   '   ');  dolocal libdir=$ (echo  $i  | sed  ' s,/$,, '  | sed -r   ' [email protected] (. */) ([^/]+) @\[email protecteD] ') [ -d  $DIR $libdir ] | |  install -d  $DIR $libdir[ -f  $DIR $i ] && echo -e  "\ 033[32m$dir$i\033[0m is exist " | |  cp  $i   $DIR $idone}read -t 10 -p  ' Enter a binary program  name:  '  fukewhile [  $fuke  !=  ' quit '  ]; doif [  ' $fuke  ==  "Quit"  -o -z  "$fuke"  ]; thencontinueelif ! which  $fuke  &> /dev/null; thencontinuefi# #复制命令cpCmd   $fuke # #复制库文件cp_lib # #显示复制完成echo &NBSP;-E   "\033[32mcopy finished.....\033[0m" read -t 10 -p  ' enter again a  binary program name:  '  fukedone




This article is from the "Reading" blog, make sure to keep this source http://sonlich.blog.51cto.com/12825953/1964938

Thank you---------Linux Bash programming

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.