当前位置:网站首页>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
边栏推荐
- 【kotlin】好看的弹出框、自定义弹出框(对话框)、扩展函数、菊花等待条、消息提示框
- 论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
- Redis+aop+ user defined annotation to realize flow restriction
- On the necessity and solution of building a campus online teaching video convergence platform
- Websocket for im instant messaging development: concept, principle and common sense of mistakes
- Pytorch builds transformer to realize multivariable and multi step time series forecasting (load forecasting)
- Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
- LeCun预言AGI:大模型和强化学习都是斜道!我的世界模型才是新路
- Is it safe to open a stock account online?
- 数学知识:求组合数 I—求组合数
猜你喜欢

windows mysql5.7 开启binlog日志

深入虚拟内存(Virtual Memory,VM)

Leetcode detailed explanation of stack type

How powerful is the Zadig build? Practice together

Get to know Alibaba cloud (Cloud Computing) - development history, technical architecture, region and availability zone!

Zadig officially launched vs code plug-in, making local development more efficient

Linux安装mysql5.7(CentOS7.6) 教程

Pytorch builds transformer to realize multivariable and multi step time series forecasting (load forecasting)

On the necessity and solution of building a campus online teaching video convergence platform

C#/VB. Net to convert PDF to excel
随机推荐
Career consultation | what should I answer when I am asked about my intended salary during the interview?
SqlServer复习
The love digital smart 2022 summit opens, sharing data strategy and building data-driven organization methodology
What is the prospect of VR panoramic production?
【SSH】无密码登录
A password error occurred when docker downloaded the MySQL image to create a database link
【HackTheBox】 meow
Career consultation | how to answer the question of career planning during the interview?
What is low code development?
论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
How to use London gold to draw support resistance line
Wechat red envelope cover making tutorial and use guide with link jump
Redis+AOP+自定义注解实现限流
QtCreator5.15.0源码编译全过程记录
leetCode-栈类型详解
What is the difference between WMS warehouse management system and ERP
Steady! How thousands of micro services can quickly access Zadig (helm chart)
C#/VB. Net to convert PDF to excel
计数排序的简单理解
数学知识:求组合数 I—求组合数