If you want to use linux, you will not know the basic syntax of the shell.

01. Variables

1. Environment variables

echo $PATH

2. Custom variables

hello="hello_world"
echo $hello

3. Store the execution results of Linux commands as variables

(2 ways, the second one is recommended, the first one is that the slanted point above the ~ key is more difficult to identify)
files= ls -al
path=(pwd) Pay attention to the point of defining the variable = There should be no spaces on both sides of the sign, and symbols need to be added to use variables

 If you want to learn automated testing, here I recommend a set of videos for you. This video can be said to be the number one automated testing tutorial on the entire network at station B. At the same time, the number of people online has reached 1,000, and there are notes to collect and share with you. Dashen Technical Exchange: 798478386   

[Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (the latest version of actual combat)_哔哩哔哩_bilibili [Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (actual combat) The latest version) has a total of 200 videos, including: 1. Why should interface automation be done in interface automation, 2. The overall view of request in interface automation, 3. Interface combat in interface automation, etc. UP hosts more exciting videos, please pay attention to UP account . icon-default.png?t=N7T8https://www.bilibili.com/video/BV17p4y1B77x/?spm_id_from=333.337

02. Basic operations

1. Operator

+: Add
-: Subtract
*: Multiply
/: Divide
%: Take the remainder
==: Determine whether they are equal
! =: Not equal

: greater than
= : greater than or equal to
< : less than
<= : less than or equal to

2. Integer operations (expr)

expr 10 + 3
echo $[10 + 3]
将计算结果存储为变量2种方式
num=$(expr 10 + 3)
num=`expr 10 + 3`

 Note that
the operator must be preceded and followed by a space
* and must be escaped with a backslash

3. Integer operation ($[])

num1=100
num2=200
sum_num=$[$num1+num2]

Note that
there can be no spaces before and after the operator
* multiplication does not need to add escape characters

4. Floating-point operations

num=$(echo "scale=2;10/3" | bc)

scale: retain the number of decimal places

Give 10/3 to the bc calculator, rounding to two decimal places

03. Condition selection

The if is followed by a command, which is actually the exit status code of this command. If it exits normally, the status code is 0, and the others are not 0. This means that if it is 0, execute then, otherwise it will not execute

1、

if-then
if command
then 
    "执行成功"
fi

2、

if-then-else

3、

if-then-elif-else
if判断基本语法,if开头,fi结尾
#/bin/bash
  NUM='4'
  if (( $NUM > 4 ))
    then
    echo "$NUM more then 4"
  elif (( $NUM == 4 ))
    then 
    echo "$NUM 等于4" 
  else
    echo "$NUM less then 4"
  fi 

 4. case statement

case $num in
1)
  echo "num=1"
2)
  echo "num=2"
3)
  echo "num=3"
esac

04. Cycle

1、for - in

for i in list
do
  commands
done

2. C language style

for (( i = 0;i <= 10;i++))
do
  commands
done

3. while loop

while test command
do 
  echo "条件满足的时候执行这里的操作"
done
num=8
while (( $num < 10 ))
do
  echo "条件满足的时候执行这里的操作"
  echo "$num小于10"
done

4. until loop

until test command
do 
    echo “条件不满足的时候执行”
done

 illustrate:

  1. condition is a conditional expression, if the value is false, then continue to execute the statement in the loop body, otherwise jump out of the loop

  2. The until loop is just the opposite of the while loop

  3. control loop

    break Jump out of the outer loop, same as python

    continue terminates the current loop and enters the next loop, same as python

while  ((  1>0  )) 
do
    echo –n “输入一个数字”
   read num
    if (( $num>10 ))
       then  
           break
      else
          continue
    fi
done

05. Command line parameter processing

The bash shell can obtain parameters according to the parameter position.
Get the 1st to 9th command line parameters through $1 to $9.
$0 is the shell name. If there are more than 9 parameters, use ${10} to get them

06. Obtain user input (read)

A single input, specifying the variable to receive the value of the input (choice)

echo -n "yes or no(y/n)"
read choice
echo "you choice is : $choice"

 Single input, no variable is specified to receive the value of the input, read will put any data it receives into the special environment variable REPLY

echo -n "yes or no(y/n)"
read
echo "you choice is : $REPLY"

multiple inputs

read -p "what is you name?" first second
echo first:$first
echo second:$second

 The above example will first output what is you name? and then wait for user input in this line. The read -p here achieves the effect of echo -n + read in the above example without line breaks. The input parameters are separated by spaces. If the input If the value exceeds the number of received variables, the shell will assign the remaining values ​​to the last variable.

07. Timeout setting

if read -t 5 -p "enter you name:"
name
then
  echo "hello $name"
else
    echo "time out"
fi

important point

  1. Variable names are generally capitalized

  2. Numeric comparison (( 8 > 7 ))

  3. String comparison [[ str1 != str2 ]]

  4. The test command can only judge the following three types of conditions

    Numeric comparison
    String comparison
    File comparison

  5. 5The flow control statement of the shell script and the end statement are the reverse of the start statement, such as the end statement fi of the if, and the esac end statement of the case

     Numeric comparison
     String comparison
     File comparison

 

Guess you like

Origin blog.csdn.net/m0_73409141/article/details/132606059