18 shell script --009 arrays and strings

Review:
Function: write a block of code, called repeatedly used;
1, function written format
2, the parameter directly added after the function name can; if outside
abc () {
function body @ $
}
ABC 1 2 . 5. 4. 3
: WQ

a.sh. 1. 5. 4. 3 2. 6
. 3, variable
local local variable
local i = 1
if the function in vitro also defines a variable of the same
extension: source / bash / chmod + x full path
[parent and child processes the problem
4, return
ends the execution of the function body
and exit but do not
return and exit are specified by the return value of 0 for successful execution, 1-255 representatives failed
return only in the function body;
5, iteration
iteration time must to refer to the initial value! ! !
n!

n!=n(n-1)!

fact(){
if [ $1 -lt 1 -o $1 -eq 0 ];then
echo 1
else
echo "$[$1*$(fact $[$1-1])]"
fi
}
fact 100

1 2 3 4 5 6 7 8
1 1 2 3 5 8 13 21 34 ... 斐波那锲数列
F(n)=F(n-1)+F(n-2)

fab(){
if [ $1 -eq 1 ];then
echo 1
elif [ $1 -eq 2 ];then
echo 1
else
echo "$[$(fab $[$1-1])+$(fab $[$1-2])]"
fi
}
fab 100

fork bomb:
. {()
.. | &
}
.

. () {|.. &} ;. [look into other languages ​​fork bomb how to write]

==========================================================

An array of strings and
arrays
arrays that white is the period of continuous variables, a contiguous memory storage space
Solution: too many variables problem; in the same class variables, we do not need to define multiple names, but in an array of ways to defined above; (listing)
were good cloud three INSTITUTE [1] Zhuo =
1, the array name
2, index
3, the value of

How to define the array:
DECLARE defined integer -i (affirmed)
DECLARE -a definition array Array
DECLARE -A defined nested arrays
were good cloud INSTITUTE [1] = professional cloud
cloud professional [1] = three times a cloud
cloud three class [1] = Zhuo
[such an array of values, not commonly used in bash]
[python and shell]
array = (value)
array = ()

How to assign the array:
Array = ()
. 1, Array [. 8] = Zhuo (in computer languages, typically the index count from zero)
(not necessarily the index numbers: key-value array [Zhuo] = 1.87m )
(array assignment in the shell, can jump (jump assignment))
2, = array (/ var / log / *. log)
made traverse
array = (ABCDE)
. 3, = array ( 'Zhuo = 187' = 186 Lianghao Tian '...)
4, assigned by the read command to
read delay -t -p "specify Description" -a variable desired value input array
read -aabcde

How to call the array:
$ {variable name}
$ {name array [index]} which index is defined, it is to see the index corresponding to the specified values
$ {name} called by default array index value = 0 $ Array (preferably Do not write)
$ {array name [*]} or $ {array name [@]} the value of all calls within the array $ * $ @

under exercise: statistics /var/log/*.log in an even index file and the number of lines;
a = (. / var / log / log *)
for I in {0..7}; do
IF [$ [$ I% 2] -eq 0]; the then
line WC -l $ = ` A {[$ I]} | Cut -d '' -f1`
SUM = $ Line # + + SUM = $ `-l WC {A [$ I]} | Cut -d '' -f1` # + SUM = $ (WC a $ {-l [$ I]} | Cut -d '' -f1)
Fi
DONE
echo $ SUM

. 1, the array length of an array of call
length ( "variable")
$ {# variable name [*]} or {# $ variable name [@]}
2, special Add the array values
$ {variable name [$ {# variable name [@]}]} = "value" or $ {variable name [$ {# variable name [*]}] } = "value"


other operations on the array of
the array slice
$ {variable name [@]: offset: number of values that need to remove}
echo $ {a [*]:2:4}
$ {Variable name [@]: Offset}
delete the value
unset $ {variable name [index]}
Note: Delete is the value of the index, but the index does not take a deletion;
[to know the value, how to view the index? ]
Modified value of
A [. 3] = C
A [. 3] D = modified i.e. cover


Exercise:
generating a random number 10, wherein the maximum value of the print
DECLARE -aa
DECLARE -i max = 0

for I in SEQ 0 9 ``; do
A [$ I] = $ the RANDOM
[$ {A [$ I]} - $ max gt] && $ = max {A [$ I]}
DONE
echo $ {A [@]}
echo "maximum value: $ max"

10 generates random numbers, and sorted


String:
==============================
the printf
name = Zhuo
hiagth = 183 is

Python (% S, raised his of% d)% (name, hiaght )
output: CHONG his elevated 183

S%
% D
% F

% [NUM] Specifies the width of the string S placeholder
% - [num] s on behalf of Left
[centered]

=============================

1, the string sections:
an array of slices: $ {variable name [@]: Offset: The 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} the default assignment means
Read "sasasa:" Test
[Test the -Z $] && Test ABC =
2, based on a mode to take a substring
from the left and right from the
$ {variable name #pattern} to remove first match pattern string
$ {variable name ## pattern} to delete all the pattern matching string

from right from left
$ {variable name pattern}%
$ {variable name %% pattern}

Thoughts: Take the url IP (domain name)

3, replace characters
$ {variable name / pattern / replacement string} is not a regular expression pattern, but the wildcard file]
match the first pattern, to replace
$ { after a string variable name // pattern / replacement}
matches all oh pattern, and replace
$ {string variable names / # pattern / replacement}
specifies the beginning of the line must be matched pattern, will replace
$ {variable name / % pattern string of / replacement}
specifies the end line must be matched pattern, replaces a

Note: / separator, not replaced (#% other special characters, and sed not the same)

4, find and delete
the alternative, do not specify a replacement string, that is, to delete
$ {variable name / pattern}
$ {variable name pattern} //
$ {variable name / # pattern}
$ {variable name /% pattern}

5, the replacement case
tr Alternatively sensitive
tr [[: lower:]] [[: upper:]] <filename

$ {} String variable name ^^ replace all uppercase
$ {} string variable name ,, replace all lowercase

6, variable assignment
variable name 1 = $ {variable name 2: -word} no space! Enter a default value of the action, not the variable 2 values, default values will be assigned to a variable word 1
variable name 1 = $ {variable name 2: + word} used without variable has a value of 2, the default value was assigned to the word 1 variable
$ {variable: = word} If the variable has no value, will be assigned to a variable word, there is value, the original value of
the variable $ {variable 1 = 2:? error_infor} erro_infor error message if the variable value is not 2, the output
[I do not know how to use? ]

7, the script application other variables in the file, how to achieve?
filename1
Vim filename1
A. 1 =
B = 2
: WQ

filename1.sh Vim
. / full path / filename which source / full path / filename // execute the entire code
C = $ [$ $ A + B]
ehco $ C
: WQ

bash filename1.sh

Exercise: Review hostname, define a hostname = XXX file name, hostname.sh
hostname
name = test.com

hostname.sh
IF [-R & lt / the root / hostname]; the then
/ the root / hostname.
the else
echo "/ the root / hostname not have execute permissions "
Exit 1
fi

hostname $name

8, two command
mktemp creates temporary files and directory temp - / tmp
to avoid all take a file name repeated; mktemp name .XXXXXX (XX represents a random number (letters and numbers))
is created by default is random file
-d is created random directory

file=`mktemp a.XXXX`

install copy or delete files, use the same copy
advantages can assign permissions to copy files
Usage: intall a file directory // b b copy a file to a directory, and respond to initial permissions (file: 755 Contents: 644)
-t source project on the exchange
-d create
-m the MODE
-o OWNER
-g the GROUP

Exercise:
1, obtaining all the libraries of this command depends list
Ldd / bin / CAT
2, the copy command to the corresponding path in a specified destination directory (e.g., / mnt / sysroot)
/ bin / the bash  / mnt / sysroot / bin / the bash
/ usr / bin /  the passwd / MTN / sysroot / usr / bin / the passwd
. 3, copy all libraries to this command depends path corresponding to the target directory
/ lib64 / ld-linux-x86-64 . so.2  /mnt/sysroot/lib64/ld-linux-x86-64.so.2
each copy after the completion of a command, do not quit, but prompts the user to enter a new copy of the order, and repeat the above functions, until the user enters quit quit;

 

 


 

 

 

 

 

 


Guess you like

Origin www.cnblogs.com/wx00/p/11386659.html
Recommended