当前位置:网站首页>Understand shell script in one article
Understand shell script in one article
2022-06-28 22:54:00 【yida&yueda】
Article, understand shell Script
1 、shell Script Introduction
- What is? shell Script
A series of shell Collection of commands , You can also add some logical operations (if else for) Put these commands in a file .
What is? shell command ?
ls
pwd
cd
chmod +x test.sh
shell The basic format of the script
Naming format
- General naming rules : xxxxx.sh ( It is suggested that .sh Name the suffix )
Writing format
# test.sh # yes shell Comments in the script # The first line if you forget to write , Use the default command parser /bin/sh #!/bin/bash # Specify resolution shell The command parser used in the script /bin/sh It's fine too # A series of shell command ls pwd cp rm
shell Script execution
# perform shell Script ./xxx.sh sh test.sh
2、 shell Variables in scripts
Common variables ( The local variable )
# Defining variables , Definition complete , You have to assign , = No spaces before and after temp=666 # Normal variables can only be used in the current processenvironment variable —— In general capital
# It is understandable to ask global variables , It can be accessed globally in the current operating system # classification - System native - PWD - SHELL - PATH - HOME - User defined - Promote normal variables to system level environment variables GOPATH=/home/zoro/go/src - > Common environment variables set GOPATH=/home/zoro/go/src - > System environment variable export GOPATH=/home/zoro/go/src ~/.bashrcPositional variable
When executing a script , You can pass parameters to a script , Receive these parameters inside the script , You need to use position variables
# Existing scripts test.sh
#!/bin/bash
echo "hello , world, $0"
echo " The first parameter : $1"
echo " The first 2 Parameters : $2"
echo " The first 3 Parameters : $3"
echo " The first 4 Parameters : $4"
echo " The first 5 Parameters : $5"
echo " The first 6 Parameters : $6"
# perform test.sh
$ ./test.sh 11 22 3 4 5 6 aa bb
hello , world, ./test.sh
The first parameter : 11
The first 2 Parameters : 22
The first 3 Parameters : 3
The first 4 Parameters : 4
The first 5 Parameters : 5
The first 6 Parameters : 6
- Special variables
- $#: Get the number of parameters passed
- [email protected]: All parameters passed to the script
- $?: The status after the script execution is completed , Failure >0 or success =0
- $$: The corresponding process after the script process is executed ID
# test.sh
#!/bin/bash
echo "hello , world, $0"
echo " The first parameter : $1"
echo " The first 2 Parameters : $2"
echo " The first 3 Parameters : $3"
echo " The first 4 Parameters : $4"
echo " The first 5 Parameters : $5"
echo " The first 6 Parameters : $6"
echo " Number of parameters passed : $#"
echo " All parameters passed : [email protected]"
echo " The process of the current script ID: $$"
$ ./test.sh aa bb cc dd ee ff 8 9 0
hello , world, ./test.sh
The first parameter : aa
The first 2 Parameters : bb
The first 3 Parameters : cc
The first 4 Parameters : dd
The first 5 Parameters : ee
The first 6 Parameters : ff
Number of parameters passed : 9
All parameters passed : aa bb cc dd ee ff 8 9 0
The process of the current script ID: 47946
# Script execution status view
$ echo $?
0 -> success
Not 0 -> Failure
- Common variable value
# Variable definitions
value=123 # By default, it is processed as a string
value1 = "123 456"
echo $value
# How to get the value of a variable :
- $ Variable name
- ${ Variable name }
Take the result value after the command is executed
# There are two ways to take value : var=$(shell command ) var=`shell command `Use of quotation marks
# Double quotes echo " Current file : $var" - When printing, it will var Take out the value in and output # Single quotation marks echo ' Current file : $var' - Output the string as it is
3 、 Condition judgment and cycle
shell Script if conditional
# if sentence # matters needing attention : - if and [] There is a space directly - [ Conditions ] : There are spaces before and after the condition - else if => elif - if [ conditional ];then Logical processing -> shell command xxx xxxx xxxx fi # =================== if [ conditional ] then Logical processing -> shell command xxx xxx fi # if ... elif .. fi if [ conditional ];then Logical processing -> shell command xxx xxxx xxxx elif [ conditional ];then shell command elif [ conditional ];then shell command elif [ conditional ];then shell command else shell command fi# if.sh #!/bin/bash # You need to judge the file name passed into the script if [ -d $1 ];then echo "$1 Is a directory !" elif [ -s $1 ];then echo "$1 It's a document , And the file is not empty " else echo "$1 Not a directory , There may not be , Or the file size is 0" fi- shell Script for loop
# shell The cycle in for/ while # grammar : for Variable in aggregate ; do;done for var in aggregate ;do shell command done# for.sh #!/bin/bash # Traverse the files in the current directory list=`ls` for var in $list;do echo " Current file : $var" echo ' Current file : $var' done
4、shell Functions in scripts
# No function modification , No parameters , no return value
# Format
funcName(){
# Get the first parameter
arg1=$1
# Get the first 2 Parameters
arg2=$2
The body of the function -> shell command + Logic loops and judgments
mkdir /root/abc
}
# No parameter list , But you can pass it on
# Function call
funcName aa bb cc dd
# The state after the function call :
0 -> Successful call
Not 0 -> Failure
#!/bin/bash
# Determine whether the file name passed is a directory , If not , establish ...
# Defined function
is_directory()
{
# Get the filename , Get the file name through the parameter
name=$1
if [ -d $name ];then
echo "$name Is a directory !"
else
# Create directory
mkdir $name
if [ 0 -ne $? ];then
echo " Directory creation failed ..."
exit
fi
echo " Directory created successfully !!!"
fi
}
# Function call
is_directory $1
边栏推荐
- 嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
- 在长投学堂开通证券账户是安全可靠的吗?
- 微搭低代码中实现二维码生成
- 【kotlin】好看的弹出框、自定义弹出框(对话框)、扩展函数、菊花等待条、消息提示框
- Career consultation | in the data analysis interview, it is only reliable to introduce yourself in this way
- Is it safe and reliable to open a securities account in changtou school?
- Sample code of using redis to realize the like function
- Advanced workplace | understand the "entry" of position advantages
- 全面掌握const的用法《一》
- CS5463代码模块解析(包含下载链接)
猜你喜欢

