课程由阿里云大学免费提供
0. 运行
chmod +x <name>.sh
./<name>.sh
1. 变量
赋值:
- 直接赋值:
A=aaa
echo $A
B="$A B"
B='$A B'
注意:1) 等号两边不能有空格; 2)单引号之间的内容原封不动地制定给了变量,双引号之间仅仅是取消了空格的作用,保留特殊符号的含义。
- 命令的结果作为变量 :
# 1.
A=`date`
echo $A
# 2.
B=$(ls -l)
echo $B
- 与其他变量或字符串组成新字符串(变量):
DAY=mon
echo Today is ${DAY}day
# 如果不加{}则shell会认为这是一个叫做DAYday的变量
-
read
命令:从键盘读入数据,赋值给变量
read a b c 1 2 3 echo $a $b $c 1 2 3
查看变量值
set
:查看所有变量值。
set| grep <name>
: 查看<name>
变量的值。
删除变量
unset <name>
位置变量和特殊变量
位置变量: Shell解释执行用户的命令时,将命令行的第一个字作为命令名,而其他字作为参数。由在命令行上的位置确定的参数称为位置参数。
- $0 :执行的命令/程序名
- $n :命令/程序 的第n个参数值。
特殊变量:有些变量时一开始执行Script脚本时就会设定,且不能被修改,但是我们不叫它只读的系统变量,而叫他特殊变量。
- $* : 表示这个程序的所有参数
- $#:表示程序参数的个数
- $$:这个程序的PID
- $!:执行上一个后台指令的PID
- $?:上一个指令的返回值
变量测试语句 : test
测试范围:整数,字符串,文件。
语法:
-
字符串变量:
test str1==str2 #是否相等 test str1!=str2 #是否不等 test str1 # 测试字符串是否不空 test -n str1 # 测试字符串是否为空 test -z str1 # 测试字符串是否为空
-
整数变量:
test int1 -eq int2 # equals test int1 -ge int2 # greater than or equals test int1 -gt int2 # greater than test int1 -le int2 # less than or equals test int1 -lt int3 # less than
也可以省略test, 写成:
[int1 -lt int2]
-
文件:
test -d file # Is directory test -f file # Is normal file test -x file # Is executeble test -r file # Is readable test -w file # IS writeable test -e file # Is exist test -s file # Is empty
也可以省略test,简写成
[-x file]
2. 流程控制
2.1 if 语句
Syntax:
#!/bin/bash
int1=123
if [$int1 -lt 222]
then
echo less
elif [$int1 -eq 222]
then
echo euqals
else
echo greater
fi
echo "==============="
if [$int1 -lt 222]; then
echo less
else
echo equal or greater than
fi
多条件的联合:
-a
or&&
: 逻辑与-o
or||
: 逻辑或
2.2 case 语句
Syntax:
case $A in
str1) echo "execute this:str1"
;;
str2) echo "execute this:str2"
;;
*) echo "default case"
;;
esac
2.3 for 语句
Syntax:
for DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturday
do
echo "The day is $DAY"
done
2.4 while 语句
Syntax:
num=1
while [$num -le 10]
do
square=`expr $num \* $num`
echo $square
num=`expr $num + 1`
done
2.5 双括号(())的使用方法
使用’[]'时,必须保证运算符与算数之间有空格。四则运算也只能借助expr
指令来完成。使用双括号(())
结构语句可以对算数和赋值运算扩展。
使用方法:((表达式1,表达式2…))
特点:
- 在双括号结构中,所有表达式可以像c语言一样,如:a++,b–登
- 在双括号结构中,所有变量可以不加
$
前缀。 - 双括号结构可以进行逻辑运算和四则运算。
- 双括号结构扩展了for,while,if的条件测试运算
- 支持多个表达式运算,各个表达式之间用逗号分开。
VAR=1
while ((VAR<100))
do
echo "Value is: $VAR"
((VAR=VAR*2))
done
2.6 break and continue
Same as them in other language.
3.函数
Shift 指令
参数左移动指令,每执行一次,参数序列顺次左移一个位置,$#的值减1;用于分别处理每个参数,移出去的参数不再可用。
函数定义
function Name ()
{
echo "executing this"
}
函数调用
Syntax:
函数名 参数1 参数2…
调用函数时不需要()。
4.awk
和sed
sed
stream editor
sed编辑器是一行一行地处理文件内容的。正在处理的内容存放在模式空间(缓冲区)内,处理完成后按照选项的规定进行输出或者文件的修改。
Syntax:
sed [options] ‘[command]’ [filename]
常用选项:
-n
By default, each line of input is echoed to the standard output after all of the commands have been
applied to it. The -n option suppresses this behavior.
-i <extension>
Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no
backup will be saved. It is not recommended to give a zero-length extension when in-place editing files,
as you risk corruption or partial content in situations where disk space is exhausted, etc.
常用命令:
i : insert
p : print
d : delete
s : search
c : change
a : append
实例:
sed -n '3p' <filename> # 显示文件前三行
sed -n '1,3p' <filename> # 显示文件1~3行
sed -n '1,3!p' <filename> # 显示文件除1~3行之外的内容
sed -n '1,+3p' <filename> # 显示文件从1开始的四行
sed '1i###' <filename> > <filename> # 在文件头插入###
sed '$a@@@' <filename> > <filename> # 在文件尾插入@@@
sed '3c$$$' <filename> # 把文件第三行替换为$$$
sed '2,4H;$G' <file1> > <file2> # 把文件2到4行复制;粘贴到文件尾部
sed '/^$/d' <filename> # 删除空行
sed '/Somestring/w <filename>' <filename> # 把文件中包含Something的行写入到新文件中
awk
任何awk语句都是由模式和动作组成,一个awk脚本可以有多个语句。模式决动作语句的处罚条件和触发时间。
BEGIN
: 设计计数和打印头部信息,在任何动作之前进行。
END
: 语句输出统计结果,在完成动作之后执行。
默认分隔符为空格,可以使用-F
+分隔符,来更改为其他分割符。
date | awk '{print "Year:" $6"\tMonth:"$2"\tDay:" $3}' # 自定义日期显示方式
$0 : 所有内容
$i : 第i列的内容
awk '{print $1,$3}' <filename> # 显示第一列和第三列的内容, 中间仍然以空格为分割,','只起分割作用
print 中的空格会在输出时被忽略!