新秀篇 ##Linux中shell变量##

一、变量概述:

1.变量:在程序运行过程中允许改变值的量

2.特点:用一串固定的字符表示不固定的值;
是一种使用方便的占位符,用于引用计算机内存地址;
在shell中不能永久保存在系统中,必须在文件中声明;
3.种类
环境级:只在当前shell有效,shell关闭变量丢失;
用户级:只针对当前用户有效,其他用户无效;
系统级:当前系统所有用户有效;
二、变量设置:

1.环境级变量:
只在顶级程序使用变量(shell)

[root@localhost ~]# a=1
[root@localhost ~]# echo $a
1
[root@localhost ~]# bash
[root@localhost ~]# ps f
  PID TTY      STAT   TIME COMMAND
 2705 pts/1    Ss     0:00 -bash
32323 pts/1    S      0:00  \_ bash
32347 pts/1    R+     0:00      \_ ps f
 2187 pts/0    Ss+    0:00 /bin/bash
[root@localhost ~]# echo $a

在子程序中也可使用变量

[root@localhost ~]# export a=1
[root@localhost ~]# echo $a
1
[root@localhost ~]# bash
[root@localhost ~]# echo $a
1

2.用户级变量:

[root@localhost ~]# vim .bash_profile
[root@localhost ~]# source .bash_profile
[root@localhost ~]# echo $a
1
[root@localhost ~]# su - student 
Last login: Thu May 11 20:23:54 EDT 2017 on pts/0
[student@localhost ~]$ echo $a

3.系统级变量:

[root@localhost ~]# vim /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# echo $a
1
[root@localhost ~]# su - student 
Last login: Mon May 14 23:45:45 EDT 2018 on pts/1
[student@localhost ~]$ echo $a
1

三、变量的声明:

1.字符的转译:
\ ##转译单个字符

’ ’ ##转译”中所有字符

” ” ##弱引用,不能转译“\”、“$”、“`”、“!”

$(date) ##等同于 date

[root@localhost ~]# echo "###`date`###"
###Tue May 15 01:37:01 EDT 2018###
[root@localhost ~]# echo $(date)
Tue May 15 01:37:28 EDT 2018

   $[1+2+3]  ##计算[]的值
   ${a}b     ##区分显示{}内变量
[root@localhost ~]# a=1
[root@localhost ~]# echo ${a}b
1b

四.变量值传递:
$1 ##脚本后的第1串字符

$# ##脚本后字符串的个数

$* ##脚本后的所有字符串 ” 1 2 3 …”

$@ ##脚本后的所有字符串 “1” “2” “3” “…”
read -p ” ” 变量
read -p ” ” -s 加密变量
【实验测试】:
1.变量值传递:

[root@localhost mnt]# vim text.sh                   ##编辑脚本
[root@localhost mnt]# cat text.sh
#!/bin/bash
echo \$0  is  $0
echo \$1  is  $1
echo \$2  is  $2
echo \$3  is  $3
echo \$#  is  $#
echo \$*  is  $*
echo \$@  is  $@
[root@localhost mnt]# sh text.sh                    ##执行脚本
$0 is text.sh                       ##脚本名称有显示
$1 is
$2 is
$3 is
$# is 0
$* is
$@ is
[root@localhost mnt]# sh text.sh westos                    ##在脚本后加一个字符
$0 is text.sh
$1 is westos                           ##脚本第一行有显示
$2 is
$3 is
$# is 1                                ##脚本后字符串个数有显示
$* is westos
$@ is westos
[root@localhost mnt]# sh text.sh westos linux
$0 is text.sh
$1 is westos
$2 is linux                                 ##脚本第二行有显示
$3 is
$# is 2
$* is westos linux
$@ is westos linux
[root@localhost mnt]# sh text.sh westos linux redhat
$0 is text.sh
$1 is westos
$2 is linux
$3 is redhat
$# is 3
$* is westos linux redhat
$@ is westos linux redhat

2.$$*与$#的区别:
$*:后面是一串字符
$#:后面是所有字符串

