shell编程
bash和sh的关系:sh是通过 指向bash(解释器),bash负责解释shell命令给linux内核
shell脚本入门
脚本格式
脚本以#!/bin/bash开头(指定解释器)
脚本的常用执行方式
(1)采用bash或sh +脚本的相对路径或者结对路径(不用赋予脚本+X权限)
sh+脚本的相对路径:
[atguigu@hadoop101 datas]$ sh helloworld.sh
输出:Helloworld
sh+脚本的绝对路径
[atguigu@hadoop101 datas]$ sh /home/atguigu/datas/helloworld.sh
输出:helloworld
bash+脚本的相对路径
[atguigu@hadoop101 datas]$ bash helloworld.sh
输出:Helloworld
bash+脚本的绝对路径
[atguigu@hadoop101 datas]$ bash /home/atguigu/datas/helloworld.sh
输出:Helloworld
(2)采用输入脚本的绝对路径或者相对路径执行脚本(必须具有可执行权限)
a.首先要赋予hellworld.sh脚本的执行权限
[atguigu@hadoop101 datas]$ chmod +x helloworld.sh
b.执行脚本
相对路径
[atguigu@hadoop101 datas]$ ./helloworld.sh
输出:Helloworld
绝对路径
[atguigu@hadoop101 datas]$ /home/atguigu/datas/helloworld.sh
输出:Helloworld
变量
常用系统变量
$HOME , $PWD , $SHELL , $USER 等
自定义变量
(1)基本语法
a.定义变量:变量=值
b.撤销变量:unset 变量
c.声明静态变量:readonly变量,注意:不能unset
(2)变量定义规则
a.变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建议大写
b.等号两侧不能有空格
c.在bash中,变量默认类型都是字符串类型,无法直接进行数值运算
d.变量的值如果有空格,需要使用双引号或单引号括起来
双引号和单引号
双引号可以在里面放参数,单引号不可以,单引号就把它们整体当作字符串了
(1)单引号不取变量值
(2)双引号取变量值
(3)反引号`,执行引号中命令
(4)双引号内部嵌套单引号,取出变量值
(5)单引号内部嵌套双引号,不取出变量值
特殊变量
$n
基本语法:$n (功能描述:n为数字,$0代表该脚本名称,$1- 9 代 表 第 一 到 第 九 个 参 数 , 十 以 上 的 参 数 , 十 以 上 的 参 数 需 要 用 大 括 号 包 含 , 如 9代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如 9代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如{10})(用来接受参数)
$#
基本语法:$# (功能描述:获取所有输入参数个数,常用于循环)
$?
基本语法:$? (功能描述:最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了。)
$*
**基本语法:**这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体(“ ”)
$#
**基本语法:**这个变量也代表命令行中所有的参数,不过$@把每个参数区分对待(“ ”)
$?
**基本语法:**最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了
运算符
基本语法:
$((运算式)) 或 $[运算式]
条件判断(重点)
基本语法
举例:
[atguigu@hadoop101 datas]$ [ 23 -ge 22 ]
[atguigu@hadoop101 datas]$ echo $?
输出:0
echo $? --判断是否判断正确(0则正确,其他则是错)
常用判断条件
(1)两个整数之间比较
= 字符串比较
-lt 小于 (less than) -le 小于等于(less equal)
-eq 等于 (equal) -gt 大于(greater than)
-ge 大于等于(greater equal) -ne 不等于 (Not equal)
(2)按照文件权限进行判断
-r 有读的权限 (read) -w 有些的权限 (write)
-x 有执行的权限 (execute)
(3) 按照文件类型进行判断
-f 文件存在并且是一个常规的文件(file)
-e 文件存在 (existence) -d 文件存在并是一个目录 (directory)
流程控制
if判断
基本语法
if [ 条件判断式 ]
then
//程序
elif [条件判断式]
then
//程序
else
//程序
fi //结束if循环
注意事项:
(1) [条件判断式],中括号和条件判断式之间必须有空格
(2) if后要有空格
case语句
基本语法
case $变量名 in
"值1")
如果变量的值等于值1,则执行程序1
;;
"值2")
如果变量的值等于值2,则执行程序2
;;
…省略其他分支…
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac
注意事项:
(1) case行尾必须为单词“in”,每一个模式匹配必须以右括号“)”结束
(2) 双分号“;;”表示命令序列结束,相当于java中的break
(3) 最后的“*)”表示默认模式,相当于java中的default
for循环
基本语法
for(( 初始值;循环控制条件;变量变化 ))
do
程序
done
while循环
基本语法
while [ 条件判断式 ]
do
程序
done
read读取控制台
基本语法
read(选项)(参数)
选项:
-p: 指定读取时的提示符
-t: 指定读取值时等待的时间 (秒)
参数
变量:指定读取值的变量名
函数
系统函数
basename
基本语法
basename [string / pathname] [suffix] (功能描述:basename命令会删掉所有的前缀包括最后一个(‘/’)字符,然后将字符串显示出来
举例:
//截取该/home/atguigu/banzhang.txt路径的文件名称
[atguigu@hadoop101datas]$ basename /home/atguigu/banzhang.txt
banzhang.txt
[atguigu@hadoop101 datas]$ basename /home/atguigu/banzhang.txt .txt
banzhang
dirname
基本语法
dirname 文件绝对路径 (功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分))
举例;
//获取banzhang.txt文件的路径
[atguigu@hadoop101 ~]$ dirname /home/atguigu/banzhang.txt
/home/atguigu
自定义函数
基本语法
//定义函数(function指函数名)
[ function ] funname[()]
{
Action;//代码
[return int;]
}
//点用函数
funname
shell工具
cut
说明:cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出
基本用法
cut[选项参数] filename
说明:默认分隔符是制表符
选项参数说明
选项参数 | 功能 |
---|---|
-f | 列号,提取第几列 |
-d | 分隔符,按照指定分隔符分割列 |
-c | 指定具体的字符 |
举例:
//数据准备
[atguigu@hadoop101 datas]$ touch cut.txt
[atguigu@hadoop101 datas]$ vim cut.txt
dong shen
guan zhen
wo wo
lai lai
le le
//切割cut.txt第一列
[atguigu@hadoop101 datas]$ cut -d " " -f 1 cut.txt
dong
guan
wo
lai
le
//切割cut.txt第二、三列
[atguigu@hadoop101 datas]$ cut -d " " -f 2,3 cut.txt
shen
zhen
wo
lai
le
//在cut.txt文件中切割出guan
[atguigu@hadoop101 datas]$ cat cut.txt | grep "guan" | cut -d " " -f 1
guan
//选取系统PATH变量值,第2个“:”开始后的所有路径:
[atguigu@hadoop101 datas]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/atguigu/bin
[atguigu@hadoop102 datas]$ echo $PATH | cut -d: -f 2-
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/atguigu/bin
//切割ifconfig 后打印的IP地址
[atguigu@hadoop101 datas]$ ifconfig eth0 | grep "inet addr" | cut -d: -f 2 | cut -d" " -f1
192.168.1.102
awk
说明:一个强大的文本分析工具,把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行分析处理
基本用法
awk [选项参数] ‘pattern1{
action1} pattern2{
action2}...’ filename
pattern:表示AWK在数据中查找的内容,就是匹配模式
action:在找到匹配内容时所执行的一系列命令
选项参数说明
选项参数 | 功能 |
---|---|
-F | 指定输入文件折分隔符 |
-v | 赋值一个用户定义变量 |
举例:
//数据准备
[atguigu@hadoop102 datas]$ sudo cp /etc/passwd ./
//搜索passwd文件以root关键字开头的所有行,并输出该行的第7列
[atguigu@hadoop102 datas]$ awk -F: '/^root/{print $7}' passwd
/bin/bash
//搜索passwd文件以root关键字开头的所有行,并输出该行的第1列和第7列,中间以“,”号分割
[atguigu@hadoop102 datas]$ awk -F: '/^root/{print $1","$7}' passwd
root,/bin/bash
//注意:只有匹配了pattern的行才会执行action
//只显示/etc/passwd的第一列和第七列,以逗号分割,且在所有行前面添加列名user,shell在最后一行添加"dahaige,/bin/zuishuai"
[atguigu@hadoop102 datas]$ awk -F : 'BEGIN{print "user, shell"} {print $1","$7} END{print "dahaige,/bin/zuishuai"}' passwd
user, shell
root,/bin/bash
bin,/sbin/nologin
。。。
atguigu,/bin/bash
dahaige,/bin/zuishuai
//注意:BEGIN 在所有数据读取行之前执行;END 在所有数据执行之后执行
//将passwd文件中的用户id增加数值1并输出
[atguigu@hadoop102 datas]$ awk -v i=1 -F: '{print $3+i}' passwd
1
2
3
4
awk的内置变量
变量 | 说明 |
---|---|
FILENAME | 文件名 |
NR | 已读的记录数 |
NF | 浏览记录的域的个数(切割后,列的个数) |
举例:
//统计passwd文件名,每行的行号,每行的列数
[atguigu@hadoop102 datas]$ awk -F: '{print "filename:" FILENAME ", linenumber:" NR ",columns:" NF}' passwd
filename:passwd, linenumber:1,columns:7
filename:passwd, linenumber:2,columns:7
filename:passwd, linenumber:3,columns:7
//切割IP
[atguigu@hadoop102 datas]$ ifconfig eth0 | grep "inet addr" | awk -F: '{print$2}' | awk -F " " '{print $1}'
192.168.1.102
//查询sed.txt中空行所在的行号
[atguigu@hadoop102 datas]$ awk '/^$/{print NR}' sed.txt
5
sort
说明:sort命令是在Linux里非常有用,它将文件进行排序,并将排序结果标准输出
基本语法
sort(选项)(参数)
选项 | 说明 |
---|---|
-n | 依照数值的大小排序 |
-r | 以相反的顺序来排序 |
-t | 设置排序时所用的分隔字符 |
-k | 指定需要排序的列 |
参数:指定待排序的文件列表
举例:
//数据准备
[atguigu@hadoop102 datas]$ touch sort.sh
[atguigu@hadoop102 datas]$ vim sort.sh
bb:40:5.4
bd:20:4.2
xz:50:2.3
cls:10:3.5
ss:30:1.6
//按照“:”分割后的第三列倒序排序
[atguigu@hadoop102 datas]$ sort -t : -nrk 3 sort.sh
bb:40:5.4
bd:20:4.2
cls:10:3.5
xz:50:2.3
ss:30:1.6