shell中的变量简介

1.变量的定义

变量即在程序运行过程中它的值是允许改变的量
变量是用一串固定的字符来标示不固定的值的一种方法
变量是一种使用方便的占位符,用于引用计算机内存地址,该地址可以存储Script 运行时可更改的程序信息
在 shell 中变量是不能永久保存在系统中的,必须在文件中声明

2.在 shell 脚本中变量的种类

在 shell 中变量分为环境级变量,用户级变量,系统级变量
(1)环境级变量只在当前 shell 中生效, shell 关闭变量丢失
(2)用户级变量写在用户的骨文件中,只针对当前用户生效
(3)系统级变量被写在系统的配置文件 /etc/profile 或者 /etc/profile.d/ 中,对于所有用户都生效

3.shell 脚本中变量的定义方法

用固定的字符串来表示一个不固定的值
(1)环境级
export a=1
(2)用户级
vim ~/bash_profile
export a=1
(3)系统级
vim /etc/profile
export a=1

linux中export命令用于设置或显示环境变量
在shell中执行程序时,shell会提供一组环境变量,export可新增,修改或删除环境变量,供后续执行的程序使用。

${}声明变量
$()不通用

实验一:变量的输出

<1>在当前环境中写入变量a=1,echo可以输出变量

[root@shell_example mnt]# a=1
[root@shell_example mnt]# echo $a
1
[root@shell_example mnt]# echo ${a}b
1b

在这里插入图片描述
<2>在脚本中写入变量时,必须使用export命令设置环境变量,否则不会生效

[root@shell_example mnt]# vim test.sh
[root@shell_example mnt]# chmod +x test.sh 
[root@shell_example mnt]# ll test.sh 
-rwxr-xr-x. 1 root root 20 Apr  4 12:30 test.sh
[root@shell_example mnt]# cat test.sh 
#!/bin/bash
echo $a
[root@shell_example mnt]# ./test.sh 

[root@shell_example mnt]# export a=1
[root@shell_example mnt]# ./test.sh 
1

在这里插入图片描述
在这里插入图片描述
实验二:变量的种类及定义方法

1)环境级的变量:当前环境关闭后会失效

[root@shell_example mnt]# export a=1
[root@shell_example mnt]# echo $a
1
[root@shell_example mnt]# logout
Connection to 172.25.254.227 closed.
[kiosk@foundation68 ~]$ ssh root@172.25.254.227
root@172.25.254.227's password: 
Last login: Thu Apr  4 12:29:51 2019 from 172.25.254.68
[root@shell_example ~]# echo $a

在这里插入图片描述
在当前环境中写入的变量退出登陆(当前环境关闭后)会失效,所以在当前环境中设置的变量只是环境级的变量

2)用户级的变量:切换到别的用户不会生效,只在当前用户生效

[root@shell_example ~]# vim ~/.bash_profile
[root@shell_example ~]# source ~/.bash_profile 
[root@shell_example ~]# echo $a
1
[root@shell_example ~]# logout
Connection to 172.25.254.227 closed.
[kiosk@foundation68 ~]$ ssh root@172.25.254.227
root@172.25.254.227's password: 
Last login: Thu Apr  4 12:37:19 2019 from 172.25.254.68
[root@shell_example ~]# echo $a
1
[root@shell_example ~]# su - student
[student@shell_example ~]$ echo $a

[student@shell_example ~]$ logout

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
将变量写入~/.bash_profile 文件中当前环境关闭后再次登陆可以使用但是切换用户后不能使用,所以在~/.bash_profile 文件中写入的变量时用户级的变量
注:还原之前的状态,删除在文件中写入的东西

[root@shell_example ~]# vim ~/.bash_profile

在这里插入图片描述
在这里插入图片描述
3)系统级的变量:切换用户和退出当前环境都不会失效

[root@shell_example ~]# vim /etc/profile
[root@shell_example ~]# source /etc/profile
[root@shell_example ~]# echo $a
1
[root@shell_example ~]# su - student
Last login: Thu Apr  4 12:42:35 EDT 2019 on pts/0
[student@shell_example ~]$ echo $a
1
[student@shell_example ~]$ logout
[root@shell_example ~]# logout
Connection to 172.25.254.227 closed.
[kiosk@foundation68 ~]$ ssh root@172.25.254.227
root@172.25.254.227's password: 
Last login: Thu Apr  4 12:48:29 2019 from 172.25.254.68
[root@shell_example ~]# echo $a
1

在这里插入图片描述
在这里插入图片描述
注:将更改的内容还原

[root@shell_example ~]# vim /etc/profile
[root@shell_example ~]# source /etc/profile

