当前位置:网站首页>Shell script series (1) getting started
Shell script series (1) getting started
2022-06-27 01:53:00 【liyuanjie8521】
Shell Script entry
1. Shell Concept
Shell Is a command interpreter , The function is to explain and execute the commands and programs entered by the user , Every command the user enters ,Shell Just explain and implement one of them . This kind of input commands from the keyboard , You can get an immediate response , It is called the way of interaction .
Shell Scripting language is a weakly typed language ( You don't need to define the type of the variable to use ).
Check out the shell Support
- Centos7.8
$ cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
- ubuntu18.04
$ cat /etc/shells
/bin/sh
/bin/bash
/bin/rbash
/bin/dash
2 Shell Scripting language advantages
- Shell Scripting language is more suitable for dealing with the business at the bottom of the operating system .
- Linux Regular expressions and three swordsmen grep、awk、sed Wait for the order .
- Use Shell More in line with Linux Simple operation and maintenance 、 Easy to use 、 Three basic principles of efficiency .
3. Common operating systems default Shell
CentOS Linux By default Shell yes :
bashsee CentOS Linux By default Shell
# echo $SHELL
# grep root /etc/passwd
1.4 Shell Script format
- Check out the shell edition
# bash --version
- upgrade bash edition
# yum -y update bash
- At the beginning of the script ( first line ) : The first line indicates which program interpreter executes the script content
#! /bin/bash
Script comment
- # The following content indicates the comments , Used to annotate scripts
- Try not to use Chinese for notes , It is better not to have Chinese in the script
- Shell The script is from top to bottom 、 From left to right, execute each line of commands and statements in turn , That is, after executing one command, execute the next .
- In execution Shell Script time , Will request the system kernel to start a new process , In order to execute the script's commands and subprograms in the process Shell Script .
Shell Script execution
1. bash xxx.sh
2. chmod +x xxx.sh then ./xxx.sh
3. source xxx.sh Use source Or point number to read or load the specified shell Script files .
Conclusion :
adopt source or . Load the executed script , Because in the present Shell Execute script in , So after the script ends , Variables in scripts ( Include function ) Value in current Shell There is still .
sh and bash Executing the script will start a new child Shell perform , After execution, return to the parent Shell. therefore , Variable ( Include function ) Values, etc. cannot be reserved .
It's going on Shell Script development , If there is a need to reference or execute the contents of other scripts or configuration files in the script , It is best to . or source Load the script or configuration file first , After that , Then load them under the script , You can call source The loaded script and variables and functions in the configuration file .
- Use sh Executing the script will cause the current Shell Unable to get variable value .
1.5 Shell Basic specifications and habits of script development
shell The script normalization format is as follows
#!/bin/bash
set +o posix
#: <<'COMMENT'
#'''
#@File : xxxxx.sh
#@Time : 2021/12/20 14:03:14
#@Modify : 2021/12/20 16:13:14
#@Author : Li yuanjie
#@Version : 1.1
#@Contact : [email protected]
#@Desc : Project shell scripts
#'''
#COMMENT
# Commissioning switch
# set -x
#set -euxo pipefail
## -------------------------------------------------------------------------
export PATH=/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin
export TERM="xterm-256color"
export WORKDIR="$(cd $(dirname '$0') && pwd )"
cd "${WORKDIR}" || exit 1
## -------------------------------------------------------------------------
info() {
date +"%F %T $( tput bold ; tput setaf 2)Info: [email protected]$( tput sgr0)"
}
warn() {
date +"%F %T $( tput bold ; tput setaf 3)Warning: [email protected]$( tput sgr0)"
}
error() {
date +"%F %T $( tput bold ; tput setaf 1)Error: [email protected]$( tput sgr0)"
}
err_exit() {
date +"%F %T $( tput bold ; tput setaf 1)Error: [email protected]$( tput sgr0)"
exit 1
}
Shell The first line of the script specifies the script interpreter
#!/bin/bashShell The script starts with a version , Copyright and other information
stay Shell Try not to use Chinese in the script .
Shell The name of the script should be
.shExtension name .Shell Scripts should be stored in a fixed path .
Code writing habits
- Pairs of symbols should be written at once , Then backspace and add content to the symbol , To prevent omission .
- brackets
([])At least... At both ends 1 A space . - For process control statements , The format should be written once , Add content .
- Make the code easier to read by indentation
- For the string definition of a regular variable, the variable value should be enclosed in double quotation marks , And there should be no spaces before and after the equal sign , Strong references are required ( Refers to WYSIWYG character references ), Use single quotation marks
(' '), If it is a reference to a command , Use back quotation marks . - Single quotation marks in scripts 、 Double quotation marks and back quotation marks must be symbols in English .
边栏推荐
- 执念斩长河暑期规划
- Hibernate generates SQL based on Dialect
- How does the C # TCP server limit the number of connections to the same IP?
- 1.44 inch TFT-LCD display screen mold taking tutorial
- 为什么传递SPIF_SENDCHANGE标志SystemParametersInfo会挂起?
- 在连接数据库的时候遇到了点问题,请问怎么解决呀?
- Oracle/PLSQL: Lower Function
- Oracle/PLSQL: NumToYMInterval Function
- Why pass SPIF_ Sendchange flag systemparametersinfo will hang?
- SystemVerilog仿真速率提升
猜你喜欢

Some exception handling for idea plug-in development

Recursion will make strtok more attractive

JVM 的指针压缩

Pointer compression for JVM

WiFi-IoT 鸿蒙开发套件样例开发

SQLite Reader 插件测试SQLite语法

The listing of Fuyuan pharmaceutical is imminent: the net amount raised will reach 1.6 billion yuan, and hubaifan is the actual controller

二叉樹oj題目

Binary tree OJ problem

uvm中的config机制方法总结(二)
随机推荐
消费者追捧iPhone,在于它的性价比超越国产手机
Oracle/PLSQL: Rpad Function
TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
ThreadLocal详解
memcached基础13
Oracle/PLSQL: CharToRowid Function
JVM 的指针压缩
Some exception handling for idea plug-in development
Reporting Classes中uvm_report_server的get_severity_count和get_server用法
Why divide the training set and the test set before normalization?
Memcached basics 15
Oracle/PLSQL: Upper Function
CVPR2022 | PointDistiller:面向高效紧凑3D检测的结构化知识蒸馏
Oracle/PLSQL: From_ Tz function
学习DiscoDiffusion的最基础操作
Oracle/PLSQL: Ltrim Function
Simply learn the entry-level concepts of googlecolab
memcached基础10
Detailed explanation of ThreadLocal
Meituan: data management and pit avoidance strategy summarized after stepping on Thunder for several years