shell

编译语言分类
1、编译语言
执行之前需要专门的编译过程,运行时不需要编译,执行效率高、依赖编译器、跨平台性差(例:c c++)
2、解释语言
程序不需要编译,程序运行时解释器翻译成机器语言,执行一次,翻译一次,效率低(例:Python、javaScriptshell)
Shell
shell是一个命令行解释器,接收应用程序、用户命令,然后调用操作系统内核。是功能相当强大的编程语言,易编写、易调试、灵活性强。
概括:shell是一个基于linux内核和应用程序之间的一个解释器
Shell解析器
(1)Liunx提供的解析器有:
[heyali@jinghang ~]$ cat /etc/shells
/bin/sh    是bash的一个快捷方式
/bin/bash    目前多用的是bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
(2)bash和sh的关系
[heyali@jinghang ~]$ cd /bin
[heyali@jinghang bin]$ ll | grep bash
-rwxr-xr-x. 1 root root 942200 3月  23 2017 bash
lrwxrwxrwx. 1 root root      4 12月 11 20:10 sh -> bash
软连接指向bash,两个是同一种
(3)Centos默认的解析器是bash
[heyali@jinghang bin]$ echo $SHELL
/bin/bash

1脚本格式

脚本以#!/bin/bash开头(指定解析

2第一个Shell脚本helloworld

1)需求:创建一个Shell脚本,输出helloworld

2案例实操:

[jinghnag@hadoop101 datas]$ touch helloworld.sh

[jinghnag@hadoop101 datas]$ vi helloworld.sh

 helloworld.sh中输入如下内容

#!/bin/bash

echo "helloworld"

3脚本的常用执行方式

种:采用bashsh+脚本相对路径或绝对路径(不用赋予脚本+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

Shell脚本入门
脚本是将需要执行的命令保存到文本中,按照顺序执行,他是解释型的,不需要编译
1、脚本格式
2、脚本以#!/bin/bash开头(指定解析器)
#!/bin/bash
#添加描述信息
#Name:谁编写的脚本
#DESC:脚本的描述,作用是什么
#PATH:路径(脚本的路径)
#CREATETIME:创建日期
#UPATETIME:更新日期
实例:
创建一个shell脚本:
1.删除class21下的test文件夹
2.在class21新建一个文件夹test2,在test2中创建3个子文件夹dir1,dir2,dir3
3.在dir2中新建一个文件student.txt
4.在student.txt中添加一个zhangsan
5.打印“任务执行完毕,2019-12-14 09:32:00”

3、脚本常用执行方式
第一种:采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)
不建议的执行方法:
[root@jinghang class21]# bash shell01.sh
任务执行完毕 2019-12-14 15:22:48
第二种:采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x,推荐采用这种方式)
注意:第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限。
执行脚本:
bash + 相对路径
bash + 绝对路径
sh + 相对路径
sh + 绝对路径
source + 绝对路径
            推荐的执行方式:(相对路径、绝对路径执行)
                1.为脚本添加可执行的权限
                    chmod +x 脚本名称.sh
123
首先要赋予helloworld.sh 脚本的+x权限
[root@jinghang class21]# chmod +x shell01.sh
[root@jinghang class21]# ll
总用量 12
-rwxr-xr-x. 1 root root  212 12月 14 15:20 shell01.sh
-rwxr-xr-x. 1 root root  204 12月 14 15:00 shell.sh
drwxr-xr-x. 2 root root 4096 12月 14 15:27 test
                2.相对路径(在脚本所在的文件夹下)
                    ./脚本名称.sh
12
[root@jinghang class21]# ./shell01.sh
任务执行完毕 2019-12-14 15:28:35
[root@jinghang class21]# ll
                3.绝对路径执行
                    /root/class21/脚本名称.sh
          [root@jinghang class21]#  /root/class21/shell01.sh
123
任务执行完毕 2019-12-14 15:52:58
                注意:
                    脚本中的第一行,指明解释器,一定不要写错
                    
        扩展命令
        bash -n 脚本名称.sh  #查找脚本的语法错误
        bash -x 脚本名称.sh  #查看脚本的执行过程  
123456
Shell中的变量
变量:临时保存变量,可变化数据

 

1. 常用系统变量

$HOME$PWD$SHELL$USER

2案例实操

1)查看系统变量的值

[jinghnag@hadoop101 datas]$ echo $HOME

/home/jinghnag

2显示当前Shell中所有变量:set

[jinghnag@hadoop101 datas]$ set

BASH=/bin/bash

BASH_ALIASES=()

BASH_ARGC=()

BASH_ARGV=()

自定义变量

1基本语法

1)定义变量:变量= 

2)撤销变量:unset 变量

3)声明静态变量:readonly变量,注意:不能unset

2变量定义规则

1)变量名称可以由字母、数字和下划线组成,但是不能以数字开头环境变量名建议大写

2)等号两侧不能有空格

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

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

3案例实操

1)定义变量A

[jinghnag@hadoop101 datas]$ A=5

[jinghnag@hadoop101 datas]$ echo $A

5

2变量A重新赋值

[jinghnag@hadoop101 datas]$ A=8

[jinghnag@hadoop101 datas]$ echo $A

8

3)撤销变量A

[jinghnag@hadoop101 datas]$ unset A

[jinghnag@hadoop101 datas]$ echo $A

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

[jinghnag@hadoop101 datas]$ readonly B=2

[jinghnag@hadoop101 datas]$ echo $B

2

[jinghnag@hadoop101 datas]$ B=9

-bash: B: readonly variable

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

[jinghnag@hadoop101 ~]$ C=1+2

[jinghnag@hadoop101 ~]$ echo $C

1+2

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

[jinghnag@hadoop101 ~]$ D=I love banzhang

-bash: world: command not found

[jinghnag@hadoop101 ~]$ D="I love banzhang"

[jinghnag@hadoop101 ~]$ echo $A

I love banzhang

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

export 变量名

[jinghnag@hadoop101 datas]$ 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

特殊变量$n

1基本语法

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

2案例实操

1)输出该脚本文件名称输入参数1和输入参数2

[jinghnag@hadoop101 datas]$ touch parameter.sh

[jinghnag@hadoop101 datas]$ vim parameter.sh

 

#!/bin/bash

echo "$0  $1   $2"

 

[jinghnag@hadoop101 datas]$ chmod 777 parameter.sh

 

[jinghnag@hadoop101 datas]$ ./parameter.sh cls  xz

./parameter.sh  cls   xz

$#

1基本语法

$# (功能描述:获取所有输入参数个数,常用于循环

2案例实操

1获取输入参数的个数

[jinghnag@hadoop101 datas]$ vim parameter.sh

 

#!/bin/bash

echo "$0  $1   $2"

echo $#

 

[jinghnag@hadoop101 datas]$ chmod 777 parameter.sh

 

[jinghnag@hadoop101 datas]$ ./parameter.sh cls  xz

parameter.sh cls xz

2

$*$@

1基本语法

$* (功能描述:这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体)

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

2案例实操

1)打印输入所有参数

[jinghnag@hadoop101 datas]$ vim parameter.sh

 

#!/bin/bash

echo "$0  $1   $2"

echo $#

echo $*

echo $@

 

[jinghnag@hadoop101 datas]$ bash parameter.sh 1 2 3

parameter.sh  1   2

3

1 2 3

1 2 3

$?

1基本语法

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

2案例实操

1判断helloworld.sh脚本是否正确执行

[jinghnag@hadoop101 datas]$ ./helloworld.sh

hello world

[jinghnag@hadoop101 datas]$ echo $?



原文地址:https://www.cnblogs.com/qu125-tf/p/12045725.html