shell编程

目录

shell简介

shell变量

用户变量

环境变量

特殊变量

字符串数据类型

shell命令和shell脚本

案例1:脚本中变量的操作

案例2:脚本中获取参数


shell简介

shell语言:是一个C语言编写的脚本语言,它是用户与linux的桥梁

shell编程:通常指的是学习shell命令语法后,利用这套语法开发脚本程序,操作,访问内核服务,而不是使用C语言去编写一个shell程序

shell解释器:unix默认sh,linux的默认是bash,shell,bash兼容sh,针对sh编写的shell代码可以不加修饰地在bash中运行

shell变量

shell中有3中变量: 用户变量    环境变量    特殊变量

        用户变量:编程过程中使用量最多

        环境变量:主要在程序运行时需要设置

        特殊变量;在对参数判断和命令返回值判断时使用

变量的定义语法:变量名=变量值

变量的定义需要遵循的规则

1.变量名可以由字母,数字,下划线组成,但是不能以数字开头,不能使用关键字,严格区分大小写

2.在bash中,变量的默认类型都是字符串型,如果要进行数值运算,则必须使用特殊命令

3.变量用等号"="连接值,"="左右两侧不能有空格

4.变量值中如果有空格,则需要使用单引号或者双引号包含,如test="hello python"

5.在变量值中,可以使用转义符"\"

用户变量

定义普通变量: 变量名=变量值                    注意:"="两侧不能有空格

定义只读变量: readonly 变量名=变量值     注意:只读变量生命周期窗口关闭或者脚本执行结束

获取变量值 $变量名 或者 ${变量名}           建议:使用${变量名}它能标识变量的边界范围

删除普通变量: unset 变量名                       注意:unset不能删除只读变量

修改普通变量值:已有变量名=新变量值

修改普通变量为只读变量:readonly 变量名

                              
[root@note1 test]# 
[root@note1 test]# name='张三'
[root@note1 test]# echo name
name
[root@note1 test]# echo $name
张三
[root@note1 test]# echo $name666

[root@note1 test]# echo ${name}666
张三666
[root@note1 test]# unset name
[root@note1 test]# echo ${name}

[root@note1 test]# echo ${name}666
666
[root@note1 test]# readonly age=18
[root@note1 test]# echo $age
18
[root@note1 test]# age=19
-bash: age: 只读变量
[root@note1 test]# unset age
-bash: unset: age: 无法反设定: 只读 variable
[root@note1 test]# 登出

Last login: Tue Nov  7 10:21:56 2023 from 192.168.88.1
[root@note1 ~]# echo $age

[root@note1 ~]# age=16
[root@note1 ~]# echo $age
16
[root@note1 ~]# 

环境变量

环境变量:是操作系统(windows,Linux,Mac)在运行的时候,记录的一些关键信息,用以辅助系统运行

注意:环境变量的名字习惯上使用大写字母

查看系统记录的环境变量:env

常见的环境变量:HOSTNAME SHELL USER PATH...

自定义环境变量格式:export 变量名=变量值

        临时设置:直接命令形式编写              注意:窗口关闭就是失效

        永久设置:在/etc/profile文件中编写    注意:编写重新登录生效或者source/etc/profile

[root@cent1 test22]# env
HOSTNAME=cent1
SHELL=/bin/bash
USER=root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@cent1 test22]# echo $SHELL
/bin/bash
[root@cent1 test22]# echo $USER
root
[root@cent1 test22]# echo $HOSTNAME
cent1
[root@cent1 test22]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@cent1 test22]# env | grep PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@note1 ~]# export sisi=pretty
[root@note1 ~]# echo $sisi
pretty
[root@cent1 ~]# vim /etc/profile
...
# 文件最后一行添加并保存
export BIGDATA=666888
[root@cent1 ~]# source /etc/profile
[root@cent1 ~]# exit
logout
login
[root@cent1 ~]# echo $BIGDATA
666888

特殊变量

特殊变量:可以在执行shell脚本时,向脚本传递参数,这时候可以使用特殊变量来获取参数

作用:可以向shell脚本传递参数,某些配置属性不用写死在脚本中

动态传递参数的方式:sh  shell脚本文件 参数1 参数2 ...

