当前位置:网站首页>Shell脚本编程
Shell脚本编程
2022-07-06 09:26:00 【星时代曹波涛】
Shell编程
偶尔在Linux环境中要写一些简单的Shell脚本,本文章可作参考,写出一些简单的脚本。
语法规范
定义执行环境
#!/usr/bin/bash
或者
#!/usr/bin/env bash | python |perl
执行
修改权限
chmod +x ./demo.sh
或者
chmod 700 ./demo.sh
./demo.sh
或者
bash ./demo.sh
sh ./demo.sh
# 通过bash sh 的方式不需要修改权限,指定执行环境即可
特殊符号
符号 | 解释 |
---|---|
~ | 家目录 |
- | 上一级目录 |
!! | 执行上一条命令 |
$ | 获取环境变量${USER} |
±*/% | 运算符 |
* | 通配符 多个 |
? | 一个 |
| | 管道符 |
\ | 转义字符 |
> | 重定向(echo abc > ./demo.txt) |
>> | 重定向(echo abc >> ./demo.txt)追加 |
< | wc < abc.txt |
<< |
注释
:<<EOF
注释内容...
注释内容...
注释内容...
EOF
变量
# 定义变量
name="zhangsan"
age=18
# 只读
readonly name
# 删除变量
unset age
# 访问
echo ${name}
# 获取字符串长度
echo ${
#name}
# 提取子字符串
echo ${string:0:2} # 输出 unoo
数组
arr=("a" "b" "c" "d")
echo arr[0]
# 获取所有
echo arr[@]
echo arr[*]
# 获取数组的长度
length=${
#arr[@]}
运算
原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。
$((运算式)) 或 $[运算式],效率更高。
#!/bin/bash
val=`expr 2 + 2`
echo "两数之和为 : $val"
# 1.(())
echo $((1+1))
# 2.let
let a+=6 c=a+b
# 注意,对于类似let x+y这样的写法,Shell 虽然计算了 x+y 的值,但却将结果丢弃;若不想这样,可以使用let sum=x+y将 x+y 的结果保存在变量 sum 中。
read读取控制台输入
两点注意:
表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2,这与我们熟悉的大多数编程语言不一样。
完整的表达式要被 `` 包含,注意这个字符不是常用的单引号,在 Esc 键下边。
条件表达式要放在方括号之间,并且要有空格,例如: [ a = = a== a==b] 是错误的,必须写成 [ $a == $b ]。
乘号(*)前边必须加反斜杠()才能实现乘法运算;
运算符 | 说明 | 举例 |
---|---|---|
! | 非运算,表达式为 true 则返回 false,否则返回 true。 | [ ! false ] 返回 true。 |
-o | 或运算,有一个表达式为 true 则返回 true。 | [ $a -lt 20 -o $b -gt 100 ] 返回 true。 |
-a | 与运算,两个表达式都为 true 才返回 true。 | [ $a -lt 20 -a $b -gt 100 ] 返回 false。 |
文件测试运算符用于检测 Unix 文件的各种属性。
操作符 | 说明 | 举例 |
---|---|---|
-b file | 检测文件是否是块设备文件,如果是,则返回 true。 | [ -b $file ] 返回 false。 |
-c file | 检测文件是否是字符设备文件,如果是,则返回 true。 | [ -c $file ] 返回 false。 |
-d file | 检测文件是否是目录,如果是,则返回 true。 | [ -d $file ] 返回 false。 |
-f file | 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 | [ -f $file ] 返回 true。 |
-g file | 检测文件是否设置了 SGID 位,如果是,则返回 true。 | [ -g $file ] 返回 false。 |
-k file | 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 | [ -k $file ] 返回 false。 |
-p file | 检测文件是否是有名管道,如果是,则返回 true。 | [ -p $file ] 返回 false。 |
-u file | 检测文件是否设置了 SUID 位,如果是,则返回 true。 | [ -u $file ] 返回 false。 |
-r file | 检测文件是否可读,如果是,则返回 true。 | [ -r $file ] 返回 true。 |
-w file | 检测文件是否可写,如果是,则返回 true。 | [ -w $file ] 返回 true。 |
-x file | 检测文件是否可执行,如果是,则返回 true。 | [ -x $file ] 返回 true。 |
-s file | 检测文件是否为空(文件大小是否大于0),不为空返回 true。 | [ -s $file ] 返回 true。 |
-e file | 检测文件(包括目录)是否存在,如果是,则返回 true。 | [ -e $file ] 返回 true。 |
其他检查符:
- -S: 判断某文件是否 socket。
- -L: 检测文件是否存在并且是一个符号链接。
流程控制
if
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
for
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
for((i=1;i<=5;i++))
do
echo "这是第 $i 次调用";
done;
while
while condition
do
command
done
# 示例
#!/bin/bash
int=1
while(( $int<=5 ))
do
echo $int
let "int++"
done
文件包含
通过.引入需要包含的文件
边栏推荐
- MATLAB实例:阶跃函数的两种表达方式
- LeetCode#198. raid homes and plunder houses
- UCORE LaB6 scheduler experiment report
- Scoring system based on 485 bus
- ucore lab5
- HDU - 6024 Building Shops(女生赛)
- Cost accounting [22]
- Research Report on market supply and demand and strategy of Chinese hospital cleaning chemicals industry
- Cost accounting [24]
- LeetCode#268. Missing numbers
猜你喜欢
Brief introduction to libevent
ucore lab 6
力扣刷题记录
STM32 how to use stlink download program: light LED running light (Library version)
动态规划前路径问题优化方式
Intensive learning notes: Sutton book Chapter III exercise explanation (ex17~ex29)
Learning record: Tim - capacitive key detection
Optimization method of path problem before dynamic planning
C语言数组的概念
STM32 learning record: LED light flashes (register version)
随机推荐
数据在内存中的存储&载入内存,让程序运行起来
Research Report on printed circuit board (PCB) connector industry - market status analysis and development prospect forecast
动态规划前路径问题优化方式
CS zero foundation introductory learning record
UCORE LaB6 scheduler experiment report
China earth moving machinery market trend report, technical dynamic innovation and market forecast
Research Report on market supply and demand and strategy of Chinese hospital cleaning chemicals industry
Cost accounting [15]
Optimization method of path problem before dynamic planning
The wechat red envelope cover designed by the object is free! 16888
Cost accounting [24]
ucore lab5
HDU-6025-Coprime Sequence(女生赛)
LeetCode#2062. Count vowel substrings in strings
Cost accounting [20]
Report on the market trend, technological innovation and market forecast of printing and decorative paper in China
Cost accounting [13]
Your wechat nickname may be betraying you
ucore lab 2
Intensive learning notes: Sutton book Chapter III exercise explanation (ex17~ex29)