shell script math, arrays, text processing

 intellectual point
math operations
using an array of
seq, tr, sort, uniq, cut commands

 integer arithmetic
1. Common operator
adding: +
subtraction: -
multiplication: *
division: / are commercially obtained, for example, to give a 1/2 0
modulus (remainder) operator:%
exponentiation: ** bc is the symbol ^ exponentiation

2. Common integer arithmetic
first
DECLARE -ia = 10 20 is; echo a less use The first two Kind a = a second less a = (Expr 10 '' 20 is); A $ echo less (good compatibility)
Note: * or ** operator when necessary to add an escape character \ or '

The third
A = $ [10 2] echo a [ ] in of change the amount no need plus a [] variables without adding
echo [ R A N D O M The first four Kind a = [RANDOM% 10], the fourth random number seeking 0-9 a = ((10
20)); echo $ to ( ( ) ) in of (()) Without adding the variables
echo $ ((RANDOM% 10)to find the random number 0-9
fifth (recommended)
((A= 1020 is)); echo a ( ( a + + ) ) ( ( ) ) a commonly used to assign ((a ++)) variable (()) is without adding
((a + = 10)) corresponds to a = a + 10
e.g. ::Here Insert Picture Descriptionsixth
the let A = 10
20 is; echo $ A commonly used to assign
let i ++ equivalent I = I +. 1 the let
the let I = + 10

L, which test
double parentheses integer value comparator (recommended method)
format ((integer operator integer of 2 1))
== (for equality),>, <,! = (Not equal judgment), <=,> = Here Insert Picture Descriptionl decimal arithmetic
bash only supports integer arithmetic, floating point arithmetic with bc
(()) can only be used to compare integer and integers. . And to use fractional arithmetic comparison bc Here Insert Picture DescriptionNOTE:. 1 scale bc is a variable, several left after the decimal point is used to specify
the comparison is satisfied in 2.bc, needs to be determined by the standard output of bc 0 is not established for the establishment of a Here Insert Picture Descriptionprintf "% 0.2f" is the formatted output: supplement
basic usage  array
can be used the same name 1. array (array) is a collection of several different types of variable composition, refer to these variables. Consisting of a continuous array of storage units, corresponding to the lowest address of the first element of the array, the highest address corresponding to the last element
2.bash Shell supports only one-dimensional array, the array reference numerals from 0 to array [x] represents an array element, then, array [0] means that the first element of array array, array [. 1] shows a second element array of the array, array [X] denotes the x + 1 th element of the array array of
indexed arrays (ordinary arrays ), the subscript numbers, ordered, from zero by default
associative array, the subscript string, disordered
3. statement array
format: name = array (parameter 1 parameter 2, ...)

4. ip addresses stored in the plurality of array Here Insert Picture DescriptionNOTE: Alternatively slices and
slice: Extraction e.g. 2nd to 6th small scale in the array array Here Insert Picture DescriptionReplace: replace e.g. D array in the array d in, but does not change the array the original content Here Insert Picture Descriptionis desired to change the original contents of the array, the array need to re-define, for example: Here Insert Picture Descriptionthe number of elements in the array to view inside Here Insert Picture Descriptionadded: remove an element in the array, the array index is not reordered. Reordering need to re-define the array

6. Use the array to get the current host's default gateway Here Insert Picture DescriptionNote: route -n command to view the routing table, routing table has a default gateway
using route add default gw 172.16.255.254 command to add a gateway 172.16.255.254
grep '^ 0.0.0.0' 0.0.0.0 extracting rows beginning
7. replace the variable array Here Insert Picture Description8. the cyclic array assignment Here Insert Picture Description associative array (associative array)
array bash 4.0 began to support associative arrays, does not support the RHEL5
associative array subscript string, no key sequence, an associative array (index) is not the same Here Insert Picture Descriptiondeclare -A statement associative array echo $ {name [@]} to obtain the contents of the array element echo $ {# name [@] } to obtain the number of the subscript echo $ {! name [@]} to obtain the content index

seq command
seq
Purpose: To print out a string of sequential digital
format: seq [options] range of numbers
-s: Specifies the separator character
-w: Specifies the same broadband output Here Insert Picture Description script sample
writing scripts to create users, requires the user name prefix input and the number is automatically created Here Insert Picture Descriptiontr command
1.tr command
character conversion tool
can only stdin (standard input) operations, not directly file operations

2. conversion character tr
tr SET1 SET2
replaced with characters out of the characters SET2 SET1 same position in
echo 123456 | ABC 345 tr
tr 123 ABC <AA
tr '[AZ]' '[AZ]' </ etc / the hosts
echo $ PATH | tr ':' ' \ n'

3. Delete character tr
tr -d SET
the stdin stream to delete the same data character the SET
echo 123456 | -d 345 tr
tr -d '[0-9]' </ etc / the hosts
DF -H | tr -d %
the WHO | TR -d ''

4. Use compressed character tr
tr -s SET
will compress consecutive identical characters into a character
echo 112233444555666 | tr -s 345
the ifconfig | tr -s ''
tr -s the SET1 SET2
to replace SET2 recompression
echo 112233444555666 | tr -s ABC 345
echo 112,233,444,555,666 | TR 345 ABC | TR -s ABC
WHO | TR -s '' '\ n-'

sort command
sort order
defaults for each line of the first character sort
-n: sort by integer
-r: descending order

Specify the sort keys
to specify which column to sort by data
-k: specifies which as sort keys
cat tt | sort -n -k4

Specify field delimiter
-t: Specifies the field separator (default is blank)
Sort -t: -n -K3 / etc / the passwd

unip command
uniq command
to delete data after a sort of duplicate records
is usually used in conjunction sort and
sort -n tt | uniq

Examples of statistics
-c: count the number of occurrences of duplicate records (the same count how many)
cat tt | uniq -c
-u: show only a single line of
cat tt | uniq -u
-d: show only duplicate rows
cat tt | uniq -d

 data extraction
1.cut command
to extract text from a text file or text column flow
cut - the range of options to extract text file
Common Options
-c: extract characters from a specified extraction range in
-f: extract fields from the specified extraction range in
the extraction range
n: the first n items
n-: n-th item to the end of line
-m: first row to the m-th item
n, m: m-th and n-th item item
nm: n-m-th item to item

2.cut -c命令
ls -l | cut -c10
who | cut -c 20-40
cut -c -10 /etc/passwd

3.cut -f command
-d: Specifies the delimiter defaults to the Tab
Cut -d "" -f2 filename
means that I am a space for the column separator to extract the second column
cut -d ":" -f 1,7 / etc / passwd
the WHO | Cut -d '' -f1,6

grep data extraction program
uses: Find the file and displays the lines containing the specified string
format: grep [option] ... mode object file
-i: ignore case when searching
-v: reverse lookup, the line does not match the output mode
-w: Search by a whole word
-n: display the line number in line mode requires
-r: recursive search for all files
-o: show only specified character

Guess you like

Origin blog.csdn.net/qq_43028054/article/details/89195559
Recommended