Linux:shell变量

shell变量的定义

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

一·在shell脚本中变量的分类

1、环境级变量

[root@desktop mnt]# vim file.sh
[root@desktop mnt]# chmod +x file.sh
[root@desktop mnt]# /mnt/file.sh &   ##打入后台
[root@desktop mnt]# ps f
[root@desktop mnt]# vim file.sh 
删除sleep 1000
[root@desktop mnt]# fg     ##结束进程
[root@desktop mnt]# export a=1    ##声明
[root@desktop mnt]# /mnt/file.sh 
退出后在此登陆
[root@desktop ~]# echo $a    ##为空

这里写图片描述这里写图片描述

2、用户级变量

[root@localhost ~]# vim .bash_profile     ##在最后一行添加exprot a=1
[root@localhost ~]# source .bash_profile    ##刷新
[root@localhost ~]# echo $a   ##查看
[root@localhost ~]# exit   ##退出登陆
[root@localhost ~]# echo $a     ##登陆后再次查看a=1依旧生效

添加脚本内容
这里写图片描述
这里写图片描述

3、系统级环境

[root@localhost ~]# vim /etc/profile    ##在最后一行添加export a=2
[root@localhost ~]# source /etc/profile   ##刷新
[root@localhost ~]# echo $a   ##查看
2
[root@localhost ~]# exit  ##退出登陆
[root@localhost ~]# echo $a   ##登陆后再次查看,发现发生改变
1
系统先加载系统级变量,进入系统生效的是用户级变量

添加内容:这里写图片描述
这里写图片描述

二·字符的转译变量的声明

