当前位置: 首页 > news >正文

Shell学习——shell中的变量

目录

一、父shell和子shell:

二、系统预定变量

定义方式:

脚本举例

​编辑

四、只读变量

五、撤销变量

六、小结

七、特殊变量

$n

$#

$*、$@

$?


一、父shell和子shell:

        由于shell的原理可以理解为套娃,因此有父shell和子shell的概念

[root@hadoop-master sh_test]# ps -f
UID         PID   PPID  C STIME TTY          TIME CMD
root       1876   1872  0 09:06 pts/0    00:00:00 -bash
root       1896   1876  0 09:06 pts/0    00:00:00 ps -f[root@hadoop-master sh_test]# bash
[root@hadoop-master sh_test]# ps -f
UID         PID   PPID  C STIME TTY          TIME CMD
root       1876   1872  0 09:06 pts/0    00:00:00 -bash
root       1897   1876  0 09:07 pts/0    00:00:00 bash
root       1908   1897  0 09:07 pts/0    00:00:00 ps -f[root@hadoop-master sh_test]# exit
exit
[root@hadoop-master sh_test]# ps -f
UID         PID   PPID  C STIME TTY          TIME CMD
root       1876   1872  0 09:06 pts/0    00:00:00 -bash
root       1974   1876  0 09:39 pts/0    00:00:00 ps -f

         可以看到,原本有一个登录进来启动的bash shell,我们再敲一个bash,会出现一个子进程,接下来执行脚本,将在子shell里面执行,可以使用exit退出。

        开启父子shell需要特别注意环境变量的继承关系,变量即内存当中存储的数据,由于shell是套壳的,因此系统变量对父shell及子shell都可以产生作用,但在子shell中设置的当前变量,父shell不可见。

二、系统预定变量

        环境变量即使用变量存储当前shell会话和工作环境的相关信息,

        $HOME、$PWD、$SHELL、$USER

[root@hadoop-master sh_test]# echo $USER
root
[root@hadoop-master sh_test]# printenv HOME
/root#用set可以看到所有的变量
[root@hadoop-master sh_test]# set | less

三、用户自定义变量

定义方式:

        =号两边不能有空格,有的话要用引号引起来

[root@hadoop-master sh_test]# a="my var"
[root@hadoop-master sh_test]# ps -f
UID         PID   PPID  C STIME TTY          TIME CMD
root       2067   2063  0 11:04 pts/0    00:00:00 -bash
root       2158   2067  0 11:18 pts/0    00:00:00 ps -f
[root@hadoop-master sh_test]# echo $a
my var

         进入子shell变量,局部变量就失效了:

[root@hadoop-master sh_test]# bash
[root@hadoop-master sh_test]# echo $a
[root@hadoop-master sh_test]# ps -f
UID         PID   PPID  C STIME TTY          TIME CMD
root       2067   2063  0 11:04 pts/0    00:00:00 -bash
root       2170   2067  0 11:19 pts/0    00:00:00 bash
root       2181   2170  0 11:19 pts/0    00:00:00 ps -f[root@hadoop-master sh_test]# exit
exit

        使用export可以将局部变量升级为全局变量

[root@hadoop-master sh_test]# export a
[root@hadoop-master sh_test]# bash
[root@hadoop-master sh_test]# echo $a
my var

        但是,在子shell里面的变量修改不会改变外层父shell的值,如

[root@hadoop-master sh_test]# ps -f
UID         PID   PPID  C STIME TTY          TIME CMD
root       2067   2063  0 11:04 pts/0    00:00:00 -bash
root       2170   2067  0 11:19 pts/0    00:00:00 bash
root       2183   2170  0 11:24 pts/0    00:00:00 ps -f
[root@hadoop-master sh_test]# a="your var"
[root@hadoop-master sh_test]# echo $a
your var
[root@hadoop-master sh_test]# exit
exit
[root@hadoop-master sh_test]# ps -f
UID         PID   PPID  C STIME TTY          TIME CMD
root       2067   2063  0 11:04 pts/0    00:00:00 -bash
root       2184   2067  0 11:24 pts/0    00:00:00 ps -f
[root@hadoop-master sh_test]# echo $a
my var

脚本举例

        其中:

        写个打印脚本执行:

