文章目录
前言
本文根据尚硅谷的课程整理而成,方便各位搬砖人快速掌握Linux的Shell程序。
1.Shell概述
1.1概述
Shell脚本就是把Linux的命令封装成一个类似Java一样的编程脚本。
2.Shell解析器
#(1)Linux提供的Shell解析器有:
[atguigu@hadoop101 ~]$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
#(2)bash和sh的关系
[atguigu@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
[atguigu@hadoop102 bin]$ echo $SHELL
/bin/bash
3.Shell脚本入门
1.脚本格式
脚本以#!/bin/bash开头(指定解析器)
2.第一个Shell脚本:helloworld
#(1)需求:创建一个Shell脚本,输出helloworld
[atguigu@hadoop101 datas]$ touch helloworld.sh
[atguigu@hadoop101 datas]$ vi helloworld.sh
#在helloworld.sh中输入如下内容
#!/bin/bash
echo "helloworld"
3.脚本的常用执行方式
第一种:采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)
#(1) sh+脚本的相对路径
[atguigu@hadoop101 datas]$ sh helloworld.sh
Helloworld
#(2)sh+脚本的绝对路径
[atguigu@hadoop101 datas]$ sh /home/atguigu/datas/helloworld.sh
helloworld
#(3)bash+脚本的相对路径
[atguigu@hadoop101 datas]$ bash helloworld.sh
Helloworld
#(4)bash+脚本的绝对路径
[atguigu@hadoop101 datas]$ bash /home/atguigu/datas/helloworld.sh
Helloworld
第二种:采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)
#(1)首先要赋予helloworld.sh 脚本的+x权限
[atguigu@hadoop101 datas]$ chmod 777 helloworld.sh
#(2)执行脚本
#相对路径
[atguigu@hadoop101 datas]$ ./Helloworld.sh
#绝对路径
[atguigu@hadoop101 datas]$ /home/atguigu/datas/helloworld.sh
注意:第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限。
第三种,用 source helloworld.sh 或者 . helloworld.sh
4.第二个Shell脚本:多命令处理
(1)需求:
在/home/atguigu/目录下创建一个banzhang.txt,在banzhang.txt文件中增加“I love cls”。
[atguigu@hadoop101 datas]$ touch batch.sh
[atguigu@hadoop101 datas]$ vi batch.sh
#在batch.sh中输入如下内容
#!/bin/bash
cd /home/atguigu
touch cls.txt
echo "I love cls" >>cls.txt
4.Shell中的变量
4.1 系统变量
1 常用系统变量
$HOME、$PWD、$SHELL、$USER等
#(1)查看系统变量的值
[atguigu@hadoop101 datas]$ echo $HOME
/home/atguigu
#(2)显示当前Shell中所有变量:set
[atguigu@hadoop101 datas]$ 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)变量的值如果有空格,需要使用双引号或单引号括起来。
#(1)定义变量A
[atguigu@hadoop101 datas]$ A=5
[atguigu@hadoop101 datas]$ echo $A
5
#(2)给变量A重新赋值
[atguigu@hadoop101 datas]$ A=8
[atguigu@hadoop101 datas]$ echo $A
8
#(3)撤销变量A
[atguigu@hadoop101 datas]$ unset A
[atguigu@hadoop101 datas]$ echo $A
#(4)声明静态的变量B=2,不能unset
[atguigu@hadoop101 datas]$ readonly B=2
[atguigu@hadoop101 datas]$ echo $B
2
[atguigu@hadoop101 datas]$ B=9
-bash: B: readonly variable
#(5)在bash中,变量默认类型都是字符串类型,无法直接进行数值运算
[atguigu@hadoop102 ~]$ C=1+2
[atguigu@hadoop102 ~]$ echo $C
1+2
#(6)变量的值如果有空格,需要使用双引号或单引号括起来
[atguigu@hadoop102 ~]$ D=I love banzhang
-bash: world: command not found
[atguigu@hadoop102 ~]$ D="I love banzhang"
[atguigu@hadoop102 ~]$ echo $A
I love banzhang
#(7)可把变量提升为全局环境变量,可供其他Shell程序使用
export 变量名
[atguigu@hadoop101 datas]$ vim helloworld.sh
#在helloworld.sh文件中增加echo $B
#!/bin/bash
echo "helloworld"
echo $B
#[atguigu@hadoop101 datas]$ ./helloworld.sh
Helloworld
#发现并没有打印输出变量B的值。