Linux study notes----day7(3.21)-----Script syntax foundation

arithmetic operations

expr numerical operation tool

The example is as follows: (The value of dd is the sum of aa and bb)


Expression evaluation:

addition, subtraction, multiplication and division


There are four forms of arithmetic operations: the form is as follows

let C=$A+$B

C=$[$A+$B]

C=$(($A+$B)) This is the certificate operation

C=`expr $A + $B` (backticks, space between expression and operator)

Differences in run mode (source, shscript, ./script)

Here is a new filename.sh script

(Note: read -p "........." XXXX prompts a ..... information first, and then enter the XXX content.)



We know of no issues with the script. We echo the two variables and find that they do not exist.


Found no such variable. This is because of the child process relationship. The script we wrote is equivalent to a child process, and the echo variable is in the parent process

At this time, the variables in our child process cannot be displayed in the parent process.


How to solve it? We need to use source, as shown in the screenshot, variables can also be displayed outside





Judgment and control flow in shell

variable expression

Conditional judgment command test

Conditional test type

Integer test

test n1 - parameter n2 returns 0 if true, 1 if false

The parameters are as follows:

-lt is less than little

-le less than or equal to little equal

-gt is greater than great

-ge is greater than or equal to great equal

-eq equals equal

-ne is not equal not equal

Chestnut:

Note: The equivalent form of the test statement [ 1 -lt 4 ] has a space between the expression and the brackets

Chestnut: If you enter a parameter x is a positive number, then display x number is positive




file test

parameter:

-f exists and is a normal file

-d exists and is a directory

-s exists and the number of bytes is greater than 0

-r exists and is readable

-w exists and is writable

-x exists and is executable

test -d "mydoc" to determine whether mydoc is a directory

Chestnut: Check if the file entered from the command line exists   



Analysis: When sh eg2.sh, there is no parameter. So execute the first if statement (that is, to determine whether the number of parameters is 1)

When the parameters are followed, the second if is executed to determine whether it exists and is a normal file.


String test

test s string s is not empty

test s1 = s2 string s1 is equal to s2

test s1 != s2 string s1 is not equal to s2

test -zs string length = 0, which is the empty string zero

test -n string length > 0

-a logical and
-o logical or

! logical not


Control flow (branching, looping flow)

seq command: generate a sequence of numbers 1-9


branch structure

if branch

if [ $# -eq 0 ]
then
echo "0 arguments entered"
elif[ $# -gt 1 ]
then echo "multiple arguments entered"
else echo "1 argument entered"

case
case "$#" in
0)echo "0 arguments entered";;
1)echo "1 argument entered";;
*)echo "multiple arguments entered";;
esac
must be after each branch condition ends with two semicolons

loop structure

for loop

for i  in `seq 1  9`
do
echo `expr $i \* 10`
done
或写echo $(expr $i \* 10)



read variable1 [variable2. . . ]

The values ​​of multiple variables can be read from the keyboard. When the user enters data, they are separated by spaces or the Tab key.
If the number of input data is not enough, it will be assigned from left to right, and the variable without input will be empty;
if the number of input data is exceeded, it will be assigned from left to right, and the last variable will be assigned all the remaining data.

-p: Specifies the prompt when reading the value;
read-p "please intput a num:"num

Chestnut:

Output the 10 multiples of each number in the sequence a to b

read a b

for i in `seq $a $b`

do

echo $(expr $i \* 10)



while loop




Use while and break to wait for the user to log in



Break and continue commands

Break is to jump out of the loop without executing continue is to jump out of this loop

while condition1 #outer loop
do ...
while condition2 #inner loop
do ...
break 2 #interruption of outer loop Here is all out of the loop
done
done

... # after the interruption, continue to execute the program here

Chestnut:

       


functions in the shell

定义函数
hello()
{
echo "Hello there today's date is `date`“
}

All functions must be defined before they can be used. This means that the function must be placed at the beginning of the script and not available until the shell interpreter first discovers it. A function is called using only its function name. In the above example, the function name is hello, and the function body contains an echo statement that returns the current date.

在脚本中使用函数
#!/bin/sh
# func1.sh
hello ()
{
echo "Hello there today's date is `date`“
}
echo "now going to the function hello”
hello
echo “back from the function"

In the above example, the function is defined at the top of the script. It can be called in a script with the function name hello. After the function executes, control returns to the next statement of the function call, the feedback statement back from the function.


Arguments can be passed to functions and accessed by scripts

fname arg1 arg2

fname()

{

      echo $1,$2;

        echo "$@";

        echo "$*"

return 0;

}

array

Array definition
An array can be defined using a column of values ​​in a single line:
array_var= (1 2 3 4 5 6) #These
values ​​will be stored in consecutive positions starting at 0.
Alternatively, an array can be defined as a set of "index-value":
array_var[0]="test1"
array_var[1]="test2"
array_var[2]="test3"
array_var[3]="test4"

array_var[4]="test5"




associative array

The subscripts and values ​​of an associative array are called key-value pairs, and they have a one-to-one correspondence. In an associative array, the keys are unique and the values ​​can be non-unique.

Defining Associative Arrays
Shell's associative arrays and shell's subscripted arrays are exactly the same in definition and usage, but differ in indexing.

It should be noted that before using an associative array, you need to use the command declare -A array to make an explicit declaration

Chestnut:

name=(jim tom lucy)

declare -A phone #The declaration of an associative array, at this time phone is an associative array
phone=([jim]=135 [tom]=136 [lucy]=158)
for iin `evalecho {0..$((${# name[*]}-1))}` #Generate a sequence, 0 to name array length-1
do
echo ${name[i]} phone number is ${phone["${name[i]}"]}

done

Syntax description

${!array[*]} Get all the keys of the associative array
${!array[@]} Get all the keys of the associative array
${array[*]} Get all the values ​​of the associative array
${array[@]} Get all the values ​​of the associative array
${#array[*]} length of associative array
${#array[@]} length of associative array

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813437&siteId=291194637