[root@hadoop-master sh_test]# ./hello.sh
Hi,dear~[root@hadoop-master sh_test]# source hello.sh
Hi,dear~
hi,my var

        由于直接用相对/绝对路径会新开启一个子shell,作为局部变量的my_var是打印不出来的,而source在当前bash环境直接执行

        使用export将my_var升级为全局变量后,子shell也可以使用了:

[root@hadoop-master sh_test]# export my_var
[root@hadoop-master sh_test]# ./hello.sh
Hi,dear~
hi,my var

四、只读变量

        只能读取不能修改

[root@hadoop-master sh_test]# readonly b=5
[root@hadoop-master sh_test]# b=10
-bash: b: 只读变量

五、撤销变量

        使用unset,只读变量不能撤销

[root@hadoop-master sh_test]# unset b
-bash: unset: b: 无法反设定: 只读 variable

六、小结

  • 变量名称可由数字、变量、下划线组成,但不能以数字开头,环境变量名建议大写。
  • 等号两变不能空格
  • bash中,变量默认字符串类型,无法直接进行数值计算。
  • 变量值若有空格需要用引号引起来
  • export、unset、readonly的作用

七、特殊变量

        对脚本的输入参数进行捕获和处理

$n

n为数字:

  • 0:脚本名称
  • 1-9:第1-9个参数
  • 10或以上:参数需要用{}括起来如${10}

[root@hadoop-master sh_test]# hello.sh xiaoliang
Hi,dear~
hi,xiaoliang

        区别单双引号

[root@hadoop-master sh_test]# ./hello.sh
This is ./hello.sh
Hi,dear~
hi,
==========
=====$n=====

$#

        获取输入参数的个数,对于测试脚本:

[root@hadoop-master sh_test]# ./hello.sh 
This is ./hello.sh
hi,
hi,
=====0个输入参数=====

        添加输入参数:

[root@hadoop-master sh_test]# ./hello.sh windows
This is ./hello.sh
hi,windows
hi,
=====1个输入参数=====[root@hadoop-master sh_test]# ./hello.sh windows linux
This is ./hello.sh
hi,windows
hi,linux
=====2个输入参数=====

$*、$@

        均代表命令行中所有参数:

[root@hadoop-master sh_test]# ./hello.sh windows linux
This is ./hello.sh
hi,windows
hi,linux
=====$*=====
windows linux
=====$@=====
windows linux

        区别:$*将所有参数当做一个整体,而$@将参数当做一个数组,可以在循环中进行遍历

$?

        最后一次执行命令的返回状态,若为0,证明上一次命令正确执行,若非0,证明上一个命令执行不正确(具体哪个数由命令自己决定)


http://www.mrgr.cn/news/55302.html

相关文章:

  • 案例分享—国外优秀UI卡片设计作品赏析
  • nbsaas vue3管理后台框架
  • 滚雪球学Redis[7.0讲]:Redis在Web应用中的会话管理:实现、优化与安全性!
  • Vue3中ref和reactive的对比
  • 前端面经
  • 【Flutter】Dart:运算符
  • 1.深入理解MySQL索引底层数据结构与算法
  • FuLID-Flux在ComfyUI下报错的问题解决办法
  • 贪心day6
  • 2024年双11买什么最划算?双十一购物清单,双十一囤货清单排名
  • 高速缓冲存储器Cache是如何工作的、主要功能、高速缓冲存储器Cache和主存有哪些区别
  • 如何通过ChatGPT快速编写代码、解决bug、优化代码、推荐技术解决方案(完整教程)
  • 【AscendC】配置ModelArts的算子开发环境
  • Transformer(Vit+注意力机制)
  • JDBC——(3)
  • 如何修改Ubuntu系统的共享内存shm大小
  • 在西班牙买可乐喝时常用的句子,柯桥西班牙语培训
  • 使用Python处理API数据时,有哪些常见的数据清洗技巧?
  • 推荐一款专为Nginx设计的图形化管理工具: Nginx UI!
  • Docker笔记-搭建私有仓库
  • AI大模型混战后,以知识为中心驱动的人工智能迎来风口?
  • HTB:Optimum[WriteUP]
  • C++:模板进阶
  • LLM之Agent(十二)| OpenAI Agent-Swarm简单入门
  • RequestBody接收参数报错com.fasterxml.jackson.databind.exc.MismatchedInputException
  • 移动剧院:未来活动场馆的全新选择—轻空间