Shell脚本-3变量

一、变量概述

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

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

二、变量设置

1.环境级变量

只在顶级程序使用变量(shell)

[root@host2 ~]# echo $a
1
[root@host2 ~]# bash
[root@host2 ~]# ps f
  PID TTY      STAT   TIME COMMAND
 1872 pts/0    Ss     0:00 -bash
 1940 pts/0    S      0:00  \_ bash
 1973 pts/0    R+     0:00      \_ ps f
 1311 tty1     Ssl+   0:00 /usr/bin/X :0 -background none -noreset -audit 4 -verbose -
[root@host2 ~]# echo $a

在子程序中也可使用变量

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

2.用户级变量

[root@host2 ~]# vim .bash_profile 
[root@host2 ~]# cat .bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
export a=1
[root@host2 ~]# source .bash_profile 
[root@host2 ~]# echo $a
1
[root@host2 ~]# su - student
Last login: Tue May 15 23:14:00 CST 2018 on pts/0
[student@host2 ~]$ echo $a

[student@host2 ~]$ 

3.系统级变量

[root@host2 ~]# vim /etc/profile
[student@host2 ~]$ cat /etc/profile | grep "export a=1"
export a=1
[root@host2 ~]# source /etc/profile
[root@host2 ~]# echo $a
1
[root@host2 ~]# su - student
Last login: Wed May 16 19:34:54 CST 2018 on pts/0
[student@host2 ~]$ echo $a
1

三、变量的声明

1.字符的转译
 \    ##转译单个字符
 ' '   ##转译''中所有字符
 " "   ##弱引用,不能转译“\”、“$”、“`”、“!”
 $(date)   ##等同于`date`
[root@host2 ~]# echo "###`date`###"
###Wed May 16 19:50:26 CST 2018###
[root@host2 ~]# echo $(date)
Wed May 16 19:50:43 CST 2018
   $[1+2+3]  ##计算[]的值
   ${a}b     ##区分显示{}内变量
[root@localhost ~]# a=1
[root@localhost ~]# echo ${a}b
1b
2.变量值传递
   $1  ##脚本后的第1串字符
   $2  ##脚本后的第2串字符
   $#  ##脚本后字符串的个数
   $*  ##脚本后的所有字符串 " 1 2 3 ..."
   $@  ##脚本后的所有字符串 "1" "2" "3" "..."
   read -p " " 变量
   read -p " " -s 加密变量
3.编写脚本user_ctrl.sh实现建立和删除用户的功能
[root@host2 home]# vim user_ctrl.sh
[root@host2 home]# cat user_ctrl.sh
#!/bin/bash
[ -z $1 ] && {
    echo "ERROR:please input create or delete"
    exit 1
    }
[ $1 != "create" -a $1 != "delete" ]&& {
    echo "ERROR:please input create or delete"
    exit 1
    }
[ $1 = create ]&& {
    read -p "please give a username: " UserName
    User=`awk -F : '{print $1}' /etc/passwd | grep $UserName`
    [ -z "$User" ] && {
        read -p "please input passwd: " -s Passwd
        useradd $UserName
        echo $Passwd | passwd --stdin $UserName
        echo $UserName creates sucessfully
        exit 1
    }
    echo $User is exist
}
[ $1 = delete ] && {
    read -p "please give a username: " UserName
    User=`awk -F : '{print $1}' /etc/passwd | grep $UserName`
    [ -z "$User" ] && {
        echo $User is not exist
        exit 1
    }||{
        userdel -r $User
        echo $User is deleted
        }
}

验证脚本

[root@host2 home]# sh user_ctrl.sh create
please give a username: tom
please input passwd: Changing password for user tom.
passwd: all authentication tokens updated successfully.
tom creates sucessfully
[root@host2 home]# sh user_ctrl.sh create
please give a username: tom
tom is exist
[root@host2 home]# sh user_ctrl.sh delete
please give a username: hat
is not exist
[root@host2 home]# sh user_ctrl.sh delete
please give a username: tom
tom is deleted

4.设置系统命令别名

环境级:alias xie=’vim’

[root@host2 mnt]# alias xie='vim'
[root@host2 mnt]# xie passwd
[root@host2 mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync

用户级:vim .bashrc

系统级:vim /etc/bashrc

删除系统命令别名:unalias xie

利用命令执行结果设定变量
$? 是命令在执行完成之后产生的退出值(范围是0-255)
当$0=0 时标示命令执行没有错误输出,这个值可以用exit命令执行
[root@host2 mnt]# ls -l file 
-rw-r--r--. 1 root root 13 May 15 20:31 file
[root@host2 mnt]# echo $?
0
[root@host2 mnt]# acbd
bash: acbd: command not found...
[root@host2 mnt]# echo $?
127
[root@host2 mnt]# ping -c1 -w1 172.25.254.22 &> /dev/null
[root@host2 mnt]# echo $?
1
5.函数

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

[root@host2 mnt]# vim read.sh
[root@host2 mnt]# cat read.sh 
#!/bin/bash
READ(){
    read -p "please input a word: " WORD
    [ "$WORD" = "exit" ] &&{
        echo bye
        exit
    }||{
        echo ""
        echo "$WORD"
        READ
    }
}
READ
[root@host2 mnt]# sh read.sh 
please input a word: ww

ww
please input a word: woe

woe
please input a word: exit
bye
[root@host2 mnt]# 

用函数编写脚本判断文件类型

猜你喜欢

转载自blog.csdn.net/lj_11111/article/details/80342083