shell的变量
一、变量概述
1.变量:在程序运行过程中允许改变值的量
2.特点:用一串固定的字符表示不固定的值;
是一种使用方便的占位符,用于引用计算机内存地址;
在shell中不能永久保存在系统中,必须在文件中声明;
3.种类
环境级:只在当前shell有效,shell关闭变量丢失;
用户级:只针对当前用户有效,其他用户无效;
系统级:当前系统所有用户有效;
二、变量设置
1.环境级变量
只在顶级程序使用变量(shell)
[[email protected] ost]# a=1
[[email protected] ost]# echo $a
1
[[email protected] ost]# bash
[[email protected] ost]# ps f
PID TTY STAT TIME COMMAND
3156 pts/0 S 0:00 su - root
3162 pts/0 S 0:00 \_ -bash
6908 pts/0 S 0:00 \_ bash
6932 pts/0 R+ 0:00 \_ ps f
762 tty1 Ss+ 0:55 /usr/bin/Xorg :0 -background none -verbose -auth
在子程序中也可使用变量
[[email protected] ost]# export a=1
[[email protected] ost]# echo $a
1
[[email protected] ost]# bash
[[email protected] ost]# echo $a
1
2.用户级变量
[[email protected] ~]# vim .bash_profile
12 export PATH
13 export a=1
[[email protected] ~]ls -a
[[email protected] ~]# source .bash_profile
[[email protected] ~]# echo $a
1
[[email protected] ~]# su - kiosk
Last login: Thu Jun 21 09:00:05 CST 2018 on :0
[[email protected] ~]$ echo $a
3.系统级变量
[[email protected] ~]# vim /etc/profile
[[email protected] ~]# source /etc/profile
[[email protected] ~]# echo $a
2
[[email protected] ~]# su - kiosk
Last login: Thu Jun 21 11:49:38 CST 2018 on pts/0
[[email protected] ~]$ echo $a
2
三、变量的声明
1.字符的转译
\ ##转译单个字符
' ' ##转译''中所有字符
" " ##弱引用,不能转译“\”、“$”、“`”、“!”
$(date) ##等同于`date`
[[email protected] ~]# echo "###`date`###"
###Thu Jun 21 13:42:20 CST 2018###
[[email protected] ~]# echo $(date)
Thu Jun 21 13:42:45 CST 2018
$[1+2+3] ##计算[]的值
${a}b ##区分显示{}内变量
[[email protected] ~]# a=1
[[email protected] ~]# echo ${a}b
1b
2.变量值传递
$1 ##脚本后的第1串字符
$2 ##脚本后的第2串字符
$# ##脚本后字符串的个数
$* ##脚本后的所有字符串 " 1 2 3 ..."
[email protected] ##脚本后的所有字符串 "1" "2" "3" "..."
read -p " " 变量
read -p " " -s 加密变量
3.编写脚本user_ctrl.sh实现建立和删除用户的功能
[[email protected] mnt]# 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 exit
}
[ $1 = delete ] && {
read -p "please give a username: " UserName
User=`awk -F : '{print $1}' /etc/passwd | grep $UserName`
[ -z "$User" ] && {
echo $UserName is not exit
exit 1
} ||
userdel -r $User
echo $User is deleted!!
}
实验效果:
[[email protected] ost]# sh user_ctrl.sh
Error:please input create or delete!!
[[email protected] ost]# sh user_ctrl.sh create
please give a username: tom
tom is exit
[[email protected] ost]# sh user_ctrl.sh create
please give a username: soul
please input passwd: Changing password for user soul.
passwd: all authentication tokens updated successfully.
soul creates sucessfully
[[email protected] ost]# sh user_ctrl.sh delete
please give a username: soul
soul is deleted!!
####交互式####
4.设置系统命令别名
环境级:alias xie='vim'
用户级:vim .bashrc
系统级:vim /etc/bashrc
unalias xie
5.函数
用函数编写脚本判断文件类型
[[email protected] mnt]#vim check_file3.sh
#!/bin/bash
Check_File(){
[ $1 $2 ] && {
echo $3
} || {
echo $4
# exit 1
}
}
Check_File "-e" $1 "" "$1 is not exit"
Check_File "-L" $1 "$1 is a link file"
Check_File "-f" $1 "$1 is a common file"
Check_File "-S" $1 "$1 is a socket"
Check_File "-d" $1 "$1 is a directory"
Check_File "-c" $1 "$1 is a char file"
Check_File "-b" $1 "$1 is a block"
[[email protected] ost]# sh check_file3.sh test456
test456 is a link file
test456 is a common file