大数据学习之Shell

1、Shell概述

我们知道,一台机器是由硬件组装起来的,它还有操作系统,Windows、Linux、Mac等,我们在Linux里面介绍过,而我们今天讲的Shell就是在基于Linux内核的基础上,接收应用程序(用户命令),然后调用起来操作系统的内核来分配资源,进行指令的运行,我们可以参考下图理解

shell脚本
应用程序或用户命令
服务器
分配资源及执行脚本

Shell是一个功能相当大的编程语言,易编写、易调试、灵活性强。

  • Linux自身提供Shell解析器
[root@hadoop101 ~]$ cat /etc/shells
/bin/sh
/bin/bash
  • bash和sh的关系
[root@hadoop101 bin]$ ll | grep bash
rwxr-xr-x. 1 root root 941880 5月  11 2016 bash
lrwxrwxrwx. 1 root root      4 5月  27 2017 sh -> bash

可以看出来sh是bash的软连接

  • Centos默认的解析器是bash
[root@hadoop101 bin]$ echo $SHELL
/bin/bash

2、Shell脚本入门

  1. 脚本格式
 脚本以#!/bin/bash开头(指定的解析器)
  1. 第一个脚本Shell脚本:helloworld

先创建一个Shell脚本,并且切换到vim编辑模式下

[root@hadoop101 datas]$ touch helloworld.sh
[root@hadoop101 datas]$ vim helloworld.sh

对Shell脚本进行编辑

#!/bin/bash
echo "helloworld"

3.脚本常用执行方式

①采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)
sh+脚本的相对路径和绝对路径

[root@hadoop101 datas]$ sh helloworld.sh 
Helloworld
[root@hadoop101 datas]$ sh /home/atguigu/datas/helloworld.sh 
helloworld

bash+脚本的相对路径和绝对路径

[root@hadoop101 datas]$ bash helloworld.sh 
Helloworld
[root@hadoop101 datas]$ bash /home/atguigu/datas/helloworld.sh 
Helloworld

②采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)

(a)首先要赋予helloworld.sh 脚本的+x权限

[root@hadoop101 datas]$ chmod 744 helloworld.sh

(b)执行脚本
相对路径

[root@hadoop101 datas]$ ./helloworld.sh 
Helloworld

绝对路径

[root@hadoop101 datas]$ /home/atguigu/datas/helloworld.sh 
Helloworld

注意:第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限。

③在脚本的路径前加上“.”
(a)有以下脚本

[root@hadoop101 datas]$ cat test1.sh 
#! /bin/bash
A="hello"

(b) 分别使用sh,bash,./ 和 . 的方式来执行,结果如下:

[root@hadoop101 datas]$ bash test1.sh 
[root@hadoop101 datas]$ echo $A
[root@hadoop101 datas]$ sh test1.sh 
[root@hadoop101 datas]$ echo $A
[root@hadoop101 datas]$ ./test1.sh 
[root@hadoop101 datas]$ echo $A
[root@hadoop101 datas]$ . test1.sh 
[root@hadoop101 datas]$ echo $A
hello

以上三种方式的区别:
前两种方式都是在当前shell中打开一个子shell来执行脚本内容,当脚本内容结束,则子shell关闭,回到父shell中。
第三种,也就是使用在脚本路径前加.的方式,可以使脚本内容在当前shell里执行,而无需打开子shell!
开子shell与不开子shell的区别就在于,环境变量的继承关系,如在子shell中设置的当前变量,父shell是不可见的。

3、变量

1.常用系统变量

常用的系统变量

$HOME$PWD$SHELL$USER

2.自定义变量

①基本语法
(a)定义变量:变量=值
(b)撤销变量:unset变量
(c)声明静态变量:readonly变量,注意:不能unset
②变量定义规则
(a)变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建议大写。
(b)等号两侧不能有空格
(c)在bash中,变量默认类型都是字符串类型,无法直接进行数值运算。
(d)变量的值如果有空格,需要使用双引号或单引号括起来。
③案例实操
定义变量A

[root@hadoop101 datas]$ A=5
[root@hadoop101 datas]$ echo $A
5

给变量A重新赋值

