shell script from scratch

What is a shell script

A script is a combination of many commands and followscommand sequenceandScript Format and Script SyntaxThe realization of a requirement has been completed

what the script can do

  • Automated software deployment LAMP/LNMP/Tomcat…
  • Automatically manage system initialization scripts, change host passwords in batches, push public keys...
  • Automated analysis and processing statistics of website visits
  • Automatic backup database backup, log dump

Basic writing of shell script

The first part of the shell script (the first line), the magic character #!specifies the interpreter [required]

#!/bin/bash means that the following content is parsed using the bash interpreter

If you directly write the interpreter path in the script, there may be a compatibility problem that the interpreter cannot be found in some systems, so you can use: #!/bin/bash interpreter

The second part of the shell script, use (#) instructions to describe the basic content of the script (optional)

#!/bin/bash

#以下内容为对脚本的注释内容
#name:名字
#path: 路径

The third part of the shell script, the code to realize the content of the script

#!/bin/bash

#以下内容为对脚本的注释内容
#name:名字
#path: 路径

echo 'hello world'

tip: You can use bash -n xxx.sh to check the syntax of the script.
Standard execution method./xx.sh The standard execution method requires permission. The
non-standard execution method of the script source xxx.sh

The simplest script is a stack of commands

#!/bin/bash

#以下内容为对脚本的注释内容
#name:名字
#path: 路径

rm -rf /tmp/*
mkdir /tmp/dir{
    
    1..3}
cp /etc/hosts /tmp/dir1
echo '简单脚本于$(date +%F%T)执行完毕,请检查'

variable

variable definition

Variables are used to store data that can change

How variables are defined

变量名=变量值

name=LiBai
echo $name

name=Tom
echo $name  #调用变量使用echo

The result of the first output is LiBai, and the result of the second output is Tom, because the second assignment will overwrite the first assignment

Variable Definition Rules

  • Variable names are case sensitive
  • Variable names cannot have special symbols
  • Variable names cannot start with a number but can contain numbers
  • There cannot be any spaces on either side of the equal sign
  • Variables should be as familiar as possible
slice in variable
A=0123456

echo ${A:2:4}  #表示把变量A进行切片,表示从第三个字符进行截取,截取到第4个字符结束
Assign the execution result to a variable
A=$(hostname)  或者使用反撇号  A=`hostname`
Define variables interactively (read)

Purpose: to letUser input variable assignment, more flexible
Syntax: read [option] variable name
选项

-pDefine the information to prompt the user
-nDefine the number of characters (limit the length of the variable value)
-sDo not display (do not display the content entered by the user)
-tDefine the supermarket time, the default unit is seconds (limit the timeout period for the user to input the variable value)

Usage one:

read -t 3 -p 'name' name   #超时时间为3s  提示信息为name  变量名称为 name

Usage 2: variable value comes from file

read IP < ip.txt   #变量IP来自于文本文件ip.txt
Define a typed variable (declare)

Purpose: Make some restrictions on variables and fix the types of variables. For example: integer, read-only
Syntax: declare [Option] variable name = variable value
选项

-iTreat variables as integers, and only integers can be assigned in the future
-rDefine read-only variables, and the value of this variable cannot be changed in the future
-aDefine ordinary arrays, view ordinary arrays
-ADefine associated data, view associative arrays
-xDefine environment variables, which is equivalent to export explain

local variable

Local variables: variables defined by ourselves, which can only be used by the current process

environment variable

Environment variable: the current process can be called, and the child process can also be called

env:: View environment variables
set: View local variables and environment variables
export: Extract local variables as environment variables

A='qwe'   #定义本地变量
export A   #提取本地变量为环境变量export B='QWE' 
global variable

Global variables: all users and all processes can call and can be inherited

$HOME/.bashrc Current bash information, read
$HOME/.bash_profile current user environment variable when user logs in, read $HOME/.bash_logout current bash information when user logs in, read /etc/bashrc global
for user exit current shell information
The bash information, effective for all users
/etc/profile global environment variable information
以上信息修改完都需要source执行一下

file read order

  1. /etc/profile
  2. $HOME/.bash_profile
  3. $HOME/.bashrc
  4. /etc/bashrc
  5. $HOME/.bash_logout

In general, the local configuration configuration file is better than the global configuration file, but the configuration file is a loop statement, and the writing position is different and the effect is different. If you are interested, you can check it yourself.

Built-in variables (the shell itself has been defined and cannot be modified)
built-in variables meaning
$? Returns the return status after the last command is executed, returning 0 means successful execution, non-zero means execution exception or error
$0 The currently executing program and script name
$# The number of parameters connected to the script
$* All parameters after the script, the parameters are treated as a whole, and each parameter is separated by a space
$@ All parameters behind the script, the parameters are independent, all output
$1-$9 $1 represents the first parameter and so on, which is a positional parameter
$$ The process ID of the current process
$! The last process number running in the background (current window)
!$ Call the parameters in the last history command

Guess you like

Origin blog.csdn.net/qq_41158271/article/details/113924472