shell脚本简单了解

shell介绍

shell变量规则

Shell 和python都是弱语言

1定义变量规则 : 变量名=值

shell定义变量的规则:

等号两边不能有空格
shell脚本简单了解
shell脚本简单了解

定义特殊的变量需要用单引号或者双引号结合

这样看 单引号 和 双引号 没有区别

shell脚本简单了解

但是从这张图我们可以看出区别
shell脚本简单了解

name1也可以变成name2只需要在特殊字符前添加 \ 就可以和name2一样了
shell脚本简单了解

定义linux命令需要用反单引号

Shell取值

使用:echo $变量名

$变量名,可以获取变量名里面的值
shell脚本简单了解

Shell脚本的规则
shell脚本简单了解

1:脚本名称使用.sh结尾

2:第一行必须写# !/bin/bash

3:输入语句用read

4:输出语句用:echo

5: 调用的时候
./脚本名.sh(权限的问题)

sh 脚本名.sh

注意点:read可以传递多个数据 read –p”name and passwoed”name password

1超过的部分都填充在最后一个变量上
shell脚本简单了解
shell脚本简单了解

shell脚本简单了解

shell脚本简单了解

比较运算符
shell脚本简单了解

shell脚本简单了解
shell脚本简单了解

if语句分支结构
格式:

shell脚本简单了解

注意点:
shell脚本简单了解

shell脚本简单了解
shell脚本简单了解
shell脚本简单了解

shell脚本简单了解
shell脚本简单了解
shell脚本简单了解

Case循环语句
case规则

shell脚本简单了解

shell脚本简单了解
shell脚本简单了解

For循环
shell脚本简单了解

shell脚本简单了解

While循环
shell脚本简单了解

shell脚本简单了解
shell脚本简单了解

函数
定义格式
函数名(){代码 }
调用格式
函数名

注意点:必须先定义一个函数,然后再调用它

shell脚本简单了解
shell脚本简单了解

参数传递
函数中的参数
使用$n来接收数据

shell脚本简单了解
shell脚本简单了解

shell脚本简单了解
shell脚本简单了解

返回值
Shell中函数中默认存在返回值
查看函数的返回值$?

注意点:
如果没有代码错误默认返回值为0,
有代码错误返回1
查看返回值内容:$?
shell脚本简单了解
shell脚本简单了解