[root@hadoop101 datas]$ A=8
[root@hadoop101 datas]$ echo $A
8

撤销变量A

[root@hadoop101 datas]$ unset A
[root@hadoop101 datas]$ echo $A

声明静态的变量B=2,不能unset

[root@hadoop101 datas]$ readonly B=2
[root@hadoop101 datas]$ echo $B
2
[root@hadoop101 datas]$ B=9
-bash: B: readonly variable

在bash中,变量默认类型都是字符串类型,无法直接进行数值运算

[root@hadoop101 ~]$ C=1+2
[root@hadoop101 ~]$ echo $C
1+2

变量的值如果有空格,需要使用双引号或单引号括起来

[root@hadoop101 ~]$ D=I love banzhang
-bash: world: command not found
[root@hadoop101 ~]$ D="I love banzhang"
[root@hadoop101 ~]$ echo $D
I love banzhang

可把变量提升为全局环境变量,可供其他Shell程序使用
export 变量名

[root@hadoop101 datas]$ vim helloworld.sh 

在helloworld文件中增加echo$B

#!/bin/bash
echo "helloworld"
echo $B
[root@hadoop101 datas]$ ./helloworld.sh 
Helloworld

发现并没有打印输出变量B的值。

[root@hadoop101 datas]$ export B
[root@hadoop101 datas]$ ./helloworld.sh 
helloworld
2

3.特殊变量

(1)$n
①基本语法
$n (功能描述:n为数字,$0代表该脚本名称,$1- 9 代 表 第 一 到 第 九 个 参 数 , 十 以 上 的 参 数 , 十 以 上 的 参 数 需 要 用 大 括 号 包 含 , 如 9代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如 9{10})
②案例实操

[root@hadoop101 datas]$ touch parameter.sh 
[root@hadoop101 datas]$ vim parameter.sh
#!/bin/bash
echo "$0  $1   $2"
[root@hadoop101 datas]$ chmod 777 parameter.sh
[root@hadoop101 datas]$ ./parameter.sh cls  xz
./parameter.sh  cls   xz

(2) $#
①基本语法
$# (功能描述:获取所有输入参数个数,常用于循环)。
②案例实操

[root@hadoop101 datas]$ vim parameter.sh
#!/bin/bash
echo "$0  $1   $2"
echo $#
[root@hadoop101 datas]$ chmod 777 parameter.sh
[root@hadoop101 datas]$ ./parameter.sh cls  xz
parameter.sh cls xz 
2

(3)

$*$@

①基本语法

$*	(功能描述:这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体)
$@	(功能描述:这个变量也代表命令行中所有的参数,不过$@把每个参数区分对待)

②案例实操

[root@hadoop101 datas]$ vim parameter.sh
#!/bin/bash
echo "$0  $1   $2"
echo $#
echo $*
echo $@
[root@hadoop101 datas]$ bash parameter.sh 1 2 3
parameter.sh  1   2
3
1 2 3
1 2 3

(4) $?
①基本语法
$? (功能描述:最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了。)
②案例实操

[root@hadoop101 datas]$ ./helloworld.sh 
hello world
[root@hadoop101 datas]$ echo $?
0

4、运算符

(1)基本语法
$((运算式))或
$[运算式]
(2)案例实操

[root@hadoop101 datas]# S=$[(2+3)*4]
[root@hadoop101 datas]# echo $S

5、条件判断

(1)基本语法
①test condition
②[ condition ](注意condition前后要有空格)
(2)常用判断条件
①两个整数之间比较
-lt 小于 -le 小于等于
-eq 等于 -gt 大于
-ge 大于等于) -ne 不等于
②按照文件权限进行判断
-r 有读的权限(read) -w 有写的权限(write)
-x 有执行的权限(execute)
③按照文件类型进行判断
-f 文件存在并且是一个常规的文件(file)
-e 文件存在(existence) -d 文件存在并是一个目录(directory)
(3)案例实操
①23是否大于等于22

[root@hadoop101 datas]$ [ 23 -ge 22 ]
[root@hadoop101 datas]$ echo $?
0

②helloworld.sh是否具有写权限

[root@hadoop101 datas]$ [ -w helloworld.sh ]
[root@hadoop101 datas]$ echo $?
0

