详解shell脚本的环境、普通、特殊变量

时间:2022-07-24
本文章向大家介绍详解shell脚本的环境、普通、特殊变量,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

#变量介绍 变量就是用一个固定的字代替复杂的内容,该内容里可能还会包含变量、路径、字符串等其它的内容。

#变量类型

分为环境变量(全局变量)和普通变量(局部变量)
环境变量也可称全局变量
普通变量又称局部变量

一、环境变量

环境变量一般用export内置命令导出变量,用于定义shell的运行环境,所有的环境变量都是系统全局变量 #提示1:定义的变量均为大写,环境变量应用于用户进程前,使用export命令导出 #提示2:变量赋值两边应没有空格 #示例:export DIR=/data/

#设置环境变量

#在用户家目录或全局配置中进行设置
.bashrc
/etc/bashrc
/etc/profile

#显示与取消环境变量

#1.通过echo或print命令打印环境变量
$HOME : 用户登录时进入的目录
$UID :当前用户的UID,相当于id-u
$PWD :当前工作目录的绝对路径
$SHELL :当前SHELL
$USER : 当前用户

#示例
[root@game ~]# echo $HOME
/root
[root@game ~]# echo $UID
0
[root@game ~]# echo $PWD
/root
[root@game ~]# echo $SHELL
/bin/bash
[root@game ~]# echo $USER
root

#2.通过env或set显示默认的环境变量
#3.通过unset消除本地变量和环境变量
[root@game ~]# echo $PWD
/root
[root@game ~]# unset PWD
[root@game ~]# echo $PWD

[root@game ~]#

#自定义环境变量

命令:export或declare内置命令

#格式
export 变量名=value
declare -x 变量名 value

#示例
[root@game ~]# export DIRR=guoke
[root@game ~]# declare -x DIRR=guoke

#自定义全局变量示例

[root@game ~]# cat /etc/profile |grep guoke
export DIRR='guoke'
[root@game ~]# source /etc/profile
[root@game ~]# echo $DIRR
guoke
[root@game ~]# env |grep DIRR
DIRR=guoke

二、普通变量

本地变量一般在脚本或命令中进行定义,只在当前的shell中有效

#变量名一般是由字母、数字、下划线组成,可以以字母或下划线开头

#普通变量定义

变量名=value #不加引号
变量名='value' #加单引号
变量名="value" #加双引号

#示例
[root@game test]# cat test.sh
mail=guoke@qq.com

三、特殊变量

#常用的特殊变量

特殊变量	解释
$0	获取当前执行的脚本文件名,如果脚本包含路径,则打印脚本路径
$n	获取第n个执行脚本参入的值,当n大于9,使用{}阔起来
$#	获取脚本传参数得总个数
$*	获取脚本所有传参的参数
$@	获取脚本所有传参的参数
$?	获取上一个命令执行的返回值,0为成功,非0为失败
$$	获取脚本的进程号PID
$!	获取上一次执行脚本的pid
$_	获取上一条命令的最后一个参数值
#获取更多帮助使用man bash,搜索关键字:"Special Parameters"

#详解 1.$0:获取执行脚本的名字,当n大于9,使用大括号{}括起来,防止输出不正确 #示例

#获取脚本的名称路径
[root@game test]# cat test.sh 
#!/bin/bash
echo $0
#执行效果
[root@game test]# sh test.sh 
test.sh

#带路径就显示路径
[root@game test]# sh /server/scripts/test/test.sh 
/server/scripts/test/test.sh

#扩展:dirname,basename dirname:获取脚本的路径 basename:获取脚本的名字

#简单示例
[root@game test]# dirname /server/scripts/test/test.sh 
/server/scripts/test
[root@game test]# basename /server/scripts/test/test.sh 
test.sh

#企业应用

[root@game test]# cat test5.sh 
#!/bin/bash
MY_PATH=$(cd `dirname $0`; pwd)
echo ${MY_PATH}

#执行效果
[root@game test]# sh test5.sh 
/server/scripts/test

2.$n:执行脚本传入的参数值 #简单示例

#接收一个传入的参数
[root@game test]# cat test.sh 
#!/bin/bash
echo $1 #打印脚本传递的第一个参数的值
#执行效果
[root@game test]# sh test.sh guoke
guoke

#接收2个参数
[root@game test]# cat test.sh 
#!/bin/bash
echo $1 $2
[root@game test]# sh test.sh guoke guoke1
guoke guoke1

#当需要传入的参数大于9,使用大括号括起来
[root@game test]# cat test.sh 
#!/bin/bash
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11}

#企业场景定义的传参方式

[root@game test]# cat test6.sh  #执行脚本需要接3个参数
#!/bin/bash
usage(){
   echo "usage:
$0 key start end
example
$0 ff 1 2"
}

3.$#:获取传参的总个数 #简单示例

[root@game test]# cat test.sh 
#!/bin/bash
echo $1 $2 $3 $4 $5 $6
echo $#
#执行效果
[root@game test]# sh test.sh  1 2 3 4 5 6
1 2 3 4 5 6
6

#企业场景应用

[root@game test]# cat test6.sh 
#!/bin/bash
usage(){
   echo "usage:
$0 key start end
example
$0 ff 1 2"
}
[ $# -ne 3 ] && usage && exit 1 
#判断传入的参数如果不等于3,那么就打印帮助函数,退出脚本

4.*:获取当前脚本所有传参的参数,不加引号和@相同

#简单示例

#通过set设置3个参数进行打印
[root@game ~]# set wo me guoke
[root@game ~]# echo $@
wo me guoke
[root@game ~]# echo $*
wo me guoke

5.$?:获取上一个指令的返回值

#说明:$?一般是用来判断命令或脚本是否执行成功,返回值为0就说明执行成功,否则就是失败
#简单示例:判断命令是否执行成功
[root@game ~]# cd /server/scripts/test/
[root@game test]# echo $?
0 #命令执行成功就会返回0
[root@game test]# cd /server/scripts/guoke
-bash: cd: /server/scripts/guoke: No such file or directory
[root@game test]# echo $?
1 #如果命令执行失败就会返回非0

6.$:获取当前执行的脚本的进程号7.!:获取上一次执行脚本的pid