Engage in basic series-Linux system-shell script writing

Preface

As an Android developer, you may spend most of your time writing and learning upper-level code. You may not know much about some basic things such as operating systems, networks, data structures and algorithms, and design patterns, and you may not know much about basic series preparations. Starting from personal experience, sort out some basic knowledge points that I think are more important and helpful to the work. Although they cannot be exhaustive, they strive to include key common knowledge. This is not only a sharing, but also a record and summary of personal learning.

Linux learning route

image
(This roadmap is only based on my own experience and reality. I personally feel that I can use and need to understand the knowledge in peacetime. I don't want to be comprehensive, as long as I focus on memory breakthroughs.)

1. What is a shell script

Shell scripts are the same as batch processing commands in windows. Shell scripts are a program written using shell functions to combine shell commands, and at the same time use judgments and loop condition statements to perform judgments to achieve some complex functions. Some people say what is the difference between this and ordinary java and c++ programming languages? In fact, there is no difference, but the use of shell scripts does not need to build a compilation environment, which is more universal, but the function is relatively inferior to high-level languages ​​such as java and c++.

2. Basic script structure

The most basic way to learn a language is hello world. Here we also take this example as the entry point. Take a look at the test.sh script.

#!/bin/bash
#这是个demo
echo -e "Hello World!\n"
exit 0

The first line #!/bin/bash declares the shell name used by the script, and this line is used to declare that the syntax in this file uses bash syntax.

The second line #this is a demo where # stands for comments.

The third line echo -e "Hello World!\n" is a shell command, and Hello World is output on the screen

The fourth line exit 0 is used to inform the execution result of the shell script, which is a bit similar to the main function of C++, and finally has an int type return value, through which you can mark some error messages. The return value can be obtained through $? after the script is executed. After the above script is executed, enter echo $? on the command line. You can see that 0 is returned.

3. Basic grammar

In fact, there are some routines for getting started in any language. The most basic grammar is how to define variables, the writing of conditional statements and loop statements, and the definition of functions. These are the most basic, and the writing of shell script grammar is also the same. Routine.

1. Variable

1.1 custom variables

Custom variables in the script are relatively simple, look at the code

#!/bin/bash
#定义变量
HELLO="hello"
echo -e "Hello World!\n"
echo $HELLO
exit 0

A variable HELLO is defined, its value is "hello", when referencing the variable, it is obtained through the $variable name

1.2 The calling script is the parameter variable passed in

When calling a shell script, you can pass in some parameters to enter the shell script. Let's modify the shell script first

#!/bin/bash
#这是个demo
echo -e "传入的参数个数 $# \n"
echo -e "传入的参数是$@ \n"
echo -e "第0个参数是$0 \n"
echo -e "第一个参数是$1 \n"
exit 0

Call the script./test.sh 1 2 3, the output result is as follows

Number of parameters passed in 3

The incoming parameters are 1 2 3

The 0th parameter is ./test.sh

The first parameter is 1

$# represents the number of parameters following the shell script file, 1 2 3 has three parameters, so the output is 3;

$@ can get all input parameters

$0 represents the shell file name

$1 $2 $3...You can get the corresponding 1 2 3 in turn, which is used to get each parameter separated by a space.

2. Conditional statement

2.1 if statement

The if statement is a common judgment statement, the basic syntax is as follows

#!/bin/bash
#这是个demo
# if [  ];then
# 语句
# elif [  ];then
# 语句
# else
# 语句
# fi
if [ $1 == 1 ];then
	echo -e hello one
elif [ $1 == 2 ];then
    echo hello two
else
    echo hello three
fi    

What needs to be noted here is the space, if space [space $1 space == space 1 space]; then basically there are spaces between if and the judgment symbol ([]) and in the judgment symbol, so don't miss it.

2.2case...esac statement

This is similar to the switch case statement in a high-level language.

#!/bin/bash
#这是个demo
# case $变量名称 in
# 	值1)
#     语句
#     ;;
#     值2)
#     语句
#     ;;
#     *)
#     语句
#     ;;
# esac     
case $1 in
	1)
  echo one
    ;;
    2)
  echo two
    ;;
    *)
  echo three
    ;;
esac

One thing to note is *) Just like default in java swtich, it can match any value.

3. Loop statement

3.1while do done和until do done

Among them, wile represents when the condition is met, the loop is performed, and it stops until the condition is not met. And until is the opposite of while, it means that when the condition is met, the loop is terminated.

#!/bin/bash
#这是个demo
# while [ condition ]
# do
# 	程序段落
# done


# until [ condition ]
# do
#     程序段落
# done    	
Num=$1
while [ $Num != 3 ]
do
	echo $Num
	((Num=Num+1))
done
echo 3
exit 0	

3.2 for loop

There are two ways to write for loop, the first is as follows

#!/bin/bash
#这是个demo
# for var in con1 con2 con3
# do
# 	程序段
# done	
for var in  1 2 3
do
	echo $var
done	

The second

#!/bin/bash
#这是个demo
# for (( 初始值; 限制值; 步长))
# do
# 	程序语段
# done	
for (( i=1; i<3; i++))
do
	echo $i
done	

4. Function

#!/bin/bash
#这是个demo
# function fname(){
# 	程序段
# }
function print(){
	echo "paramer 1 is $1 2 is $2"
}

print a b

You can see that there are $1 and $2 variables in the function. These variables are different from the parameter variables that are passed in to call the script defined before. Here, $1 and $2 represent the passed-in parameters, which need to be passed when calling the function. When entering the parameters, just pass them directly after the space in the parameter name, separated by spaces.

4. Reference

"Bird Brother's Linux Private Kitchen"

image
Follow my public account

Guess you like

Origin blog.csdn.net/skateboard1/article/details/105180768