当前位置:网站首页>Shell脚本基本语法
Shell脚本基本语法
2022-07-05 07:50:00 【群野】
Mac电脑下Bash,是Unix shell的一种,开发者通过shell脚本中间件调用系统内核,所以苹果开发者很有必要学习一下。
1创建和输出
1-1创建Shell文件
命令:touch hello.sh
1-2定义文件声明
#!/bin/bash
#!:表示约定标记,他会告诉系统这个脚本需要什么样子的解释器来执行,既是一种脚本体现。'#'注释,注意:在Shell脚本中,没有多行注视,只有单行注释 。
echo “Hello world!”
1-3执行Shell脚本hello文件
命令:./hello.sh
查看和修改文件权限
查看命令:ls -l hello.sh
修改命令: chmod +x ./hello.sh
-rwxr-xr-x
1-4 变量操作
- 赋值不要有空格
name="Kenny"
不能写成name = "Kenny"
变量名首字母必须是‘a-z’或‘A-Z’见的字母, 只读变量不能被修改,审美readonly name
- 删除变量
unset name
- 特殊变量
${0}
:文件名称${?}
:表示返回上一个命令执行状态返回值$#
:参数个数$*
:参数列表,组合字符串[email protected]
:参数列表$$
:后去当前shell进行ID$!
:执行上一个指令PID
1-5 字符串拼接、截取 、删除
- 拼接
#!/bin/bash
name='Andy'
age=100
sex="男"
info=" 姓名:'${name}' 年龄:'${age}' 性别:'${sex}' "
echo ${info}
- 截取
变量名:起始:结束,结果’have’
name="I have a Dream"
result=${name:2:4}
echo ${result}
求变量长度length=${#name}-1
结束位置可以不写,表示到结束
name="I have a Dream"
result=${name:5:length-1}
echo ${result}
- 删除
#语法一:${变量名#删除字符串 正则表达式}
从第一个开始删除##语法二:${变量名##删除字符串 正则表达式}
从最后一个开始删除%语法三:${变量名%删除字符串 正则表达式}
查找指定字符第一个,并且删除前面所有字符(包含自己)%%语法三:${变量名%%删除字符串 正则表达式}
查找指定字符最后一个,并且删除前面所有的字符(包含自己)
删除范围:从左边删除到右边
#->表示查询方向从左到右
%->表示查询方向从右到左
删除范围:从右边删除到左边
##->表示查询方向从右到左
%%->表示查询方向从左到右
案例一:查找指定字符第一个,并且删除后面所有的字符(包含自己)
#
从左边开始找第一个,并且删除前面所有字符(包含自己)
name="I have a Dream"
result=${name#*a}
echo ${result}
结果
ve a Dream
##
从右边开始找第一个,并且删除前面所有字符(包含自己)
name="I have a Dream"
result=${name##*a}
echo ${result}
结果
m
案例二:查找指定字符第一个,并且删除后面所有字符(包含自己)
%
从右边开始找第一个,并且删除后面所有字符(包含自己)
name="I have a Dream"
result=${name%a*}
echo ${result}
结果
I have a Dre
%%
从左边开始找第一个,并且删除后面所有字符(包含自己)
name="I have a Dream"
result=${name%%a*}
echo ${result}
结果
I h
边栏推荐
- QT's excellent articles
- Butterfly theme beautification - Page frosted glass effect
- Leetcode solution - number of islands
- Batch modify the txt file code to UTF-8 (notepad++)
- Embedded AI intelligent technology liquid particle counter
- 数字孪生实际应用案例-风机篇
- [neo4j] common operations of neo4j cypher and py2neo
- The printer encountered an abnormal configuration problem 0x8007007e (win10)
- IC software learning
- Use of orbbec Astra depth camera of OBI Zhongguang in ROS melody
猜你喜欢
Altium Designer 19.1.18 - 隐藏某一个网络的飞线
LED display equipment records of the opening ceremony of the Beijing Winter Olympics
Altium designer 19.1.18 - hide the fly line of a network
SQL JOINS
Ten thousand words detailed eight sorting must read (code + dynamic diagram demonstration)
Numpy——1. Creation of array
Nombre - 1. Création de tableaux
Latex notes
Practical application cases of digital Twins - fans
MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
随机推荐
Altium designer 19.1.18 - Import frame
Global and Chinese markets for anesthesia, breathing and sleep apnea devices 2022-2028: Research Report on technology, participants, trends, market size and share
Practical application cases of digital Twins - fans
About the problem that MySQL connector net cannot be cleared in MySQL
Query the table name used by kettle in Oracle
The printer encountered an abnormal configuration problem 0x8007007e (win10)
Apple modify system shortcut key
Train your dataset with yolov4
[popular science] some interesting things that I don't know whether they are useful or not
Apple animation optimization
Apple system shortcut key usage
Exit of pyGame, idle and pycharm
Global and Chinese markets of large aperture scintillators 2022-2028: Research Report on technology, participants, trends, market size and share
Pointnet++ classification practice
What is deep learning?
Altium designer 19.1.18 - hide the fly line of a network
Apple terminal skills
How to excavate and research ideas from the paper
Calibre garbled
A complete set of indicators for the 10000 class clean room of electronic semiconductors