③/home/atguigu/cls.txt目录中的文件是否存在

[root@hadoop101 datas]$ [ -e /home/atguigu/cls.txt ]
[root@hadoop101 datas]$ echo $?
1

④多条件判断(&& 表示前一条命令执行成功时,才执行后一条命令,|| 表示上一条命令执行失败后,才执行下一条命令)

[root@hadoop101 ~]$ [ condition ] && echo OK || echo notok
OK
[root@hadoop101 datas]$ [ condition ] && [ ] || echo notok
notok

6、流程控制

1.if判断

(1)基本语法
(1)单分支
if [ 条件判断式 ];then
程序
fi
或者
if [ 条件判断式 ]
then
程序
fi
(2)多分支
if [ 条件判断式 ]
then
程序
elif [ 条件判断式 ]
then
程序
else
程序
fi

注意事项:
(1)[ 条件判断式 ],中括号和条件判断式之间必须有空格
(2)if后要有空格

(2)实例实操
输入一个数字,如果是1,则输出banzhang zhen shuai,如果是2,则输出cls zhen mei,如果是其它,什么也不输出。

[root@hadoop101 datas]$ touch if.sh
[root@hadoop101 datas]$ vim if.sh
#!/bin/bash
if [ $1 -eq "1" ]
then
      echo "banzhang zhen shuai"
elif [ $1 -eq "2" ]
then
      echo "cls zhen mei"
fi
[root@hadoop101 datas]$ chmod 777 if.sh 
[root@hadoop101 datas]$ ./if.sh 1
banzhang zhen shuai

2.case

(1)基本语法
case $变量名 in
“值1”)
如果变量的值等于值1,则执行程序1
;;
“值2”)
如果变量的值等于值2,则执行程序2
;;
…省略其他分支…
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac

注意事项:
(1)case行尾必须为单词“in”,每一个模式匹配必须以右括号“)”结束。
(2)双分号“;;”表示命令序列结束,相当于java中的break。
(3)最后的“*)”表示默认模式,相当于java中的default。

(2)案例实操
输入一个数字,如果是1,则输出banzhang,如果是2,则输出cls,如果是其它,输出songsong。

[root@hadoop101 datas]$ touch case.sh
[root@hadoop101 datas]$ vim case.sh
!/bin/bash
case $1 in
"1")
      echo "banzhang"
;;
"2")
      echo "cls"
;;
*)
      echo "songsong"
;;
esac
[root@hadoop101 datas]$ chmod 777 case.sh
[root@hadoop101 datas]$ ./case.sh 1
1

3.for循环

(1)基本语法
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done
或者
for 变量 in 值1 值2 值3…
do
程序
done
(2)案例实操 从1加到100

[root@hadoop101 datas]$ touch for1.sh
[root@hadoop101 datas]$ vim for1.sh
#!/bin/bash
s=0
for((i=0;i<=100;i++))
do
      s=$[$s+$i]
done
echo $s
[root@hadoop101 datas]$ chmod 777 for1.sh 
[root@hadoop101 datas]$ ./for1.sh 
“5050”
[root@hadoop101 datas]$ touch for2.sh
[root@hadoop101 datas]$ vim for2.sh
#!/bin/bash
#打印数字
for i in $*
  do
   echo "ban zhang love $i "
done
[root@hadoop101 datas]$ chmod 777 for2.sh 
[root@hadoop101 datas]$ bash for2.sh cls xz bd
ban zhang love cls
ban zhang love xz
ban zhang love bd

(3)

#比较比较“$*”和“$@”区别
[root@hadoop101 datas]$ vim for.sh
#!/bin/bash 
for i in "$*" 
#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次 
      do 
             echo "ban zhang love $i"
    done 
for j in "$@" 
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次 
      do 
             echo "ban zhang love $j" 
done
[root@hadoop101 datas]$ chmod 777 for.sh
[root@hadoop101 datas]$ bash for.sh cls xz bd
ban zhang love cls xz bd
ban zhang love cls
ban zhang love xz
ban zhang love bd

4.while循环

(1)基本语法
while [ 条件判断式 ]
do
程序
done
(2)案例实操
从1加到100