在这里插入图片描述在这里插入图片描述

注意:
/etc/profile是系统的配置文件,最好不要更改,要设置系统及的变量还可以在/etc/profile.d/ 目录中新建一个文件写入,两者的效果是一样的

4)方法二

[root@shell_example ~]# cd /etc/profile.d/
[root@shell_example profile.d]# vim yyy.sh
[root@shell_example profile.d]# cat yyy.sh
export a=1
[root@shell_example profile.d]# source /etc/profile.d/yyy.sh 
[root@shell_example profile.d]# echo $a
1
[root@shell_example profile.d]# logout
Connection to 172.25.254.227 closed.
[kiosk@foundation68 ~]$ ssh root@172.25.254.227
root@172.25.254.227's password: 
Last login: Thu Apr  4 16:48:49 2019 from 172.25.254.68
[root@shell_example ~]# echo $a
1
##当前环境关闭后依然有效
[root@shell_example ~]# su - student
Last login: Thu Apr  4 12:51:09 EDT 2019 on pts/0
[student@shell_example ~]$ echo $a
1
[student@shell_example ~]$ logout
##切换用户后也有效

在这里插入图片描述
实验完成后删除文件

   [root@shell_example ~]# cd /etc/profile.d/
   [root@shell_example profile.d]# rm -fr yyy.sh 

在这里插入图片描述

4.变量名称的规范

变量名称中通常包含大小写字字母,数字,下划线(不是必须)
变量名称格式
WESTOS_LINUX
Westos_Linux
westoS_Linux

5.字符的转译及变量的声明

\转译单个字符
“”弱引用,批量转译 “” 中出现的字符,
‘’强引用,批量转译 ‘’ 中出现的字符
‘’与 “” 两者的区别在于, “” 不能转译 “”,"`","!","$"
${}变量声明

${}声明变量
$()不通用

实验:
(1)\

[root@shell_example mnt]# echo #          ##无法输出,因为#是特殊字符

[root@shell_example mnt]# echo #hello     ##无法输出,这里系统会认为#是注释的作用

[root@shell_example mnt]# echo \#hello    ##可以输出,#被\转义
#hello
[root@shell_example mnt]# echo \#hello#    ##可以输出,第二个#和前面的字符串之间没有空格,系统会认为这是同一个字符串
#hello#
[root@shell_example mnt]# echo \#hello #   ##无法输出第二个#,一为\只转义一个字符串,第二个#和前面的字符串之间有空格,这是两个字符串,第二个没有被转义,不会输出
#hello
[root@shell_example mnt]# echo \#hello \#   ##可以输出,第二个#被转义
#hello #

在这里插入图片描述
(2)""弱引用和’'强引用

[root@shell_example mnt]# echo `hostname`    ##``表示执行,成功输出
shell_example.westos.com
[root@shell_example mnt]# echo #`hostname`#  ##没有结果,系统认为`hostname`被注释掉

[root@shell_example mnt]# echo "#`hostname`#" ##输出了""中的内容,只转义了#没有转义``
#shell_example.westos.com#
[root@shell_example mnt]# echo '#`hostname`#' ##''中的``也被转义,命令没有被执行
#`hostname`#