shell常用的特殊变量在shell脚本内部支持接收并使用:

        $0:获取当前脚本文件的名称

        $n:根据数字顺序获取对应的参数    注意:n代表1,2,3,4,5...

        $#:获取参数的总个数

        $*:获取所有的参数内容

字符串数据类型

在Bash中,变量的默认数据类型都是字符串

定义字符串: 变量名=值  变量名='值'  变量名="值"

建议:使用双引号定义字符串,因为单引号括起来内容都是普通字符,双引号括起来的内容会识别特殊含义,比如:$和``

[root@note1 ~]# name=sisi  age=18 gender="女"
[root@note1 ~]# echo $name $age $gender
sisi 18 女
[root@note1 ~]# echo '我的名字是:$name 我的年龄是:$age 我的性别是:$gender'
我的名字是:$name 我的年龄是:$age 我的性别是:$gender
[root@note1 ~]# echo "我的名字是:$name 我的年龄是:$age 我的性别是:$gender"
我的名字是:sisi 我的年龄是:18 我的性别是:女
[root@note1 ~]# echo "$name $age $gender"
sisi 18 女
[root@note1 ~]# date +%Y-%m-%d
2023-11-07
[root@note1 ~]# DT1=date
[root@note1 ~]# echo $DT1
date
[root@note1 ~]# DT=`date`
[root@note1 ~]# echo $DT
2023年 11月 07日 星期二 20:31:02 CST
[root@note1 ~]# echo "现在的日期是${DT}"
现在的日期是2023年 11月 07日 星期二 20:31:02 CST
[root@note1 ~]# echo "现在的日期是`date`"
现在的日期是2023年 11月 07日 星期二 20:33:16 CST

shell命令和shell脚本

shell命令和shell脚本:本质上都是属于shell编程

shell命令:倾向于在linux命令行中使用,适合逻辑简单场景

shell脚本:倾向于在脚本文件中编写,适合复杂逻辑处理

        注意:1.shell脚本文件一般以.sh结尾  2.文件第一行一般为#!/bin/bash

shell脚本的执行方式:

方式一:sh在相对路径中执行

        格式: sh 脚本

        注意:1.需要进入脚本所在的工作目录,然后使用对应的sh命令来执行脚本

                2.这种执行方式,脚本文件不需要具有可执行权限

方式二:相对路径执行

        格式: ./脚本

       注意:1.需要进入脚本所在的工作目录,然后使用./脚本方式执行

                2.这种执行方式,必须保证脚本文件具有可执行权限

方式三:绝对路径执行

        格式: /绝对路径/脚本

       注意:1.需要使用脚本的绝对路径执行,指的是直接从根目录/到脚本目录绝对路径

                2.这种执行方式,必须保证脚本文件具有可执行权限        

shell脚本也可以动态传参:sh 脚本 参数1 参数2...

注意:脚本内部可以使用特殊变量获取

案例1:脚本中变量的操作

创建一个s1.sh脚本文件,要求此脚本内先定义一个字符串变量str并赋值sisi666,然后打印该字符串 最后保存并执行此脚本

注意: ①shell脚本后缀名没有要求,通常以.sh结尾

在shell中除了第一行的#表示特殊格式外,其他地方的#符号一般表示注释。 ②#! 是一个约定的标记,它告诉系统 这个脚本需要什么解释器来执行 ③相对路径方式如果不加./直接执行脚本 默认去系统环境变量中查找

[root@note1 test]# vim s.sh
#!/bin/bash
str=sisi666
echo $str
~                                                                       
~                                                                       
~                                                                       
~                                                                       
~                                                                       
                                                                    