【证明】:
[root@localhost mnt]# vim for.sh            ##编辑脚本
[root@localhost mnt]# cat for.sh                   ##查看脚本内容
#!/bin/bash
for name in "$*"
do
         echo $name
done
[root@localhost mnt]# sh -x for.sh westos linux redhat          ##执行脚本
+ for name in '"$*"'
+ echo westos linux redhat
westos linux redhat                        ##总共进行了一次
[root@localhost mnt]# vim for.sh                   
[root@localhost mnt]# cat for.sh
#!/bin/bash
for name in "$@"
do
         echo $name
done
[root@localhost mnt]# sh -x for.sh westos linux redhat
+ for name in '"$@"'
+ echo westos
westos
+ for name in '"$@"'
+ echo linux
linux
+ for name in '"$@"'
+ echo redhat
redhat                                      ##总共进行了三次

3.用read实现变量传递:
read WESTOS
read -s WESTOS
read -p “input:” WESTOS

【测试IP的交互式传参】:

[root@localhost mnt]# vim text.sh 
#!/bin/bash
read -p "Please input a number: " IP
ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down 
[root@localhost mnt]# sh text.sh
Please input a number: 172.25.254.20
172.25.254.20 is up

【在登录用户和密码时使用交互式传参】:

[root@localhost mnt]# vim create_user.sh 
#!/bin/bash
read -p "Please input user file: " USERFILE
[ ! -e $USERFILE ]&&{
    echo -e "\033[31mERROR: $USERFILE is not exit!!\033[0m"
    exit 1
}
read -p "Please input password file: " PASSWORDFILE
[ ! -e $PASSWORD ]&&{
    echo -e "\033[31mERROR: $PASSWORDFILE is not exit!!\033[0m"
    exit 1
}
MAX_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $USERFILE`
for LINE_NUM in `seq 1 $MAX_LINE`
do
        USERNAME=`sed -n "${LINE_NUM}p" $USERFILE`
        PASSWORD=`sed -n "${LINE_NUM}p" $PASSWORDFILE`
        useradd $USERNAME
        echo $PASSWORD| passwd --stdin $USERNAME 
done
[root@localhost mnt]# chmod +x create_user.sh 
[root@localhost mnt]# ./create_user.sh
Please input user file: userfile
Please input password file: passfile

五.Linux 系统中命令别名的设定:
环境级:alias xie=’vim’
用户级:vim .bashrc
系统级:vim /etc/bashrc
删除别名设定:unalias xie

【实验】:
[root@localhost mnt]# vim /etc/bashrc        ##编辑配置文件永久添加
写入:alias xie='vim'         ##加入别名
[root@localhost mnt]# source /etc/bashrc                ##刷新配置文件
[root@localhost mnt]# xie             ##测试可以使用
[root@localhost ~]# su - student              ##切换到普通用户
[student@localhost ~]$ xie                   ##测试可以写入
删除别名设定:
[student@localhost ~]$ su -             ##超级用户
Password: 
Last login: Sun Jun 17 22:23:48 EDT 2018 on pts/0
[root@localhost ~]# unalias xie            ##删除vim的别名xie
[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'
[root@localhost ~]# xie                 ##再次测试
bash: xie: command not found...                 ##别名配置消失

六.利用命令执行结果设定变量:
$$? 是命令在执行完成之后产生的退出值,为是【0-255】
当$0=0时标示命令执行没有错误输出,这个值可以用exit 命令执行 例如:exit 66

【实验】:用退出值显示:
[root@localhost mnt]# vim file.sh
[root@localhost mnt]# cat file.sh
#!/bin/bash
PING()
{               read -p "please input a ipaddress:"  IP
                [  "$IP" = exit ] &&{
                         echo bye
                         exit  0
                }
               ping -cl -wl $IP &> /dev/null && echo $IP  is UP || echo $IP is down
               PING

}
PING
[root@localhost mnt]# sh file.sh
please input a ipaddress:172.25.254.11
172.25.254.11 is down
please input a ipaddress:172.25.254.20
172.25.254.20 is down
please input a ipaddress:exit
bye                                      ##显示

猜你喜欢

转载自blog.csdn.net/china_zgd/article/details/80759667