当前位置:网站首页>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
边栏推荐
- MySql——存储引擎
- Global and Chinese market of digital shore durometer 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of blackbody calibration source 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets for waste treatment air switches 2022-2028: Research Report on technology, participants, trends, market size and share
- Random function usage notes
- Global and Chinese market of resistivity meter 2022-2028: Research Report on technology, participants, trends, market size and share
- Use stm32cubemx tool to write the demo program of FreeRTOS
- A series of problems in offline installation of automated test environment (ride)
- Apple input method optimization
- L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!
猜你喜欢
[neo4j] common operations of neo4j cypher and py2neo
Opendrive ramp
High end electronic chips help upgrade traditional oil particle monitoring
SQL JOINS
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
Beijing Winter Olympics opening ceremony display equipment record 3
Altium Designer 19.1.18 - 隐藏某一个网络的飞线
Numpy——1.数组的创建
Altium designer 19.1.18 - clear information generated by measuring distance
A simple method to prove 1/t Fourier transform
随机推荐
Global and Chinese market of blackbody calibration source 2022-2028: Research Report on technology, participants, trends, market size and share
Use of orbbec Astra depth camera of OBI Zhongguang in ROS melody
How to migrate the device data accessed by the RTSP of the easycvr platform to easynvr?
Apple system shortcut key usage
Detailed explanation of C language pointer
导电滑环磨损快的原因
Leetcode solution - number of islands
Record the opening ceremony of Beijing Winter Olympics with display equipment
Temperature sensor DS18B20 principle, with STM32 routine code
Embedded AI intelligent technology liquid particle counter
Numpy——1.数组的创建
C language uses arrays to realize the intersection, union, difference and complement of sets
solver. Learning notes of prototxt file parameters
Global and Chinese markets for recycled boilers 2022-2028: Research Report on technology, participants, trends, market size and share
About the problem that MySQL connector net cannot be cleared in MySQL
NSIS finds out whether the file exists and sets the installation path
Ads learning record (lna_atf54143)
Nombre - 1. Création de tableaux
Cadence simulation encountered "input.scs": can not open input file change path problem
MySQL - storage engine