一、shell简介
shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。
shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。
二、shell基本常识
- shell脚本名称命名一般为英文的大写、小写;
- 不能使用特殊的符号、空格来命名;
- shell脚本后缀以.sh结尾;
- shell脚本内容首行需以#!/bin/bash开头;
- shell脚本中变量名称尽量使用大写字母,字母之间不能使用“-”,可以使用“_”;
- shell脚本变量名称不能以数字、特殊符号开头。
- 环境变量 export name//使变量name变为环境变量
unset name//取消环境变量的设定
三、shell脚本
- 在终端使用“vim”命令进入文本编辑器,新建一个文件tssh.sh。
chmod +x ./test.sh #是脚本具有执行权限
./test.sh #执行脚本
/bin/sh test.sh #直接运行解释器
- 算数运算符 + 加法 - 减法 * 乘法 / 除法 % 取余 = 赋值 == 相等 != 不相等
- 流程控制
① if
if condition
then
程序段
f1
②if else
if condition
Then
程序段
elif condition2
Then
程序段
else
程序段
f1
③while
while:
do
程序段
done
四、文本处理工具
- awk 格式 awk ‘pattern + {action}’ file
①awk ‘NR==3,NR==5{print}’ jfedu.txt
awk ‘NR==3,NR==5{print $0}’ jfedu.txt
打印文件jfedu中的第三行至第五行,NR表示打印行,$0表示文本所有域。
②awk ‘length($0)>80(print NR) jfedu.txt 打印文件jfedu文件中长度大于80的行号
③ifconfig eth0|grep “bcast”| awk ’{print”ip_”$2}’ awk添加自定义符号。
2.find 格式 find path -option [ -print] [-exec -ok command] {}\;
① -name filename: 查找名为filename的文件。
② -perm: 按执行权限来查找。
③ -nouser:查找无有效属主文件
④ -type b/d/c/p/l/f:查找块设备、目录、字符设备、管道、符号链接、普通文件。
⑤ -user username:按文件属主来查找
⑥ -user username:按组来查找
3.sed 格式 sed [ -Options] [‘Commands’] filename;
① x:指定行号
② x,y: 指定从x到y的行号范围
③ r: 从另一个文件中读文件
④ w:从文本写入到一个文件
⑤ y:变换字符
⑥ p:打印匹配行
⑦ =:打印文件行号
4.grep 格式 grep -[acinv] ‘word’ filename
① -a:以文本文件方式搜索。
② -c:计算找到的符合行的次数。
③ -i: 忽略大小写。
④ -n: 顺便输出行号。
⑤ -!: 逻辑运算符。
五、小程序
while循环1-100的和