"s.sh" [新] 3L, 34C 已写入                            
[root@note1 test]# sh s.sh
sisi666
[root@note1 test]# ./s.sh
-bash: ./s.sh: 权限不够
[root@note1 test]# /test/s.sh
-bash: /test/s.sh: 权限不够
[root@note1 test]# ll
总用量 20
-rw-r--r--. 1 root root  16 11月  7 21:10 s1.sh
-rwxr-xr-x. 1 root root  85 11月  7 14:26 s2.sh
-rw-r--r--. 1 root root  34 11月  7 14:52 s3.sh
-rw-r--r--. 1 root root 174 11月  7 15:00 s4.sh
-rw-r--r--. 1 root root  34 11月  7 21:11 s.sh
[root@note1 test]# chmod +x s.sh
[root@note1 test]# ll
总用量 20
-rw-r--r--. 1 root root  16 11月  7 21:10 s1.sh
-rwxr-xr-x. 1 root root  85 11月  7 14:26 s2.sh
-rw-r--r--. 1 root root  34 11月  7 14:52 s3.sh
-rw-r--r--. 1 root root 174 11月  7 15:00 s4.sh
-rwxr-xr-x. 1 root root  34 11月  7 21:11 s.sh
[root@note1 test]# ./s.sh
sisi666
[root@note1 test]# /test/s.sh
sisi666

案例2:脚本中获取参数

1.编写一个脚本文件s5.sh,在里面使用之前学习的特殊变量分别获取当前脚本文件名称,传入参数个数并分别获取对应参数内容

2.执行s1.sh脚本文件,传入4个参数分别为:姓名:西西,年龄:18,性别:女,身高:170cm

3.使用3种命令方式执行s5.sh脚本文件

[root@note1 test]# vim s5.sh
echo 传入参数4
#!/bin/bash
echo 执行的文件是:$0
echo 传入的参数个数是:$#
echo 传入参数1-$1
echo 传入参数2-$2
echo 传入参数3-$3
echo 传入参数4-$4
echo 传入的所有参数是:$*
~                                                                       
~                                                                       
~                                                                       
~                                                                       
~                                                                                                                                            
"s5.sh" 8L, 193C 已写入                               
[root@note1 test]# sh s5.sh 姓名:西西 年龄:18 性别:女 身高:170cm
执行的文件是:s5.sh
传入的参数个数是:4
传入参数1-姓名:西西
传入参数2-年龄:18
传入参数3-性别:女
传入参数4-身高:170cm
传入的所有参数是:姓名:西西 年龄:18 性别:女 身高:170cm
[root@note1 test]# ./s5.sh
-bash: ./s5.sh: 权限不够
[root@note1 test]# /test/s5.sh
-bash: /test/s5.sh: 权限不够
[root@note1 test]# ll
总用量 24
-rw-r--r--. 1 root root  16 11月  7 21:10 s1.sh
-rwxr-xr-x. 1 root root  85 11月  7 14:26 s2.sh
-rw-r--r--. 1 root root  34 11月  7 14:52 s3.sh
-rw-r--r--. 1 root root 174 11月  7 15:00 s4.sh
-rw-r--r--. 1 root root 193 11月  7 21:25 s5.sh
-rwxr-xr-x. 1 root root  34 11月  7 21:11 s.sh
[root@note1 test]# chmod +x s5.sh
[root@note1 test]# ll
总用量 24
-rw-r--r--. 1 root root  16 11月  7 21:10 s1.sh
-rwxr-xr-x. 1 root root  85 11月  7 14:26 s2.sh
-rw-r--r--. 1 root root  34 11月  7 14:52 s3.sh
-rw-r--r--. 1 root root 174 11月  7 15:00 s4.sh
-rwxr-xr-x. 1 root root 193 11月  7 21:25 s5.sh
-rwxr-xr-x. 1 root root  34 11月  7 21:11 s.sh
[root@note1 test]# ./s5.sh
[root@note1 test]#  ./s5.sh 姓名:西西 年龄:18 性别:女 身高:170cm  
执行的文件是:./s5.sh
传入的参数个数是:4
传入参数1-姓名:西西
传入参数2-年龄:18
传入参数3-性别:女
传入参数4-身高:170cm
传入的所有参数是:姓名:西西 年龄:18 性别:女 身高:170cm
[root@note1 test]#  /test/s5.sh 姓名:西西 年龄:18 性别:女 身高:170cm 
执行的文件是:/test/s5.sh
传入的参数个数是:4
传入参数1-姓名:西西
传入参数2-年龄:18
传入参数3-性别:女
传入参数4-身高:170cm
传入的所有参数是:姓名:西西 年龄:18 性别:女 身高:170cm

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值