[root@hadoop101 datas]$ touch while.sh
[root@hadoop101 datas]$ vim while.sh
#!/bin/bash
s=0
i=1
while [ $i -le 100 ]
do
      s=$[$s+$i]
     i=$[$i+1]
done
echo $s
[root@hadoop101 datas]$ chmod 777 while.sh 
[root@hadoop101 datas]$ ./while.sh 
5050

7、读取控制台输出

(1)基本语法
read(选项)(参数)
选项:
-p:指定读取值时的提示符;
-t:指定读取值时等待的时间(秒)。
参数
变量:指定读取值的变量名
(2)案例实操
提示7秒内,读取控制台输入的名称

[root@hadoop101 datas]$ touch read.sh
[root@hadoop101 datas]$ vim read.sh
#!/bin/bash
read -t 7 -p "Enter your name in 7 seconds " NAME
echo $NAME
[root@hadoop101 datas]$ ./read.sh 
Enter your name in 7 seconds xiaoze
xiaoze

8、函数

1.系统函数

(1)basename
①基本语法
basename [string / pathname] [suffix] (功能描述:basename命令会删掉所有的前缀包括最后一个(‘/’)字符,然后将字符串显示出来。
②案例实操
截取该/home/atguigu/banzhang.txt路径的文件名称

[root@hadoop101 datas]$ basename /home/atguigu/banzhang.txt 
banzhang.txt
[root@hadoop101 datas]$ basename /home/atguigu/banzhang.txt .txt
banzhang

(2)dirname
①基本语法
dirname 文件绝对路径 (功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分))
②案例实操
获取banzhang.txt文件的路径

[root@hadoop101 ~]$ dirname /home/atguigu/banzhang.txt 
/home/atguigu

2.自定义函数

(1)基本语法
[ function ] funname[()]
{
Action;
[return int;]
}
funname
(2)案例实操

[root@hadoop101 datas]$ touch fun.sh
[root@hadoop101 datas]$ vim fun.sh
#!/bin/bash
function sum()
{
s=0
 s=$[ $1 + $2 ]
  echo "$s"
}
read -p "Please input the number1: " n1;
read -p "Please input the number2: " n2;
sum $n1 $n2;
[root@hadoop101 datas]$ chmod 777 fun.sh
[root@hadoop101 datas]$ ./fun.sh 
Please input the number1: 2
Please input the number2: 5
7

9、Shell工具

1.cut

cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。
(1)基本语法
cut [选项参数] filename
说明:默认分隔符是制表符
(2)选项参数

选项参数功能
-f列号,提取第几列
-d分隔符,按照指定分隔符分割列
-c指定具体的字符

(3)案例实操
①数据准备

[root@hadoop101 datas]$ touch cut.txt
[root@hadoop101 datas]$ vim cut.txt
dong shen
guan zhen
wo  wo
lai  lai
le  le

②切割cut.txt第一列

[root@hadoop101 datas]$ cut -d " " -f 1 cut.txt 
dong
guan
wo
lai
le

③切割cut.txt第二、三列

[root@hadoop101 datas]$ cut -d " " -f 2,3 cut.txt 
shen
zhen
	wo
	lai
	le

④在cut.txt文件中切割出guan

[root@hadoop101 datas]$ cat cut.txt | grep "guan" | cut -d " " -f 1
guan

⑤选取系统PATH变量值,第2个“:”开始后的所有路径:

