变量的定义:

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

在shell脚本中变量的分类:

在shell中变量分为环境级变量,用户级变量,系统级变量,
环境级变量只在当前shell中生效,shell关闭变量丢失,
用户级变量写在用户的骨文件中,只针对当前用户有效,
系统级变量被写在系统的配置文件/etc/profile中
变量即在程序运行时保存在内存中。
硬盘永久,内存临时的。

shell脚本中变量的定义方法:

环境级:
export A=1
用户级:
vim ~/bash_profile
export A=1
系统级:
vim /etc/profile
export A=1

操作步骤:

[root@localhost mnt]# echo $USER
root
[root@localhost mnt]# a=1
[root@localhost mnt]# ps 父级进程
PID TTY TIME CMD
2200 pts/1 00:00:00 bash
4833 pts/1 00:00:00 ps
[root@localhost mnt]# vim file.sh 加入显示a的值并加入睡眠时间

基于linux下的shell变量_bash

[root@localhost mnt]# chmod +x /mnt/file.sh 赋予执行权限
[root@localhost mnt]# /mnt/file.sh & 运行脚本打入后台
[1] 4867
[root@localhost mnt]# ps f 脚本为子进程
PID TTY STAT TIME COMMAND
2200 pts/1 Ss 0:00 -bash
4867 pts/1 S 0:00 \_ -bash
4868 pts/1 S 0:00 | \_ sleep 1000
4869 pts/1 R+ 0:00 \_ ps f
2145 pts/0 Ss+ 0:00 /bin/bash
593 tty1 Ss+ 0:02 /usr/bin/Xorg :0 -background none -verbose -auth
1312 ttyS0 Ss+ 0:00 /sbin/agetty --keep-baud ttyS0 115200 38400 9600
[root@localhost mnt]# fg 调回打入后台进程
/mnt/file.sh 结束进程

基于linux下的shell变量_vim_02

[root@localhost mnt]# vim /mnt/file.sh  编辑脚本只显示a的值

基于linux下的shell变量_linux_03

[root@localhost mnt]# echo $a  父级进程可以显示a的值
1
[root@localhost mnt]# /mnt/file.sh 子进程不可以显示a的值,因为进程号不同

[root@localhost mnt]# export a=1 临时修改环境级变量,父级进程做一个声明,子进程a也有值
[root@localhost mnt]# /mnt/file.sh 可以调用脚本
1
[root@localhost mnt]# ps 查看当前进程
PID TTY TIME CMD
2200 pts/1 00:00:00 bash
4900 pts/1 00:00:00 ps

基于linux下的shell变量_bash_04

[root@localhost mnt]# exit  退出
logout
Connection to 172.25.254.121 closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121 -X
root@172.25.254.121's password:
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
Warning: No xauth data; using fake authentication data for X11 forwarding.
Last login: Sun Jun 17 03:44:50 2018 from 172.25.254.21
/usr/bin/xauth: file /root/.Xauthority does not exist
[root@localhost ~]# /mnt/file.sh 临时修改退出失效运行脚本没有值

基于linux下的shell变量_vim_05

[root@localhost ~]# vim .bash_profile 修改用户环境变量

基于linux下的shell变量_vim_06

[root@localhost ~]# source .bash_profile  刷新
[root@localhost ~]# echo $a a有值
1
[root@localhost ~]# logout再次退出

Connection to 172.25.254.121 closed.
[kiosk@foundation21 Desktop]$
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121 -X
root@172.25.254.121's password:
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
Warning: No xauth data; using fake authentication data for X11 forwarding.
Last login: Sun Jun 17 04:49:40 2018 from 172.25.254.21
[root@localhost ~]# echo $a 进入a的值依旧存在
1
[root@localhost ~]# su - student
Last login: Thu May 11 20:23:54 EDT 2017 on pts/0
[student@localhost ~]$ echo $a 进去学生用户没有因为加载的时候已经将原来的文件覆盖,所以root用户设置的不会生效
[student@localhost ~]$ exit 再次退出
logout
[root@localhost ~]# echo $a root用户生效
1
[root@localhost ~]# bash 重新打开一个shell
[root@localhost ~]# echo $a 依旧生效
1
[root@localhost ~]# exit
exit

