当前位置:网站首页>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
文件包含
通过.引入需要包含的文件
边栏推荐
- Learning record: Tim - capacitive key detection
- ucore lab5
- 程序员的你,有哪些炫技的代码写法?
- LeetCode#237. Delete nodes in the linked list
- Report on the market trend, technological innovation and market forecast of printing and decorative paper in China
- csapp shell lab
- Intensive learning notes: Sutton book Chapter III exercise explanation (ex17~ex29)
- The wechat red envelope cover designed by the object is free! 16888
- LeetCode#53. Maximum subarray sum
- 学习记录:使用STM32外部输入中断
猜你喜欢

C4D quick start tutorial - creating models

ucore lab5

Learning record: Tim - capacitive key detection

Es6---es6 content details

Learning record: use STM32 external input interrupt

JS --- all knowledge of JS objects and built-in objects (III)

ucore lab 6

STM32学习记录:LED灯闪烁(寄存器版)

STM32学习记录:玩转按键控制蜂鸣器和LED

ucore lab7
随机推荐
C4D quick start tutorial - creating models
Crawler series (9): item+pipeline data storage
How to build a nail robot that can automatically reply
Cost accounting [13]
How to change XML attribute - how to change XML attribute
STM32 learning record: play with keys to control buzzer and led
LeetCode#204. Count prime
学习记录:USART—串口通讯
Learning record: understand systick system timer and write delay function
Path problem before dynamic planning
力扣刷题记录
Market trend report, technical innovation and market forecast of Chinese hospital respiratory humidification equipment
ucore lab 6
csapp shell lab
Research Report on printed circuit board (PCB) connector industry - market status analysis and development prospect forecast
JS --- detailed explanation of JS facing objects (VI)
STM32如何使用STLINK下载程序:点亮LED跑马灯(库版本)
Cost accounting [14]
Research Report on pharmaceutical R & D outsourcing service industry - market status analysis and development prospect forecast
Research Report on market supply and demand and strategy of China's earth drilling industry