在这里插入图片描述
(3)""不能引用!,$,,`但是可以引用|和?

[root@shell_example mnt]# echo "!"
-bash: !: event not found
[root@shell_example mnt]# echo '!'
!

在这里插入图片描述

[root@shell_example mnt]# echo "$2"

[root@shell_example mnt]# echo '$2'
$2

在这里插入图片描述

[root@shell_example mnt]# echo "\"
> ^C
[root@shell_example mnt]# echo '\'
\

在这里插入图片描述

[root@shell_example mnt]# echo "`"
> ^C
[root@shell_example mnt]# echo '`'
`

在这里插入图片描述

[root@shell_example mnt]# echo "|"
|
[root@shell_example mnt]# echo "?"
?

在这里插入图片描述
在这里插入图片描述
(4)变量的声明
``行和$()的区别
<1> ``可以执行

[root@shell_example mnt]# yum install perl

[root@shell_example mnt]# which perl
/usr/bin/perl
[root@shell_example mnt]# vim test1
[root@shell_example mnt]# cat test1
#!/usr/bin/perl
print `date`
[root@shell_example mnt]# chmod +x test1
[root@shell_example mnt]# /mnt/test1
Thu Apr  4 18:40:22 EDT 2019

在这里插入图片描述
在这里插入图片描述
<2>$()出现报错

[root@shell_example mnt]# vim test1
[root@shell_example mnt]# cat test1
#!/usr/bin/perl
print $(date)
[root@shell_example mnt]# /mnt/test1
Bareword found where operator expected at /mnt/test1 line 2, near "$(date"
	(Missing operator before date?)
syntax error at /mnt/test1 line 2, near "date)
"
Execution of /mnt/test1 aborted due to compilation errors.

在这里插入图片描述
<3>${}没有报错,但是没有结果

[root@shell_example mnt]# vim test1
[root@shell_example mnt]# cat test1
#!/usr/bin/perl
print ${date}
[root@shell_example mnt]# /mnt/test1

在这里插入图片描述
6.变量值传递

$1脚本后的第一串字符串
$2脚本后的第二串字符串
$3脚本后的第三串字符串
$#脚本后所跟字符串的哥数
$*脚本后跟的所有字符串,模式为“ 1 2 3 ”
&@脚本后跟的所有字符串,模式为“ 1”“2”“3”

实验:

1 编辑脚本

[root@shell_example mnt]# vim test.sh

在这里插入图片描述
在这里插入图片描述
2 不加字符串执行

[root@shell_example mnt]# sh test.sh 
$0 is test.sh
$1 is
$2 is
$3 is
$# is 0
$* is
$@ is

在这里插入图片描述
3 加一个字符串执行

[root@shell_example mnt]# sh test.sh westos 
$0 is test.sh
$1 is westos
$2 is
$3 is
$# is 1
$* is westos
$@ is westos

在这里插入图片描述
4 加两个字符串执行

[root@shell_example mnt]# sh test.sh westos redhat
$0 is test.sh
$1 is westos
$2 is redhat
$3 is
$# is 2
$* is westos redhat
$@ is westos redhat

在这里插入图片描述
5 加三个字符串执行

[root@shell_example mnt]# sh test.sh westos redhat linux
$0 is test.sh
$1 is westos
$2 is redhat
$3 is linux
$# is 3
$* is westos redhat linux
$@ is westos redhat linux

在这里插入图片描述
(2)对比$*和$@
<1>$*

[root@shell_example mnt]# ls
test.sh
[root@shell_example mnt]# vim test1.sh
[root@shell_example mnt]# sh test1.sh westos redhat linux
westos redhat linux
[root@shell_example mnt]# sh -x test1.sh westos redhat linux
+ for i in '"$*"'
+ echo westos redhat linux
westos redhat linux

在这里插入图片描述
文件内容如下:
在这里插入图片描述
注:这里"$*" = “westos redhat linux”

<2>$@

[root@shell_example mnt]# vim test1.sh
[root@shell_example mnt]# sh -x test1.sh westos redhat linux
+ for i in '"$@"'
+ echo westos
westos
+ for i in '"$@"'
+ echo redhat
redhat
+ for i in '"$@"'
+ echo linux
linux
[root@shell_example mnt]# sh test1.sh westos redhat linux
westos
redhat
linux

在这里插入图片描述
在这里插入图片描述
注:这里"$@" = “westos” “redhat” “linux”
7.用 read 实现变量传递

read WESTOS
read -s WESTOS
read -p "input: " WESTOS

8.linux 系统中命令别名的设定

命令别名的设定和变量的设定一样都有三个级别:环境级,用户级和系统级

(1)环境级
alias xie='vim’
(2)用户级
vim ~/.bashrc
alias xie='vim’
(3)系统级
vim /etc/bashrc
alias xie=‘vim’
unalias xie

实验:
1)环境级命令别名的设定,当前环境关闭后失效

[root@shell_example mnt]# ls
test1.sh  test2.sh  test.sh
[root@shell_example mnt]# alias yyy='ls'
[root@shell_example mnt]# ls
test1.sh  test2.sh  test.sh
[root@shell_example mnt]# yyy
test1.sh  test2.sh  test.sh
[root@shell_example mnt]# logout
Connection to 172.25.254.227 closed.
[kiosk@foundation68 ~]$ ssh root@172.25.254.227
root@172.25.254.227's password: 
Last login: Thu Apr  4 16:53:40 2019 from 172.25.254.68
[root@shell_example ~]# cd /mnt
[root@shell_example mnt]# yyy
bash: yyy: command not found...
[root@shell_example mnt]# ls
test1.sh  test2.sh  test.sh

在这里插入图片描述
2)用户级命令别名的设定,切换用户后失效

[root@shell_example mnt]# vim ~/.bashrc
[root@shell_example mnt]# source ~/.bashrc 
[root@shell_example mnt]# yyy
test1.sh  test2.sh  test.sh
[root@shell_example mnt]# logout
Connection to 172.25.254.227 closed.
[kiosk@foundation68 ~]$ ssh root@172.25.254.227
root@172.25.254.227's password: 
Last login: Thu Apr  4 17:53:02 2019 from 172.25.254.68
[root@shell_example ~]# cd /mnt
[root@shell_example mnt]# yyy
test1.sh  test2.sh  test.sh

在这里插入图片描述
文件中写入的内容如下:
在这里插入图片描述

[root@shell_example mnt]# su - student
Last login: Thu Apr  4 16:50:01 EDT 2019 on pts/0
[student@shell_example ~]$ cd /mnt
[student@shell_example mnt]$ yyy
bash: yyy: command not found...
[student@shell_example mnt]$ ls
test1.sh  test2.sh  test.sh
[student@shell_example mnt]$ logout

在这里插入图片描述
将文件中的内容注释掉
在这里插入图片描述
3)系统级命令别名的设定,关闭当前环境和切换用户后都不会失效

[root@shell_example mnt]# vim ~/.bashrc
[root@shell_example mnt]# vim /etc/bashrc
[root@shell_example mnt]# source /etc/bashrc 
[root@shell_example mnt]# yyy
test1.sh  test2.sh  test.sh
[root@shell_example mnt]# su - student
Last login: Thu Apr  4 17:58:00 EDT 2019 on pts/0
[student@shell_example ~]$ cd /mnt
[student@shell_example mnt]$ yyy
test1.sh  test2.sh  test.sh
[student@shell_example mnt]$ logout
[root@shell_example mnt]# logout
Connection to 172.25.254.227 closed.
[kiosk@foundation68 ~]$ ssh root@172.25.254.227
root@172.25.254.227's password: 
Last login: Thu Apr  4 17:56:40 2019 from 172.25.254.68
[root@shell_example ~]# cd /mnt
[root@shell_example mnt]# yyy
test1.sh  test2.sh  test.sh

文件中的内容如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
注:将/etc/bashrc中修改的内容注释掉,命令别名的设定和变量的设定原理相同,所以系统级的命令别名也可以在/etc/profile.d/目录下写一个脚本将命令写进去,这里就不再演示了

[root@shell_example mnt]# vim /etc/bashrc 

在这里插入图片描述
在这里插入图片描述
9.利用命令执行结果设定变量

Hostname=$(hostname)
Hostname=hostname
echo $?

$? 是命令在执行完成之后产生的退出值
范围是 [0-255]
当 $0=0 时标示命令执行没有错误输出
这个值可以用 exit 命令执行

例如 exit 66

例:
(1)执行结果没有错误时退出值为0

[root@shell_example mnt]# Hostname=$(hostname)
[root@shell_example mnt]# Hostname=`hostname`
[root@shell_example mnt]# echo $?
0

在这里插入图片描述
(2)默认输出错误时退出值为127,exit的值可以自己设定

[root@shell_example mnt]# haha
bash: haha: command not found...
[root@shell_example mnt]# echo $?
127

在这里插入图片描述
(3)自定义exit的退出值,设置的是多少就返回多少

[root@shell_example mnt]# vim test2.sh
[root@shell_example mnt]# sh test2.sh
test1.sh  test2.sh  test.sh
[root@shell_example mnt]# echo $?
66
[root@shell_example mnt]# vim test2.sh
[root@shell_example mnt]# cat test2.sh 
#!/bin/bash
ls /mnt
exit 0
[root@shell_example mnt]# sh test2.sh
test1.sh  test2.sh  test.sh
[root@shell_example mnt]# echo $?
0

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(4)利用命令执行结果设定变量

方法一:

[root@shell_example mnt]# export a=`hostname`
[root@shell_example mnt]# vim test.sh 
[root@shell_example mnt]# cat test.sh
#!/bin/bash
echo $a
[root@shell_example mnt]# ls -l
total 4
-rwxr-xr-x. 1 root root 20 Apr  4 18:31 test.sh
[root@shell_example mnt]# ./test.sh 
shell_example.westos.com

在这里插入图片描述
方法二:

[root@shell_example mnt]# export a=$(hostname)
[root@shell_example mnt]# ./test.sh 
shell_example.westos.com

在这里插入图片描述
10.脚本中的函数

定义函数:
将要执行的内容写陈一个语句块直接调用

脚本中的函数是把一个复杂的语句块定义成一个字符串的方法

Host_Message()
{
read -p "Please input you action: " Action
[ "Action" == "exit" ] && exit 0
[ "Action" == "user"]&&echo You are $USER
[ "Action" == "hostname" ]&& echo $HOST
Host_Message
}
Host_Message
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值