""是批量的,\是单个的转义。
单引号等于\的批量。
除了!\`四个符号双引号不可以引用,其他的均可以。
单引号所有均可以引用。
一般情况下,$var${var}是没有区别的,但是用${ }会比较精确的界定变量名称的范围,${}作变量声明。
$()等同于`,$()标准的扩展接口,`是扩展性接口,在bash中,$( )与` `(反引号)都是用来作命令替换的。
命令替换与变量替换差不多,都是用来重组命令行的,先完成引号里的命令行,
然后将其结果替换出来,再重组成新的命令行。最后,$( )的弊端是,
并不是所有的类unix系统都支持这种方式,但反引号是肯定支持的。
$[]等同与(()),将shell中的可变长字符转换成int整形,可变长字符节省空间。
$(( ))可以将其他进制转成十进制数显示出来。
""弱引用,批量转译""中出现的字符。
''弱引用,批量转译''中出现的字符。
``:命令替换,在输出一句话的时候,如果想中间加入命令输出结果,
在反引号里面输入命令就可以做到,和$COMMAND是一样的。
[root@localhost ~]# a=hello world   有空格无法表示需要转译
[root@localhost ~]# a=hello\ world  \转译一个空格
[root@localhost ~]# echo $a
[root@localhost ~]# a="hello   world"  用引号批量转译
[root@localhost ~]# echo $a
[root@localhost ~]# a=1
[root@localhost ~]# echo $ab  无法区分得有空格或者变量声明
[root@localhost ~]# echo $a  b  用空格区分
[root@localhost ~]# echo ${a}b  ${}变量声明
[root@localhost ~]# echo 1+1
[root@localhost ~]# echo $[1+1]  用$[]将1+1转换成整形2
[root@localhost ~]# a=(1 2 3)  数组
[root@localhost ~]# echo $a
[root@localhost ~]# echo ${a[0]} 调用下标显示数组第零个数,从零开始索引
[root@localhost ~]# echo ${a[1]} 调用下标显示数组第一个数

这里写图片描述

[root@localhost ~]# cd
[root@localhost ~]# name=(`ls`)    
[root@localhost ~]# echo $name  
[root@localhost ~]# echo ${name[1]}  依次调用name下标显示文件

这里写图片描述

三·变量值传递

$1 脚本后的第一串字符串
$2 脚本后的第二串字符串
$3 脚本后的第三串字符串
$# 脚本后所跟字符串的个数
$* 脚本后根的所有字符串,模式为“1 2 3$@ 脚本后根的所有字符串,模式为"1" "2" "3"
[root@localhost mnt]# echo $$
2648
[root@localhost mnt]# vim test.sh   ##编辑脚本
[root@localhost mnt]# cat test.sh 
[root@localhost mnt]# sh test.sh   ##调用
[root@localhost mnt]# sh test.sh westos
[root@localhost mnt]# sh test.sh westos  linux
[root@localhost mnt]# sh test.sh westos  linux  redhat

这里写图片描述这里写图片描述这里写图片描述这里写图片描述

@区别: @后面是三串字符

[root@localhost mnt]# vim for.sh 
[root@localhost mnt]# sh -x for.sh westos linux redhat    ##做了一次循环,为一个字符
[root@localhost mnt]# vim for.sh
[root@localhost mnt]# sh -x for.sh westos linux redhat   ##做了三次循环,为三个字符

是”$*”时:这里写图片描述
是”$@”时这里写图片描述
这里写图片描述

实验一·当输入的文件少于两个时提示错误,用脚本建立文件中用户及密码

[root@localhost mnt]# vim userfile   ##编辑用户
[root@localhost mnt]# vim passfile   ##编辑密码
[root@localhost mnt]# vim user_create.sh    ##编辑脚本
[root@localhost mnt]# sh user_create.sh    ##直接调用会提示
ERROR:please input userfilr and passwordfile
[root@localhost mnt]# sh user_create.sh userfile    ##输入一个文件名会提示
ERROR:please input userfilr and passwordfile
[root@localhost mnt]# sh user_create.sh userfile passfile   ##输入两个则为调用成功

添加用户以及密码
这里写图片描述这里写图片描述
脚本内容
这里写图片描述
这里写图片描述

四·用read实现变量传递

实验二·在登录用户和密码时使用交互式传参

[root@localhost mnt]# vim user_create.sh     ##编辑脚本
[root@localhost mnt]# sh user_create.sh    ##调用
please input user file: userfile   ##文件名需要匹配
please input password file: passfile  ##文件名需要匹配
(如果执行卡住,将useradd改为userdel -r 后再执行,执行成功后继续改回useradd)
[root@localhost mnt]# su - user1   ##检测是否建立成功
[user1@localhost ~]$ su - user2
Password: 
[user2@localhost ~]$ 

脚本内容
这里写图片描述
这里写图片描述
检测:这里写图片描述

五·linux系统中命令别名的设定

[root@desktop mnt]# alias xie='vim'    ##设定"xie"的别名为"vim"(临时修改)
[root@desktop mnt]# alias   ##查看
alias xie='vim'
[root@desktop mnt]# cd
[root@desktop ~]# vim .bashrc     ##永久修改
alias xie='vim'   ##添加内容
[root@desktop ~]# xie
[root@desktop ~]# su - student
Last login: Thu May 11 20:23:54 EDT 2017 on pts/0
[student@desktop ~]$ xie     ##普通用户执行不了
bash: xie: command not found...
[student@desktop ~]$ logout
[root@desktop ~]# vim /etc/bashrc    ##永久修改
alias xie='vim'    ##添加内容
[root@desktop ~]# source /etc/bashrc    ##刷新
[root@desktop ~]# su - student
Last login: Sun Jun 17 22:22:41 EDT 2018 on pts/0
[student@desktop ~]$ xie    ##普通用户可以执行
[student@desktop ~]$ logout
[root@desktop ~]# vim /etc/bashrc 
删除添加内容
[root@desktop ~]# source /etc/bashrc   ##刷新
[root@desktop ~]# vim .bashrc
删除添加内容
[root@desktop ~]# source .bashrc   ##刷新
[root@desktop ~]# xie
[root@desktop ~]# unalias xie    ##撤销命令别名
[root@desktop ~]# xie
bash: xie: command not found...

这里写图片描述
这里写图片描述

六·查询 exit退出值

exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行。
退出码(exit status,或exit code)的约定:
0表示成功(Zero - Success)
非0表示失败(Non-Zero - Failure)
2表示用法不当(Incorrect Usage)
127表示命令没有找到(Command Not Found)
126表示不是可执行的(Not an executable)
=128 信号产生

[root@localhost mnt]# echo $?     ##查看退出值
[root@localhost mnt]# vim file.sh   ##编辑文件
[root@localhost mnt]# sh file.sh   ##调用
[root@localhost mnt]# echo $?   ##查看退出值后,会发现和第一次的退出值不同
[root@localhost mnt]# vim file.sh   ##再次修改退出值
[root@localhost mnt]# sh file.sh   ##调用
[root@localhost mnt]# echo $?   ##再次查看是,发现和第二次也不同

第一次编辑脚本内容:这里写图片描述
第二次修改脚本内容:这里写图片描述
这里写图片描述

实验三:用退出值方式判断ip

[root@desktop mnt]# vim ip_cheak.sh
[root@desktop mnt]# sh ip_cheak.sh
please input a number:172.25.254.119
172.25.254.119 is up
[root@desktop mnt]# sh ip_cheak.sh
please input a number:172.25.254.219
172.25.254.219 is down

编辑脚本内容
这里写图片描述
这里写图片描述

七·脚本中的函数

函数的作用:函数可以确保命令,循环执行,简化脚本内容,使内容可读

1、利用函数循环检测IP是否运行并在输入exit退出

[root@localhost ~]# vim file.sh    ##编辑脚本
You have new mail in /var/spool/mail/root
[root@localhost ~]# sh file.sh
please input a ipaddress: 172.25.254.119
172.25.254.119 is up
please input a ipaddress: 172.25.254.250
172.25.254.250 is down
please input a ipaddress: exit
bye

编辑脚本内容
这里写图片描述
这里写图片描述

2、利用函数循环检测文件类型

[root@localhost ~]# vim westos.sh    ##编辑文件
[root@localhost ~]# sh westos.sh /etc/passwd    ##检测文件类型
/etc/passwd is file

编辑脚本内容
这里写图片描述
这里写图片描述

实验四·根据函数建立用户删除用户,并设置密码

[root@localhost mnt]# vim westos.sh
[root@localhost mnt]# sh westos.sh 
please input action: add    ##建立用户
please input a username: linux
please input a password:  
Changing password for user linux.
passwd: all authentication tokens updated successfully.
please input action: del     ##删除用户
please input a username: linux
please input action: exit
bye

编辑脚本内容
这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/le_anny/article/details/80763736