[root@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
[root@hadoop101 datas]$ echo $PATH | cut -d: -f 3-
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/atguigu/bin

⑥切割ifconfig 后打印的IP地址

[root@hadoop101 datas]$ ifconfig | grep "netmask" | cut -d "t" -f 2 
|cut -d " " -f 2

2.sed

sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。
(1)基本语法
sed [选项参数] ‘command’ filename
(2)选项参数说明

选项参数功能
-e直接在指令列模式上进行sed的动作编辑。
-i直接编辑文件
命令功能描述
a新增,a的后面可以接字串,在下一行出现
d删除
s查找并替换

(3)案例实操
①数据准备

[root@hadoop101 datas]$ touch sed.txt
[root@hadoop101 datas]$ vim sed.txt
dong shen
guan zhen
wo  wo
lai  lai

le  le

②将“mei nv”这个单词插入到sed.txt第二行下,打印。

[root@hadoop101 datas]$ sed '2a mei nv' sed.txt 
dong shen
guan zhen
mei nv
wo  wo
lai  lai

le  le
[root@hadoop101 datas]$ cat sed.txt 
dong shen
guan zhen
wo  wo
lai  lai

le  le
···
#注意:文件并没有改变
③删除sed.txt文件所有包含wo的行
```bash
[root@hadoop101 datas]$ sed '/wo/d' sed.txt
dong shen
guan zhen
lai  lai

le  le

④将sed.txt文件中wo替换为ni

[root@hadoop101 datas]$ sed 's/wo/ni/g' sed.txt 
dong shen
guan zhen
ni  ni
lai  lai

le  le

⑤将sed.txt文件中的第二行删除并将wo替换为ni

[root@hadoop101 datas]$ sed -e '2d' -e 's/wo/ni/g' sed.txt 
dong shen
ni  ni
lai  lai

le  le

3.awk(重点)

一个强大的文本分析工具,把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行分析处理。
(1)基本语法
awk [选项参数] ‘pattern1{action1} pattern2{action2}…’ filename
pattern:表示AWK在数据中查找的内容,就是匹配模式
action:在找到匹配内容时所执行的一系列命令
(2)选项说明

选项参数功能
-F指定输入文件折分隔符
-v赋值一个用户定义变量

(3)案例实操
①数据准备

[root@hadoop101 datas]$ sudo cp /etc/passwd ./

②搜索passwd文件以root关键字开头的所有行,并输出该行的第7列。

[root@hadoop101 datas]$ awk -F: '/^root/{print $7}' passwd 
/bin/bash

③搜索passwd文件以root关键字开头的所有行,并输出该行的第1列和第7列,中间以“,”号分割。

[root@hadoop101 datas]$ awk -F: '/^root/{print $1","$7}' passwd 
root,/bin/bash

④只显示/etc/passwd的第一列和第七列,以逗号分割,且在所有行前面添加列名user,shell在最后一行添加"ltc,/bin/zuishuai"。

[root@hadoop101 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
ltc,/bin/zuishuai
#注意:BEGIN 在所有数据读取行之前执行;END 在所有数据执行之后执行。

⑤将passwd文件中的用户id增加数值1并输出

[root@hadoop101 datas]$ awk -v i=1 -F: '{print $3+i}' passwd
1
2
3
4

(4)awk的内置变量

变量说明
FILENAME文件名
NR已读的记录数
NF浏览记录的域的个数(切割后,列的个数)

(5)案例实操
①统计passwd文件名,每行的行号,每行的列数

[root@hadoop101 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

[root@hadoop101 datas]$ ifconfig | grep "netmask" | awk -F "inet" 
'{print $2}' | awk -F " " '{print $1}'

③查询sed.txt中空行所在的行号

[root@hadoop101 datas]$ awk '/^$/{print NR}' sed.txt 
5

4.sort

(1)基本语法
sort(选项)(参数)

(2)选项参数说明

选项说明
-n依照数值的大小排序
-r以相反的顺序来排序
-t设置排序时所用的分隔字符
-k指定需要排序的列

(3)案例实操
①数据准备

[root@hadoop101 datas]$ touch sort.sh
[root@hadoop101 datas]$ vim sort.sh 
bb:40:5.4
bd:20:4.2
xz:50:2.3
cls:10:3.5
ss:30:1.6

②按照“:”分割后的第三列倒序排序。

[root@hadoop101 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

5.wc

(1)基本语法
wc [选项参数] filename

(2)选项参数

选项参数功能
-l统计文件行数
-w统计文件的单词数
-m统计文件的字符数
-c统计文件的字节数

(3)案例实操
统计/etc/profile文件的行数、单词数、字节数!

[root@hadoop101 datas]# wc -w /etc/profile 
[root@hadoop101 datas]# wc -l /etc/profile 
[root@hadoop101 datas]# wc -m /etc/profile

10、正则表达式

正则表达式我认为很重要,下次专门找个时间做🙂

  • 9
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值