Shell中的变量

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 10:34   136   0

一.变量的定义及种类
1.定义
变量即在程序运行过程中他的值是允许被改变的量
变量是用一串固定的字符来标示不固定值的一种方法
变量是一种使用方便的占位符,用于引用计算机的内存地址,该地址可以存储script运行时可更改的程序信息
在shell中变量是不能永久保存在系统中的,必须在文件中声明
2.种类
在shell中变量分为环境级变量,用户级变量,系统级变量。
环境级变量只在当前shell中生效,shell关闭则变量丢失
用户级变量写在用户的骨文件中,只针对当前用户生效
系统级变量被写在系统的配置文件/etc/profile或者/etc/profile.d中,对于所有的用户都生效
(1)环境级变量

[root@server mnt]# vim test.sh
[root@server mnt]# cat test.sh  ##编写脚本输出变量的值
#!/bin/bash
echo $a  
[root@server mnt]# a=1  ##给变量进行赋值
[root@server mnt]# echo $a  ##输出变量值
1
[root@server mnt]# sh test.sh  ##以此方式调用脚本,会在当前shell中开启一个子shell来运行所以不会显示输出结果

[root@server mnt]# export a=1  ##定义变量,可以在当前shell及子shell中使用改变变量
[root@server mnt]# source test.sh
1
[root@server mnt]# sh test.sh  ##运行脚本在子shell中调用变量
1
[root@server mnt]# echo $a  ##当前shell中调用shell的值
1

(2)用户级变量

[root@server mnt]# vim /root/.bash_profile

在这里插入图片描述

[root@server mnt]# source /root/.bash_profile  ##重新加载骨配置文件
[root@server mnt]# sh test.sh
1
[root@server mnt]# su - student  ##切换到其他用户
Last login: Thu May 11 20:23:54 EDT 2017 on pts/0
[student@server ~]$ sh /mnt/test.sh  ##无法调用脚本

(3)系统级变量

[root@server mnt]# vim /etc/profile

在这里插入图片描述

[root@server mnt]# source /etc/profile
[root@server mnt]# sh /mnt/test.sh
1
[root@server mnt]# su - student
Last login: Sat May 18 12:53:51 EDT 2019 on pts/0
[student@server ~]$ sh /mnt/test.sh  ##普通用户可以使用此脚本
1

(4)PATH环境变量
PATH 环境变量:用来指定系统查找存放命令的目录。
例如存在一个脚本在/mnt中,无法在别的目录中使用脚本名称直接运行该脚本,将/mnt 添加到PATH 环境变量 中去之后,就可以使用该脚本名称直接运行该脚本。
相当于告诉系统可以在/mnt寻找该脚本

[root@server student]# vim /etc/profile

在这里插入图片描述

[root@server student]# source /etc/profile
[root@server student]# test.sh
--bash: /mnt/test.sh: Permission denied
[root@server student]# chmod 755 /mnt/test.sh
[root@server student]# test.sh
1

二.字符的转译及变量的声明
\ ##转译单个字符
“” ##弱引用,批量转译"“中出现的字符
‘’ ##强引用,批量转译’'中出现的字符
''与”“两者的区别是”“不能转译”" ," ’ “,”!","$"

[root@server student]# echo #westos

[root@server student]# echo \#westos
#westos
[root@server student]# echo \##westos
##westos
[root@server student]# echo \# #westos
#
[root@server student]# echo \# \#westos  \  ##转译单个字符
# #westos
[root@server student]# echo "# #westos"
# #westos
[root@server student]# echo "$$westos"  ##不可以转译$
2671westos
[root@server student]# ps
  PID TTY          TIME CMD
 2170 pts/0    00:00:00 bash
 2405 pts/0    00:00:00 su
 2458 pts/0    00:00:00 su
 2467 pts/0    00:00:00 bash
 2634 pts/0    00:00:00 su
 2670 pts/0    00:00:00 su
 2671 pts/0    00:00:00 bash   ##2671具有特殊含义
 2930 pts/0    00:00:00 ps
[root@server student]# echo '$$westos'
$$westos

三.变量值的传递
传递方式:变量值传递和read方式
1.变量值传递
$1 ##脚本后跟的第一串字符
$2 ##脚本后跟的第二个字符
$3 ##脚本后跟的第三个字符
$# ##脚本后所跟字符串的个数
$* ##脚本后跟的所有字符串,模式为“1 2 3”
&@ ##脚本后跟的所有字符串,模式为“1”“2”“3”
$$ ##当前进程的pid
$0 ##当前脚本文件的名称

[root@server mnt]# vim file.sh
[root@server mnt]# cat file.sh
#!/bin/bash
echo '$$' is $$
echo '$0' is $0
echo '$1' is $1
echo '$2' is $2
echo '$3' is $3
echo '$*' is $*
echo '$@' is $@
echo '$#' is $#
[root@server mnt]# chmod +x file.sh
[root@server mnt]# sh file.sh 1 2 3 4
$$ is 3326
$0 is file.sh
$1 is 1
$2 is 2
$3 is 3
$* is 1 2 3 4
$@ is 1 2 3 4
$# is 4
[root@server mnt]# vim luck.sh
[root@server mnt]# cat luck.sh
#!/bin/bash
for i in "$*"
do
     echo $i
