If you want to use linux, you can't get the basic shell syntax~

01. Variables

1. Environmental variables

echo $PATH

2. Custom variable

hello="hello_world"
echo $hello

3. Store Linux command execution results as variables

(two methods are recommended. The second one is recommended. The first one is that the oblique point on the ~ key is difficult to identify)
files=ls -al
path=(pwd) note that there can be no spaces on both sides of the variable = sign. Variables need to be signed

02. Basic operation

1. Operator
+: add
-: minus
*: multiply
/: except
%: residual
==: judge whether it is equal
!=: Not equal to

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

2. Integer operation (expr)

expr 10 + 3
echo $[10 + 3]
There are two ways to store the calculation results as variables
num=$(expr 10 + 3)
num=`expr 10 + 3`

Attention
Operators must be preceded by spaces
*Multiply must be escaped with a backslash

3. Integer operation ($[])

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

Attention
No spaces are allowed before and after operators
*Multiplication does not require an escape character

4. Floating point operation

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

scale: number of decimal places reserved

Give 10 / 3 to bc calculator and keep two decimal places

03. Condition selection

If is followed by a command. In fact, it is the exit status code of the command. Normal exit, status code 0, and others are not 0. This means that if it is 0, execute then, otherwise do not execute

1,

if-then
if command
then 
    "Successful execution"
fi

2,

if-then-else

3,

if-then-elif-else
if Judgment basic grammar,if start, fi ending
#/bin/bash
  NUM='4'
  if (( $NUM > 4 ))
    then
    echo "$NUM more then 4"
  elif (( $NUM == 4 ))
    then 
    echo "$NUM Equal to 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 "Perform the operation here when the conditions are met"
done
num=8
while (( $num < 10 ))
do
  echo "Perform the operation here when the conditions are met"
  echo "$num Less than 10"
done

4. until loop

until test command
do 
    echo "Execute when conditions are not met "
done

explain:

1. If condition is a conditional expression and the value is false, continue to execute the statements in the loop body, otherwise jump out of the loop

2. The until loop is handled in the opposite way to the while loop

3. Control cycle

break jumps out of the outer loop, the same as python

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

while  ((  1>0  )) 
do
    echo –n ""Enter a number"
   read num
    if (( $num>10 ))
       then  
           break
      else
          continue
    fi
done

05. Command line parameter processing

The bash shell can get parameters based on their location
Get the 1st to 9th command line parameters through $1 to $9
$0 is the shell name. If there are more than 9 parameters, you can get them through

06. Get user input (read)

Single input, specifying that the variable receives the input value (choice)

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

For a single input, no variable is specified to receive the input value. 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? Then wait for user input in the line. read -p here realizes the effect of echo -n + read without line break in the above example. The input parameters are separated by spaces. If the input 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

Attention

1. Variable names are generally capitalized

2. Numerical comparison ((8 > 7))

3. String comparison [[STR1! = STR2]]

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

Numerical comparison
 string comparison
 File comparison

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

 Numerical comparison
 string comparison
 File comparison

Here is our question brushing moment! Scan the code and brush the interview questions with the small program. See how much you know? I don't know how much

The following is the test data. It should be the most comprehensive and complete war preparation warehouse for friends doing [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

Finally, it can be in the official account: the sad spicy bar! Get a 216 page interview document of Software Test Engineer for free. And the corresponding video learning tutorials for free!, It includes basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, test advanced Python programming, Web automation test, APP automation test, interface automation test, advanced continuous test integration, test architecture, development test framework, performance test, security test, etc.

Don't fight alone in learning. It's best to stay together and grow together. The effect of mass effect is very powerful. If we study together and punch in together, we will have more motivation to learn and stick to it. You can join our testing technology exchange group: 914172719 (there are various software testing resources and technical discussions)

Friends who like software testing, if my blog is helpful to you and if you like my blog content, please click "like", "comment" and "collect" for three times!

Haowen recommendation

Job transfer interview, job hopping interview, these interview skills that software testers must know!

Interview experience: move bricks in first tier cities! Another software testing post, 5000 will be satisfied

Interviewer: I've worked for three years and have a preliminary test? I'm afraid your title of software test engineer should be enclosed in double quotation marks

What kind of person is suitable for software testing?

The man who left work on time was promoted before me

The test post changed jobs repeatedly and disappeared

Tags: Linux Operation & Maintenance Programmer bash software testing

Posted on Tue, 26 Oct 2021 08:34:02 -0400 by barbatruc