在线文本过滤小于指定长度工具

Zadig + 洞态 IAST:让安全溶于持续交付

How to use London gold to draw support resistance line

Post-00 cloud native Engineer: use Zadig to increase revenue and reduce expenditure for the R & D of Sichuang Technology (Guangzhou public transport)
Sample code of using redis to realize the like function

Ansible production environment usage scenario (7): batch deployment of elk clients

如何结合均线分析伦敦金行情走势线图

Wave picking of WMS warehouse management system module

时间序列预测系列文章总结(代码使用方法)

What does project management really manage?
随机推荐
The example application of histogram in data visualization makes the public performance results clear at a glance
The technology giants set up the meta universe standard forum to open up or build a besieged city?
【kotlin】好看的弹出框、自定义弹出框(对话框)、扩展函数、菊花等待条、消息提示框
LeCun预言AGI:大模型和强化学习都是斜道!我的世界模型才是新路
TCP three handshakes and four waves
The Best of Many Worlds_ Dual Mirror Descent for Online Allocation Problems
Database basic notes
code review
[SSH] login without password
网上办理股票开户安全性高吗?
What is the difference between WMS warehouse management system and ERP
【Try to Hack】nmap
How powerful is the Zadig build? Practice together
在QT进行cin(全网最清晰教程)
Windows mysql5.7 enable binlog log
直击产业落地 | 飞桨重磅推出业界首个模型选型工具
一文读懂,WMS仓储管理系统与ERP有什么区别
The Best of Many Worlds_ Dual Mirror Descent for Online Allocation Problems
Zadig + cave Iast: let safety dissolve in continuous delivery
flowable 边界定时器