基于linux下的shell变量_bash_07

[root@localhost ~]# vim /etc/profile  编辑系统变量文件

基于linux下的shell变量_linux_08

[root@localhost ~]# cat /etc/profile | tail -n 1  将系统变量文件更改为a=2
export a=2
[root@localhost ~]# source /etc/profile 刷新
[root@localhost ~]# echo $a a的值已经改变但是是由于刷新临时的
2
[root@localhost ~]# exit 退出
logout
Connection to 172.25.254.121 closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121 -X
root@172.25.254.121's password:
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
Warning: No xauth data; using fake authentication data for X11 forwarding.
Last login: Sun Jun 17 04:52:14 2018 from 172.25.254.21
[root@localhost ~]# echo $a 系统开启先加载系统进程之后加载用户进程,默认读用户进程,所以依旧是a=1
1

基于linux下的shell变量_linux_09

[root@localhost ~]# echo $PS1  shell的变量
[\u@\h \W]\$
[root@localhost ~]# PS1='westos>' 更改shell变量
westos>date
Sun Jun 17 05:11:52 EDT 2018
westos>PS1='westos\u>'
westosroot>PS1='westos \u>'
westos root>PS1='westos \W>'
westos ~>PS1='westos \W>'
westos ~>PS1='westos \w>' w为绝对路径
westos ~>cd /mnt/
westos /mnt>cd /etc/sysconfig/
westos /etc/sysconfig>PS1='westos \W>'
westos sysconfig>cd /etc/sysconfig/ W为相对路径
westos sysconfig>PS1='westos @\W>'
westos @sysconfig>exit
logout
Connection to 172.25.254.121 closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121 -X
root@172.25.254.121's password:
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
Warning: No xauth data; using fake authentication data for X11 forwarding.
Last login: Sun Jun 17 04:55:02 2018 from 172.25.254.21

基于linux下的shell变量_vim_10

[root@localhost ~]# cd /mnt/ 
[root@localhost mnt]# ls
file.sh passwd showline.sh showusernumber.sh
[root@localhost mnt]# showline.sh 直接用相对路径无法调用
bash: showline.sh: command not found...
[root@localhost mnt]# echo $PATH 查看环境变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost mnt]# chmod +X /mnt/* 给/mnt/下所有文件赋予执行权限
[root@localhost mnt]# chmod +x /mnt/*
[root@localhost mnt]# ll 查看权限
total 16
-rwxr-xr-x 1 root root 271 Jun 17 04:49 file.sh
-rwxr-xr-x 1 root root 422 Jun 17 03:54 passwd
-rwxr-xr-x 1 root root 313 Jun 17 04:12 showline.sh
-rwxr-xr-x 1 root root 335 Jun 17 03:38 showusernumber.sh
[root@localhost mnt]# PATH=$PATH:/mnt 添加/mnt/为环境变量
[root@localhost mnt]# showline.sh 直接用相对路径可以调用
10

基于linux下的shell变量_vim_11

[root@localhost mnt]# echo $PATH 查看环境变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/mnt
[root@localhost mnt]# exit
logout
Connection to 172.25.254.121 closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121 -X
root@172.25.254.121's password:
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
Warning: No xauth data; using fake authentication data for X11 forwarding.
Last login: Sun Jun 17 05:20:38 2018 from 172.25.254.21
[root@localhost ~]# showline.sh 退出失效
bash: showline.sh: command not found...
[root@localhost ~]# vim /etc/profile 写入系统变量文件

基于linux下的shell变量_bash_12

[root@localhost ~]# source /etc/profile  刷新
[root@localhost ~]# echo $PATH 查看环境变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/mnt
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# showline.sh 可以使用相对路径调用
10

基于linux下的shell变量_linux_13

变量名称的规范:

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

shell中特殊符号讲解:

""是批量的,\是单个的转义。
单引号等于\的批量。
除了!\`四个符号双引号不可以引用,其他的均可以。
单引号所有均可以引用。

一般情况下,$var与${var}是没有区别的,但是用${ }会比较精确的界定变量名称的范围,${}作变量声明。
$()等同于`,$()标准的扩展接口,`是扩展性接口,在bash中,$()与` `(反引号)都是用来作命令替换的。

$[]等同与(()),将shell中的可变长字符转换成int整形,可变长字符节省空间。
$(( ))可以将其他进制转成十进制数显示出来。

举例演示:

[root@localhost ~]# a=hello world   有空格无法表示需要转译
bash: world: command not found...
[root@localhost ~]# a=hello\ world \转译一个空格
[root@localhost ~]# echo $a
hello world
[root@localhost ~]# a=hello\ world \只可以转译一个空格
bash: world: command not found...
[root@localhost ~]# a="hello world" 用引号批量转译
[root@localhost ~]# echo $a
hello world
[root@localhost ~]# a=1
[root@localhost ~]# echo $ab 无法区分得有空格或者变量声明

[root@localhost ~]# echo $a b 用空格区分
1 b
[root@localhost ~]# echo ${a}b ${}变量声明
1b
[root@localhost ~]# echo 1+1
1+1
[root@localhost ~]# echo $[1+1] 用$[]将1+1转换成整形2
2

基于linux下的shell变量_linux_14

[root@localhost ~]# a=(1 2 3)  数组
[root@localhost ~]# echo $a
1
[root@localhost ~]# echo ${a[0]} 调用下标显示数组第零个数,从零开始索引
1
[root@localhost ~]# echo ${a[1]} 调用下标显示数组第二个数,都是从零开始索引
2
[root@localhost ~]# echo ${a[2]} 调用下标显示数组第三个数
3
[root@localhost ~]# ls
anaconda-ks.cfg Documents Music Public Videos
Desktop Downloads Pictures Templates
[root@localhost ~]# name=(`ls`)
[root@localhost ~]# echo $name
anaconda-ks.cfg
[root@localhost ~]# echo ${name[1]} 依次调用name下标显示文件,默认按字母排序
Desktop
[root@localhost ~]# echo ${name[2]}
Documents

基于linux下的shell变量_linux_15

变量值传递:

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

操作演示:

[root@localhost mnt]# ps
PID TTY TIME CMD
2558 pts/1 00:00:00 bash
2604 pts/1 00:00:00 ps
[root@localhost mnt]# echo $$ 查看当前PID
2558
[root@localhost mnt]# vim & 占用后台
[1] 2605
[root@localhost mnt]# echo $$ 查看当前PID
2558

[1]+ Stopped vim

基于linux下的shell变量_linux_16

[root@localhost mnt]# vim test.sh  编辑脚本

基于linux下的shell变量_bash_17

[root@localhost mnt]# sh test.sh 依次测试,$0后面接脚本名字
$0 is test.sh
$1 is
$2 is
$3 is
$# is 0
$* is
$@ is
[root@localhost mnt]# sh test.sh westos $1后面所接第一串字符
$0 is test.sh
$1 is westos
$2 is
$3 is
$# is 1
$* is westos
$@ is westos
[root@localhost mnt]# sh test.sh westos linux $2后面接第二串字符
$0 is test.sh
$1 is westos
$2 is linux
$3 is
$# is 2
$* is westos linux 依次显示1,2,3字符串
$@ is westos linux
[root@localhost mnt]# sh test.sh westos linux redhat $3后面接第三串字符
$0 is test.sh
$1 is westos
$2 is linux
$3 is redhat
$# is 3
$* is westos linux redhat
$@ is westos linux redhat

基于linux下的shell变量_vim_18

[root@localhost mnt]# ls
passfile test.sh user_create.sh userfile
[root@localhost mnt]# vim for.sh 编辑脚本检测$*和$@区别

基于linux下的shell变量_linux_19

[root@localhost mnt]# sh for.sh 

[root@localhost mnt]# sh -x for.sh westos linux redhat $*脚本后跟的所有字符串,模式为"1 2 3"一串字符
+ for name in '"$*"'
+ echo westos linux redhat
westos linux redhat

基于linux下的shell变量_linux_20

[root@localhost mnt]# vim for.sh  更改为$@

基于linux下的shell变量_vim_21

[root@localhost mnt]# sh -x for.sh westos linux redhat $@    脚本后跟的所有字符串,模式为"1""2""3"三串字符
+ for name in '"$@"'
+ echo westos
westos
+ for name in '"$@"'
+ echo linux
linux
+ for name in '"$@"'
+ echo redhat
redhat

基于linux下的shell变量_bash_22

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

[root@localhost mnt]# vim user_create.sh 编写脚本

基于linux下的shell变量_bash_23

[root@localhost mnt]# sh user_create.sh  调用
ERROR:please give me userfile or passfile!!
[root@localhost mnt]# sh user_create.sh userfile 文件少于两个
ERROR:please give me userfile or passfile!!
[root@localhost mnt]# sh user_create.sh userfile passfile 正确建立文件并显示密码

基于linux下的shell变量_bash_24

用read实现变量的传递,交互式传递:

read    WESTOS
read -s WESTOS -s表示加密
read -p "input:" WESTOS -p表示提示

操作演示:

[root@localhost mnt]# vim bb.sh
[root@localhost mnt]# sh bb.sh 非交互式没有提示
123
123

基于linux下的shell变量_bash_25

[root@localhost mnt]# vim bb.sh   加入提示
[root@localhost mnt]# sh bb.sh
please give me a filewestos
westos

基于linux下的shell变量_linux_26

[root@localhost mnt]# vim cc.sh   交互式提示并检测IP是否在运行
[root@localhost mnt]# sh cc.sh
please input a number: 172.25.254.121
172.25.254.121 is up

基于linux下的shell变量_bash_27

[root@localhost mnt]# vim cc.sh   输入时候加入加密
[root@localhost mnt]# sh cc.sh
please input a number: 172.25.254.121 is up

基于linux下的shell变量_vim_28

[root@localhost mnt]# vim cc.sh  加入空行不然显示看起来不好看
[root@localhost mnt]# sh cc.sh
please input a number:
172.25.254.121 is up

基于linux下的shell变量_vim_29

操作:

基于linux下的shell变量_vim_30

用脚本实现read交互式提示输入建立用户文件,密码文件并检测是否存在:

[root@localhost mnt]# vim user_create.sh  编写脚本

基于linux下的shell变量_linux_31

[root@localhost mnt]# sh user_create.sh  调用脚本
please input userfile: lee 文件错误
ERROR: lee is not exist!!
[root@localhost mnt]# sh user_create.sh
please input userfile: userfile
please input passfile: lee 文件名不匹配无法运行脚本
ERROR: lee is not exist!!
[root@localhost mnt]# sh user_create.sh 调用脚本
please input userfile: userfile 有提示输入文件
please input passfile: passfile
Changing password for user user1.
passwd: all authentication tokens updated successfully. 建立成功
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.

基于linux下的shell变量_vim_32

检测建立用户密码:

[root@localhost mnt]# su - user1  成功切换建立用户
[user1@localhost ~]$ su - user2 验证密码
Password:
Last failed login: Sun Jun 17 01:01:47 EDT 2018 on pts/1
There was 1 failed login attempt since the last successful login.
[user2@localhost ~]$ exit 退出
logout
[user1@localhost ~]$ exit
logout

基于linux下的shell变量_vim_33

linux系统中命令别名的设定:

[root@localhost mnt]# 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'
[root@localhost mnt]# alias xfl='vim' 设置别名

基于linux下的shell变量_linux_34

[root@localhost mnt]# xfl调用直接打开vim按:q退出

基于linux下的shell变量_linux_35

[root@localhost mnt]# 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'
alias xfl='vim'
[root@localhost mnt]# exit 退出失效
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121 -X
[root@localhost ~]# 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'

基于linux下的shell变量_bash_36

[root@localhost ~]# vim .bashrc  编辑用户家目录文件

基于linux下的shell变量_linux_37

[root@localhost ~]# source .bashrc 刷新
[root@localhost ~]# 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'
alias xfl='vim'
[root@localhost ~]# su -student 切换到学生用户
su: failed to execute tudent: No such file or directory
[root@localhost ~]# su - student
Last login: Sun Jun 17 04:52:35 EDT 2018 on pts/1
[student@localhost ~]$ xfl 不生效
bash: xfl: command not found...
[student@localhost ~]$ exit
logout

基于linux下的shell变量_bash_38

[root@localhost ~]# vim /etc/bashrc   编辑系统配置文件

基于linux下的shell变量_vim_39

[root@localhost ~]# source /etc/bashrc  刷新
[root@localhost ~]# su - student 切换用户
Last login: Sun Jun 17 22:44:46 EDT 2018 on pts/0
[student@localhost ~]$ alias 永久有效
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 vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
alias xfl='vim'
[student@localhost ~]$ xfl 调用
[student@localhost ~]$ exit
logout
[root@localhost ~]# vim /etc/bashrc 删除添加
[root@localhost ~]# vim .bashrc 删除添加
[root@localhost ~]# source .bashrc 刷新
[root@localhost ~]# source /etc/bashrc 刷新

基于linux下的shell变量_bash_40

[root@localhost ~]# 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'
alias xfl='vim'
[root@localhost ~]# unalias xfl unalias 撤销命令的别名
[root@localhost ~]# 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'

基于linux下的shell变量_linux_41

查询 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 ~]# echo $?
0
[root@localhost ~]# ls
anaconda-ks.cfg Documents Music Public Videos
Desktop Downloads Pictures Templates
[root@localhost ~]# echo $? 查看退出值
0
[root@localhost ~]# vim file.sh 编写脚本

基于linux下的shell变量_linux_42

[root@localhost ~]# sh file.sh 调用
hello world
[root@localhost ~]# echo $? 查看退出值
1

基于linux下的shell变量_bash_43

[root@localhost ~]# vim file.sh  编写脚本退出值自己设定

基于linux下的shell变量_vim_44

[root@localhost ~]# sh file.sh 调用
hello world
[root@localhost ~]# echo $? 查看退出值
66

基于linux下的shell变量_bash_45

利用命令执行结果设定变量:

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

编写脚本利用退出值检测ip

[root@localhost mnt]# vim cc.sh   编写脚本检测$?退出值是否等于0来返回值

基于linux下的shell变量_bash_46

[root@localhost mnt]# sh cc.sh 
please input a number:
172.25.254.111 is up

基于linux下的shell变量_vim_47

脚本中的函数:

函数可以确保命令循环执行,可以解决命令多重复的情况,
函数用来简化脚本,使脚本可以循环执行。

编写脚本利用函数循环检测IP是否运行并在输入exit退出:

[root@localhost mnt]# vim ip_check.sh   编写脚本

基于linux下的shell变量_linux_48

[root@localhost mnt]# sh ip_check.sh   检测脚本功能
please input a ipaddr: 172.25.254.111
172.25.254.111 is up
please input a ipaddr: 172.25.254.222
172.25.254.222 is down
please input a ipaddr: exit
byebye

基于linux下的shell变量_vim_49

编写脚本利用函数循环检测文件类型:

[root@localhost mnt]# vim file_check.sh   编写脚本

基于linux下的shell变量_vim_50

[root@localhost mnt]# sh file_check.sh /dev/vdb   检测文件类型
/dev/vdb is link
[root@localhost mnt]# sh file_check.sh /var/lib/mysql/mysql.sock
/var/lib/mysql/mysql.sock is not exist!!
[root@localhost mnt]# sh file_check.sh /dev/pts/1
/dev/pts/1 is link

基于linux下的shell变量_linux_51

利用脚本根据函数建立用户删除用户,并设置密码:

[root@localhost mnt]# vim user_ctl.sh  编写脚本

基于linux下的shell变量_bash_52

[root@localhost mnt]# sh user_ctl.sh   调用脚本演示功能
[root@localhost mnt]# sh user_ctl.sh
please input action: add
please input a username: lee 建立用户
please input a password:
Changing password for user lee.
passwd: all authentication tokens updated successfully.
please input action: del 删除用户
please input a username:lee
please input action: exit
bye

基于linux下的shell变量_bash_53