大数据技术之Shell
第1章 Shell概述
第2章 Shell解析器
(1)Linux提供的Shell解析器有:
[jinghnag@hadoop101 ~]$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
(2)bash和sh的关系
[jinghnag@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
(3)Centos默认的解析器是bash
[jinghnag@hadoop101 bin]$ echo KaTeX parse error: Expected 'EOF', got '#' at position 37: …脚本入门 1.脚本格式 脚本以#̲!/bin/bash开头(指定… touch helloworld.sh
[jinghnag@hadoop101 datas]$ vi helloworld.sh
在helloworld.sh中输入如下内容
#!/bin/bash
echo “helloworld”
(3)脚本的常用执行方式
第一种:采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)
sh+脚本的相对路径
[jinghnag@hadoop101 datas]$ sh helloworld.sh
Helloworld
sh+脚本的绝对路径
[jinghnag@hadoop101 datas]$ sh /home/jinghnag/datas/helloworld.sh
helloworld
bash+脚本的相对路径
[jinghnag@hadoop101 datas]$ bash helloworld.sh
Helloworld
bash+脚本的绝对路径
[jinghnag@hadoop101 datas]$ bash /home/jinghnag/datas/helloworld.sh
Helloworld
第二种:采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x,推荐采用这种方式)
(a)首先要赋予helloworld.sh 脚本的+x权限
[jinghnag@hadoop101 datas]$ chmod +x helloworld.sh
(b)执行脚本
相对路径
[jinghnag@hadoop101 datas]$ ./helloworld.sh
Helloworld
绝对路径
[jinghnag@hadoop101 datas]$ /home/jinghnag/datas/helloworld.sh
Helloworld
注意:第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限。
3.第二个Shell脚本:多命令处理
(1)需求:
在/home/jinghnag/目录下创建一个banzhang.txt,在banzhang.txt文件中增加“I love cls”。
(2)案例实操:
[jinghnag@hadoop101 datas]$ touch batch.sh
[jinghnag@hadoop101 datas]$ vi batch.sh
在batch.sh中输入如下内容
#!/bin/bash
cd /home/jinghnag
touch cls.txt
echo “I love cls” >>cls.txt
(3)查看脚本的执行流程(一般使用这个命令来查看脚本执行的步骤,错误)
[jinghnag@hadoop101 datas]$ bash -x batch.sh
(4)查看脚本的语法(一般使用这个命令来查看脚本语法错误)
[jinghnag@hadoop101 datas]$ bash -n batch.sh
第4章 Shell中的变量
4.1 系统变量
- 常用系统变量
HOME、HOME、HOME、PWD、SHELL、SHELL、SHELL、USER等
2.案例实操
(1)查看系统变量的值
[jinghnag@hadoop101 datas]$ echo HOME/home/jinghnag(2)显示当前Shell中所有变量:set[jinghnag@hadoop101datas]HOME /home/jinghnag (2)显示当前Shell中所有变量:set [jinghnag@hadoop101 datas]HOME/home/jinghnag(2)显示当前Shell中所有变量:set[jinghnag@hadoop101datas] set
BASH=/bin/bash
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
4.2 自定义变量
1.基本语法
(1)定义变量:变量=值
(2)撤销变量:unset 变量
(3)声明静态变量:readonly变量,注意:不能unset
2.变量定义规则
(1)变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建议大写。
(2)等号两侧不能有空格
(3)在bash中,变量默认类型都是字符串类型,无法直接进行数值运算。
(4)变量的值如果有空格,需要使用双引号或单引号括起来。
3.案例实操
(1)定义变量A
[jinghnag@hadoop101 datas]$ A=5
[jinghnag@hadoop101 datas]$ echo A5(2)给变量A重新赋值[jinghnag@hadoop101datas]A 5 (2)给变量A重新赋值 [jinghnag@hadoop101 datas]A5(2)给变量A重新赋值[jinghnag@hadoop101datas] A=8
[jinghnag@hadoop101 datas]$ echo A8(3)撤销变量A[jinghnag@hadoop101datas]A 8 (3)撤销变量A [jinghnag@hadoop101 datas]A8(3)撤销变量A[jinghnag@hadoop101datas] unset A
[jinghnag@hadoop101 datas]$ echo A(4)声明静态的变量B=2,不能unset[jinghnag@hadoop101datas]A (4)声明静态的变量B=2,不能unset [jinghnag@hadoop101 datas]A(4)声明静态的变量B=2,不能unset[jinghnag@hadoop101datas] readonly B=2
[jinghnag@hadoop101 datas]$ echo B2[jinghnag@hadoop101datas]B 2 [jinghnag@hadoop101 datas]B2[jinghnag@hadoop101datas] B=9
-bash: B: readonly variable
(5)在bash中,变量默认类型都是字符串类型,无法直接进行数值运算
[jinghnag@hadoop101 ~]$ C=1+2
[jinghnag@hadoop101 ~]$ echo C1+2(6)变量的值如果有空格,需要使用双引号或单引号括起来[jinghnag@hadoop101 ]C 1+2 (6)变量的值如果有空格,需要使用双引号或单引号括起来 [jinghnag@hadoop101 ~]C1+2(6)变量的值如果有空格,需要使用双引号或单引号括起来[jinghnag@hadoop101 ] D=I love banzhang
-bash: world: command not found
[jinghnag@hadoop101 ~]$ D=“I love banzhang”
[jinghnag@hadoop101 ~]$ echo AIlovebanzhang(7)可把变量提升为全局环境变量,可供其他Shell程序使用export变量名[jinghnag@hadoop101datas]A I love banzhang (7)可把变量提升为全局环境变量,可供其他Shell程序使用 export 变量名 [jinghnag@hadoop101 datas]AIlovebanzhang(7)可把变量提升为全局环境变量,可供其他Shell程序使用export变量名[jinghnag@hadoop101datas] vim helloworld.sh
在helloworld.sh文件中增加echo $B
#!/bin/bash
echo “helloworld”
echo $B
[jinghnag@hadoop101 datas]$ ./helloworld.sh
Helloworld
发现并没有打印输出变量B的值。
[jinghnag@hadoop101 datas]$ export B
[jinghnag@hadoop101 datas]$ ./helloworld.sh
helloworld
2
4.3 特殊变量:$n
1.基本语法
$n (功能描述:n为数字,$0代表该脚本名称,$1-9代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如9代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如9代表第