done
[root@server mnt]# sh -x luck.sh 1 2 3 4
+ for i in '"$*"'
+ echo 1 2 3 4
1 2 3 4
[root@server mnt]# vim luck.sh
[root@server mnt]# cat luck.sh
#!/bin/bash
for i in "$@"
do
     echo $@
done
[root@server mnt]# sh -x luck.sh 1 2 3 4
+ for i in '"$@"'
+ echo 1 2 3 4
1 2 3 4
+ for i in '"$@"'
+ echo 1 2 3 4
1 2 3 4
+ for i in '"$@"'
+ echo 1 2 3 4
1 2 3 4
+ for i in '"$@"'
+ echo 1 2 3 4
1 2 3 4

2.read实现变量传递–交互式输入变量的值
read WESTOS ##定义变量WESTOS接下来需要输入的值
read -s WESTOS ##定义变量WESTOS接下来需要输入的值 -s 加密不显示
read -p “input :” WESTOS ##-p先输出“”中的值在输入WESTOS变量的值

[root@server mnt]# read -p "Plese input a word:" WORD
Plese input a word:haha
[root@server mnt]# echo $WORD
haha
[root@server mnt]# read -p "Plese input a word:" -s WORD
Plese input a word:
[root@server mnt]# echo $WORD
westos

四.Linux中命令别名的设定
1.仅在当前环境中使用,在其他环境中不可以使用

[root@server mnt]# shijian
bash: shijian: command not found...
[root@server mnt]# alias shijian='date'
[root@server mnt]# shijian
Sat May 18 14:14:32 EDT 2019

2.某一用户可以永久使用别名

[root@server mnt]# vim /root/.bashrc

在这里插入图片描述

[root@server mnt]# source /root/.bashrc
[root@server mnt]# shijian  ##当前用户可用
Sat May 18 14:17:28 EDT 2019
[root@server mnt]# su - student
Last login: Sat May 18 14:10:33 EDT 2019 on pts/0
[student@server ~]$ shijian  ##其他用户不可用
bash: shijian: command not found...

3.设定系统中的用户均可以使用此别名

[root@server student]# vim /etc/bashrc

在这里插入图片描述

[root@server student]# source /etc/bashrc
[root@server student]# shijian
Sat May 18 14:23:22 EDT 2019
[root@server student]# su - student
Last login: Sat May 18 14:21:49 EDT 2019 on pts/0
[student@server ~]$ shijian
Sat May 18 14:23:33 EDT 2019

4.取消系统中别名的设定

[root@server student]# vim /etc/bashrc  ##删除系统中的别名设定
[root@server student]# vim /root/.bashrc  ##删除用户的别名设定
[root@server student]# unalias shijian  ##删除当前环境的别名设定
[root@server student]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

五.利用命令执行结果设定的变量
1.命令结果赋值给变量的方式有两种

[root@server student]# HOSTNAME=$(hostname)  ##仅使用于shell
[root@server student]# echo $HOSTNAME
server.luck.com
[root@server student]# HOSTNAME=`hostname`  ##通用
[root@server student]# echo $HOSTNAME
server.luck.com

KaTeX parse error: Expected 'EOF', got '#' at position 4: ? ##命令在执行完成之后产生的退出…?=0时标示命令执行时没有错误的输出,这个值可以用exit命令执行保存

[root@server student]# date
Sat May 18 14:42:26 EDT 2019
[root@server student]# echo $?  ##无错误输出
0
[root@server mnt]# vim luck.sh
[root@server mnt]# cat luck.sh
#!/bin/bash
date
[root@server mnt]# sh luck.sh
Sat May 18 14:43:59 EDT 2019
[root@server mnt]# echo $?
0
[root@server mnt]# vim luck.sh
[root@server mnt]# cat luck.sh
#!/bin/bash
date
exit 66
[root@server mnt]# sh luck.sh
Sat May 18 14:44:41 EDT 2019
[root@server mnt]# echo $?  ##命令执行完成以后产生的退出值
66

六.脚本中的函数
脚本中的函数是把一个复杂的语句快定义成一个字符串的方法
模板为:
Host_Message()
{
read -p “Please input your action:” ACTION
[ “ACTION” ==“exit” ] && exit 0
[ “ACTION” ==“user” ] &&echo You are $USER
[ “ACTION” == “hostname” ] && echo &HOST
Host_Messages
}
Host_Message
判断一个文件的文本类型

[root@server mnt]# vim check_file.sh

在这里插入图片描述

[root@server mnt]# sh check_file.sh
Please input filename:/etc/passwd
/etc/passwd is a common file
Please input filename:/mnt
/mnt is a directory
Please input filename:exit
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP