Shell Script Summary

Source: Internet
Author: User

Shell Script Summary Array

Several common uses of indexed arrays

array[0] array数组的第一个值array[*] array数组的所有值#array[*] array数组里数值的个数unset array[0] 删除array数组里的第一个值unset array 删除掉array整个数组


Array slices:

    ${array[@]:num1:num2}    num1:要跳过的元素个数    num2:要取出的元素个数    取偏移量之后的所有元素        

Array Scripting exercises:
1. Write a script that defines an array in which the elements in the array are all files ending in. Log in the/var/log directory, and the sum of the number of rows in the file whose subscript is even
#!/bin/bash
Array= (/var/log/. Log)
Echo ${array[]}
Sum=0
For I in $ (seq 0 $[${#array [
]}-1]);d o
If [$[$i%2]-eq 0];then
num=$ (cat ${array[$i]}| wc-l)
sum=$[$sum + $num]
Fi
Done
Echo $sum

2. Enter several values into the array, using bubbling method to sort ascending or descending order

[[email protected] 0323]# cat Maopao #!/bin/bashi=0flag=0wrong=0#-------------------------------------------- Save the array value---------------------------------------------while True;do read-p "Enter a number (enter quit):" Num if [["$num" = = ^ ""] ];then echo "Please enter the number you have lost the wrong $wrong times" let wrong++ fi if [$wrong-eq 5];then echo "You can get the wrong $wrong times I can't stand you. "Break Elif [[$num = = quit]];then break elif [[$num =~ ^[-]{0,1}[[ :d igit:]]{0,}[.] {0,1} [[:d Igit:]] {0,} [[:d igit:]]$]];then #筛选出 integer decimal negative echo $num array[$i]= $num let i++ else echo "Please enter a number you have lost the wrong $wrong times "Let wrong++ fidone#---------------------------------------------compare two number size------------------------------- -----------Compare () {echo $ >/app/0324/tmp echo $ >>/app/0324/tmp max=$ (cat/app/0324/tmp|sort-n R|HEAD-1) If [["$max" = = "$"]];then flag=1 else flag=0 fi}#-------------------------------------------Bubble Sort---------------------------------------------------length=${#array [*]}for i in $ (seq 0 $length);d o For j in $ (seq 0 $[$length-$i]);d o t=$[$j +1] Compare ${array[$j]} ${array[$t]} if [$flag-eq 1];the n temp=${array[$j]} array[$j]=${array[$t]} array[$t]= $temp Fidonedoneecho "from small to large Sort to: ${array[*]} "

Final result diagram

3. Implementing Matrix Transpose

        #!/bin/bash #------------------------------------------Input Matrix------------------------------------------------ ---read-p "Please enter the number of rows in the Matrix" raw t=0 for I in $ (seq 0 $[$raw-1]);d O for J in $ (seq 0 $[$raw-1]);d o r Ead-p "Value of input matrix" value array[$t]= $value let t++ do done#-------------------------------  -----------the middle position of the computed matrix-------------------------------------------------------------Juzhen3 () {t=0 if [$[$raw%2] -eq 0];then flag=$[$raw/2] Else flag=$[$[$raw +1]/2] fi#------------------------------------------Output Original  Matrix--------------------------------------------------for I in $ (seq 0 $[$raw-1]);d o u= $i for                                    J in $ (seq 0 $[$raw-1]);d o echo-e "${array[$t]} \c" Let t++ done#------------------------------------------the transfer symbol in the middle of the output------------------------------------- --If [$[$i +1]-EQ $flag];then echo-e "\c" for M in $ (seq 1 $raw);d o echo-e ' =\c ' do                                                     Ne echo-e "> \c" Else echo-e "\c" for M in $ (seq 1 $[$raw]);d o Echo-e "\c" done Echo -E "\c" FI #------------------------------------------output transpose matrix------------------------------------------                                    ------for J in $ (seq 0 $[$raw-1]);d o echo-e "${array[$u]} \c" Let u=$[$u + $raw] do echo done unset t i} juzhen3

Final result diagram:

string slices

Several common uses

eval command

The first function of Eval is that all permutations of the command can be performed before the command is executed.


The second function is to make a scalar assignment of multiple values

Shell Script Summary

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.