目录

1、shell的概述

2、脚本的调用形式

3、shell语法初识

4、变量

5、预设变量

6、变量的扩展

7、条件测试

8、控制语句

9、函数


1、shell的概述

shell 是一种脚本语言
脚本:本质是一个文件,文件里面存放的是 特定格式的指令,系统可以使用脚本解析器 翻译或解析 指令 并执行(它不需要编译)
shell 既是应用程序 又是一种脚本语言(应用程序 解析 脚本语言)
shell命令解析器:
系统提供 shell命令解析器: sh ash bash
查看自己linux系统的默认解析:echo $SHELL

ansable sudo 运行shell脚本 shell脚本使用sudo_linux

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_02

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_03

shell脚本是一种脚本语言,我们只需使用任意文本编辑器,按照语法编写相应程序,增加可执行权限,即可在安装shell命令解释器的环境下执行


2、脚本的调用形式

打开终端时系统自动调用:/etc/profile 或 ~/.bashrc

/etc/profile
此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行,系统的公共环境变量在这里设置
开始自启动的程序,一般也在这里设置
~/.bashrc
用户自己的家目录中的.bashrc
登录时会自动调用,打开任意终端时也会自动调用
这个文件一般设置与个人用户有关的环境变量,如交叉编译器的路径等等
用户手动调用:用户实现的脚本

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_04


3、shell语法初识

3.1、定义以开头:#!/bin/bash

#!用来声明脚本由什么shell解释,否则使用默认shell

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_05

3.2、单个"#"号代表注释当前行第一步:编写脚本文件

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_06

第二步:加上可执行权限

1

chmod +x xxxx.sh

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_07

第三步:运行

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_08

三种执行方式 (./xxx.sh bash xxx.sh . xxx.sh)

三种执行方式的不同点(./xxx.sh bash xxx.sh . xxx.sh)

./xxx.sh :先按照 文件中#!指定的解析器解析

如果#!指定指定的解析器不存在 才会使用系统默认的解析器

bash xxx.sh:指明先用bash解析器解析

如果bash不存在 才会使用默认解析器

. xxx.sh 直接使用默认解析器解析

三种执行情况:(重要)

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_09

注意:windows下 写脚本 在linux下执行 注意

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_10

执行结果:

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_11

将windows文件 转换成 unix文件
方法一:dos2unix 如果没有该插件 需要安装
sudo apt-get install dos2unix
dos2unix shell脚本
转换成功就可以执行运行
方法二:
需要用vi打开脚本,在最后一行模式下执行
:set ff=unix

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_12


4、变量

定义变量
变量名=变量值
如:num=10
引用变量
$变量名
unset :清除变量值

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_13

运行结果:

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_14

从键盘获取值read

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_15

运行结果:

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_16

案例:

在一行上显示和添加提示 需要加上-p

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_17

运行结果:

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_18

案例:读取多个值

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_19

运行结果:

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_20

案例只读变量:

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_21

运行结果:

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_22

查看环境变量:env

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_23

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_24

导出环境变量(其他shell识别该变量)

source 脚本文件
source命令用法:
source FileName
作用:在当前bash环境下读取并执行FileName中的命令。
注:该命令通常用命令“.”来替代。
如:source .bash_rc 与 . .bash_rc 是等效的。
注意:source命令与shell scripts的区别是,
source在当前bash环境下执行命令,而scripts是启动一个子shell来执行命令。这样如果把设置环境变量(或alias等等)的命令写进scripts中,就只会影响子shell,无法改变当前的BASH,所以通过文件(命令列)设置环境变量时,要用source 命令。

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_25

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_26

可以在终端中读取:

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_27

在其他sh脚本读取:

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_28

运行结果:

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_29

注意事项:

1、变量名只能包含英文字母下划线,不能以数字开头
1_num=10 错误
num_1=20 正确
2、等号两边不能直接接空格符,若变量中本身就包含了空格,则整个字符串都要用双引号、或单引号括起来
3、双引号 单引号的区别
双引号:可以解析变量的值
单引号:不能解析变量的值

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_30

运行结果:

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_31

如果想在PATH变量中 追加一个路径写法如下:(重要!!!!)

1

export PATH=$PATH:/需要添加的路径


5、预设变量

shell直接提供无需定义的变量

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_32

案例:

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_33

运行结果:

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_34

脚本标量的特殊用法

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_35

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_36

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_37

加-e转义 才起换行作用

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_38

()由子shell 完成

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_39

{}由当前的shell执行

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_40


6、变量的扩展

6.1、判断变量是否存在

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_41

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_42

6.2、字符串的操作

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_43


7、条件测试

test命令:用于测试字符串、文件状态和数字
test命令有两种格式:
test condition 或[ condition ](结果:1:否;0:是)
使用方括号时,要注意在条件两边加上空格。

7.1、文件测试

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_44

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_45

7.2、字符串测试

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_46

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_47

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_48

7.3、数值测试

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_49

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_50

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_51

7.4、符合语句测试

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_52

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_53


8、控制语句

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_54

8.1、if控制语句

1

2

3

4

5

6

7

8

9

10

11

12

13

14

格式一:

if [条件1]; then

执行第一段程序

else

执行第二段程序

fi

格式二:

if [条件1]; then

执行第一段程序

elif [条件2];then

执行第二段程序

else

执行第三段程序

fi

案例:

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_55

案例:判断当前路径下有没有文件夹 有就进入创建文件 没有 就创建文件夹 再进入创建文件

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_56

运行结果:

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_57

案例:

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_58

运行结果:

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_59

8.2、case

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_60

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_61

8.3、for循环语句

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_62

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_63

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_64

案例:

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_65

案例:扫描当前文件

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_66

8.4、while

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_67

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_68

8.5、until

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_69

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_70

8.6、break continue

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_71


9、函数

ansable sudo 运行shell脚本 shell脚本使用sudo_shell_72

所有函数在使用前必须定义,必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用

ansable sudo 运行shell脚本 shell脚本使用sudo_linux_73

案例:求最值

ansable sudo 运行shell脚本 shell脚本使用sudo_开发语言_74

案例:函数分文件

ansable sudo 运行shell脚本 shell脚本使用sudo_运维_75

fun.sh

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_76

24_sh.sh

ansable sudo 运行shell脚本 shell脚本使用sudo_bash_77

到此这篇关于shell脚本语言的使用(超全超详细)的文章就介绍到这了