当前位置:网站首页>Shell script learning day01
Shell script learning day01
2022-07-27 07:53:00 【yygyj】
1、 first Shell Program
1.1 establish Shell file
command :touch hello.sh
1.2 To write Shell Program
Definition file declaration
#!/bin/bash
echo "Hello word"
#!: Indicates the Convention mark , He will tell the system what kind of interpreter this script needs to execute , It is a kind of script embodiment
echo: Indicates that the command is used to output text information
1.3 Modify file permissions
View permissions for each part command ls -l hello.sh
Modify file permissions chmod +x ./hello.sh
1.4 perform Shell Script files
command : ./hello.sh
2、Shell Script Syntax notes
“#” Notation
Be careful : stay Shell Script , No multiline comments , Only single line comments
3、Shell Scripting language - Variable
3.1 Variable definitions - matters needing attention
Attention two : Variable names and equal signs cannot have spaces ( It is different from our general language )
Note three : The first letter of the variable name must be a letter or an underscore
Notice four : Spaces are not allowed in the middle of variable names
Pay attention to five : Punctuation is not allowed
3.2 A read-only variable
keyword :readonly( read-only , Can't modify )
3.3 Delete variables
grammar :unset
Case study :unset Variable name
3.4 Variable type
3.4.1 Type 1 : The local variable
Scope the whole bash The process can use
grammar :name="smile2018"
3.4.2 Type 2 : local variable
Scope : The current code snippet ( Modifier :local)
3.4.3 Type 3 : environment variable
Scope : At present shell Processes and sub processes
grammar :export name=“hello”
3.4.4 Type four positional variables
name=${
0}
age=${
1}
sex=${
2}
echo " full name :${name} Age :${age} Gender :${sex}"
Execute the script :
./hello.sh tom 23 male
Execution results : full name :./hello.sh Age :tom Gender :23
${
0} Indicates the name of the script file Parameter from 1 Start
3.4.5 Special variables
${
0}- Special variables - File name
${
?} Indicates the return value of the execution status of the previous command
0: Indicates successful execution
1: Program execution result
2: Indicates the program status return code (0-255)
System reservation error (1,2,127)
echo ${
0}
echo ${
?}
$# - Number of parameters
$* - parameter list
[email protected] - parameter list
$$ - Go back to the current shell Conduct ID
$! - Execute last instruction PID
$* and [email protected] difference
$* parameter list : Combine all the parameters into a string
[email protected] parameter list : String separation
4、Shell Scripting language - character string
4.1 character string - Single quotation marks
4.2 character string - Double quotes
4.3 character string - Splicing
Script code
name="Andy"
age=100
sex=" male "
info="${name}${age}${sex}"
echo ${
info}
Execution results
Andy100 male
Script code
name="Andy"
age=100
sex=" male "
info=" full name :"${
name}" Age :"${
age}" Gender :${sex}"
echo ${
info}
Execution results
full name :Andy Age :100 Gender : male
4.4 character string - Get string length
Grammatical structure :${
# Variable name }
Script code
name="Andy"
echo ${
#name}
Script results
4
4.5 character string - Intercept
grammar :${
Variable name : Starting position : Interception length }
Script code
Case study 1: Intercept from the fourth string , Intercept 3 individual
name="my name is XQ"
result=${
name:3:3}
echo ${
result}
Case study 2: From the string No 5 The first one starts to intercept , To the last end
The way 1:
name="my name is XQ"
length=${
#name}
result=${
name:4:length-1}
echo ${
result}
The way 2:
name="my name is XQ"
result=${
name:4}
echo ${
result}
4.6 character string - Delete
Grammar 1 :$( Variable name # Delete string Regular expressions )
effect : Start with a string ( On the left ), Start matching the string to be deleted
Case study 1:
name="my name is XQ"
result=${
name#XQ}
echo ${
result}
reason : It doesn't match
Case study 2:
name="my name is XQ"
result=${
name#my}
echo ${
result}
Case study 3: Find the first of the specified characters , And delete all the previous characters ( Include yourself )
name="my name is XQ"
result=${
name#*X}
echo ${
result}
Grammar II : $( Variable name ## Delete string Regular expressions )
effect : From the end of string ( On the right ) Start matching the string to be deleted
Case a
name="my name is XQ"
result=${
name##*X}
echo ${
result}
summary : grammar 1 And grammar 2, Are deleted from the left to the right , The search direction is different
Grammar 3 :${
Variable name % Delete string Regular expressions }
Match from the end of the string , Delete the matching string
Case study 1: Find the first character ( Match the first )
Code 1 :
name="my name is XQ"
result=${
name%Q}
echo ${
result}
Case study 2: Find the first of the specified characters , And delete all the previous characters ( Include yourself )
name="my name is XQ"
result=${
name%m*}
echo ${
result}
Grammar 4 :$( Variable name %% Delete string Regular expressions )
Case study 1: Find the last of the specified character , And delete all the previous characters ( Include yourself )
name="my name is XQ"
result=${
name%%m*}
echo ${
result}
summary : Delete from left to right
#- Indicates that the query direction is from left to right
##- Indicates that the query direction is from right to left
Delete from right to left
%- Indicates that the query direction is from right to left
%%- Indicates that the query direction is from left to right
边栏推荐
- C#winform 窗体事件和委托结合用法
- C语言:随机生成数+插入排序
- shell 函数和数组练习
- C语言:优化后的希尔排序
- User unlock sm04 sm12
- Confluence vulnerability learning - cve-2021-26084/85, cve-2022-26134 vulnerability recurrence
- ADC噪声全面分析 -02- ADC 噪声测量方法和相关参数
- Shell functions and arrays exercises
- 【小程序】如何获取微信小程序代码上传密钥?
- Showdoc vulnerability learning - cnvd-2020-26585 (arbitrary file upload)
猜你喜欢
随机推荐
小程序支付管理-新版支付对接流程
Debug:与泛型有关的“无法解析的外部符号”
C语言:随机生成数+希尔排序
HU相关配置
IDEA中文乱码怎么办
C event usage case subscription event+=
[day42 literature intensive reading] a Bayesian model of perfect head centered velocity during smooth pursuit eye movement
杂谈:手里有竿儿,肩上有网,至于背篓里有多少鱼真的重要吗?
Gossip: is rotting meat in the pot to protect students' rights and interests?
【万字长文】吃透负载均衡,和阿里大牛的技术面谈
Abstract factory pattern
C#委托的使用案例
RPC remote procedure call
Promise详解
瑞芯微RK3399-I2C4挂载EEPROM的修改案例
shell循环练习
2020国际机器翻译大赛:火山翻译力夺五项冠军
帮个忙呗~不关注不登录,不到一分钟的一个问卷
RPC 远程过程调用
反弹shell是什么?反弹shell有什么用?









