shell中的变量
1.变量的定义:
1>变量即在程序运行过程中它的值是允许改变的值2>变量是用一串固定的字符来标示不固定的值的一种方法
3>变量是一种使用方便的占位符,用于引用计算机内存地址,该地址可以存储Script运行时可更改的程序信息
4>在shell中变量是不能永久保存在系统中的,必须在文件中声明
2.变量名称的规范:
变量名称中通常包括大小写字母,数字,下划线(不是必须)变量名称格式
WESTOS_LINUX
Westos_Linux
westoS_Linux
字符的转译及变量的声明
\ 转译单个字符
"" 弱引用,批量转译""中出现的字符;
" 强引用,批量转译"中出现的字符;
"" " 两者区别在于,""不能转译"\","!","$"
${} 变量声明
3 变量类型:
3.1> 环境级变量:只在当前shell中生效,shell关闭变量丢失
export A=1echo $A
重新打开一个shell 测试 echo $A
3.2> 用户级变量:写在用户的骨文件中,只针对当前用户生效
vim ~/bash_profileexport A=2
echo $A
source ~/bash_profile #重新加载使更改重新加载到该用户配置内存里
echo $A
su - student
echo $A
3.3>系统级变量:被写在系统的配置文件/etc/profile中
vim /etc/profileexport A=3 #把信息放到公开的区域
source /etc/profile
echo $A
exit
ssh root@172.25.254.132
echo $A
![](https://i-blog.csdnimg.cn/blog_migrate/dc08967f559c309eadf725028c4e50ed.png)
3.2>更改路径
1 vim file.sh
2 chmod +x file.sh
3 /mnt/file.sh
4 PATH=$PATH:/mnt #临时更改
5 file.sh
6 exit
7 file.sh
8 vim /etc/profile #永久更改
9 source /etc/profile
10 echo $PATH
11 file.sh
12 exit
13 file.sh
4 更改shell变量
1 echo $PS1
[\u@\h \W]\
2 PS1='westos>'#更改shell变量
westos>PS1='westos \u>'
westos root>PS1='westos \W>' #W为相对路径
westos ~>cd /etc/httpd/
westos httpd>PS1='westos \w>' #w为绝对路径
westos /etc/httpd>cd /mnt/
westos /mnt>exit
logout
Connection to 172.25.254.132 closed.
变量即在程序运行时。
变量保存在内存中。
硬盘永久,内存临时的。
进程分为父极进程和子进程。
5 变量值传递
$* #默认为一串字符 " 1 2 3"
$@ #默认为三串字符"1" "2" "3"
$0 #本身
$# #脚本后所根的字符数
$1 #脚本后的第一串字符串
$2 #脚本后的第二串字符串
$3 #脚本后的第三串字符串
1 cd /mnt/
2 ps
3 echo $$
4 vim &
5 echo $$
6 vim test.sh
7 cat test.sh
8 sh test.sh
9 sh test.sh linux
10 sh test.sh linux hello rdhat
5.1>比较$*和$@
1 vim for.sh
2 sh -x for.sh linux westos hello
+ for name in '"$*"'
+ echo linux westos hello
linux westos hello
3 cat for.sh
#!/bin/bash
for name in "$*"
do
echo $name
done
4 vim for.sh
5 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
6 cat for.sh
#!/bin/bash
for name in "$@"
do
echo $name
done
![](https://i-blog.csdnimg.cn/blog_migrate/3ac69308dafd4b3f2468fa546ad24129.png)
脚本实验:
写一个脚本建立用户并设置密码给该用户
1 vim user_create.sh
#!/bin/bash
[ "$#" -eq "2" ] ||{
echo "Please input username and password !!!" #当script后没有输入两串字符时
exit 1
}
MAX_LINE=`wc -l $1|cut -d " " -f 1`
for NUM in `seq 1 $MAX_LINE`
do
USERNAME=`sed -n ${NUM}p $1` #第一串字符为用户名
PASSWORD=`sed -n ${NUM}p $2` #第二串字符为用户密码
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME
done
2 vim userfile
3 vim passfile
4 sh user_show.sh userfile passfile
6 read 变量的交互式传参
read命令从键盘读取变量的值,通常用在shell脚本中与用户进行交互的场合。用read实现变量传递
read WESTOS
read -s WESTOS #-s表示加密
read -p "input" WESTOS #指定读取时的提示符
1 vim hello.sh
2 cat hello.sh
#!/bin/bash
read WESTOS #无提示
echo $WESTOS
3 sh hello.sh
4 vim hello.sh
5 sh hello.sh
6 cat hello.sh
#!/bin/bash
read -p "Please give me a file " WESTOS #有提示
echo $WESTOS
7 vim read.sh
#!/bin/bash
read -p "Please give me a number: " IP
ping -c1 -w1 $IP &> /dev/null && echo $IP IS up || echo $IP is down
8 sh read.sh
![](https://i-blog.csdnimg.cn/blog_migrate/21fb584ec2733077bf63587fa37b845b.png)
脚本实验:
用脚本实现提示输入建立用户文件,密码文件检测是否存在:
1 vim user_create.sh
#!/bin/bash
read -p "Please input userfile: " USERFILE
[ ! -e $USERFILE ] && {
echo -e "\033[32mERROR: $USERFILE is not exist !!\033[0m"
exit 1
}
read -p "Please input pasfile: " PASSFILE
[ ! -e $PASSFILE ] && {
echo -e "\033[33mERROR: $PASSFILE is not exist !!\033[0m"
exit 1
}
MAX_LINE=`awk 'BEGIN{n=0}{n++}END{print n}' $USERFILE`
for NUM in `seq 1 $MAX_LINE`
do
USERNAME=`sed -n ${NUM}p $USERFILE`
PASSWORD=`sed -n ${NUM}p $PASSFILE`
useradd $USERNAME
echo $PASSWORD |passwd --stdin $USERNAME
done
测试:
2 sh user_create.sh
3 id user1
4 id user2
5 id user3
user_create.sh
2 Linux系统中命令别名的设定
2.1>临时设定;效力仅及于该次登入的操作
1 alias
2 alias xie='vim'
3 xie
4 alias
5 exit
![](https://i-blog.csdnimg.cn/blog_migrate/a8a139d3fc7b99e6170a0a29f40b1e10.png)
2.2>永久设定指令的别名;每次登入时,自动设好别名
1 vim .bashrc
alias xie='vim'
2 source .bashrc
3 alias
4 su - student #此时在student用户中并不能执行xie操作;当重新打开一个shell是可以执行的
5 vim /etc/bashrc
alias xie='vim'
6 source /etc/bashrc
7 su - student
xie
8 alias
![](https://i-blog.csdnimg.cn/blog_migrate/bf6aafe66f128dbe142fd0718267f064.png)
2.3>撤销指令的别名
1 vim /etc/bashrc
#alias xie='vim'
2 source /etc/bashrc
3 vim .bashrc
#alias xie='vim'
4 source .bashrc
5 su - student
6 alias
7 unalias xie #取消命令别名
8 alias
3 查询 exit退出值
1 echo $?
2 ls
3 vim file.sh
4 sh file.sh
5 cat file.sh
#!/bin/bash
echo hello world
exit 1
6 echo $?
7 sh file.sh
8 echo $?
9 vim file.sh #修改退出值
10 sh file.sh
11 echo $? #查看退出值
12 cat file.sh
#!/bin/bash
echo hello world
exit 6
利用命令执行结果设定变量
Hostname=$(hostname)
Hostname=`hostname`
$?
$?是命令在执行完成之后产生的退出值范围是[0-255]
当$0=0时标示命令执行没有错误输出;这个可以用exit命令来执行
1 vim ip_check.sh
#!/bin/bash
ping -c1 -w1 $1 &>/dev/null
[ "$?" -eq "0" ] && echo $1 is up || echo $1 is down #根据退出值判断网络是否畅通
2 sh ip_check.sh 172.25.254.33
3 vim ip_check.sh #非交互式
#!/bin/bash
read -p "Please give me a ipaddress : " -s IP
echo " "
ping -c1 -w1 $IP &>/dev/null
[ "$?" -eq "0" ] && echo $IP is up || echo $IP is down
4 sh ip_check.sh
Please give me a ipaddress :
172.25.254.64 is up
![](https://i-blog.csdnimg.cn/blog_migrate/59d8104312fa4ece147df2fa13f8d41a.png)
3.1 脚本中的函数
函数可以确保命令循环执行,可以解决命令多重复的情况,函数用来简化脚本,使脚本可以循环执行。编写脚本利用函数循环检测IP输入exit退出:
1 vim ip_check.sh
#!/bin/bash
PING()
{
read -p "Please give me a ipaddress : " IP
[ "$IP" = exit ] && {
echo bye
exit 0
}
ping -c1 -w1 $IP &>/dev/null && echo $IP is up || echo $IP is down
PING
}
PING
2 sh ip_check.sh
Please give me a ipaddress : 172.25.254.32
172.25.254.32 is down
Please give me a ipaddress : 172.25.254.64
172.25.254.64 is up
Please give me a ipaddress : exit
bye
![](https://i-blog.csdnimg.cn/blog_migrate/419765bc4b1b703a2c8e5f86b52f627d.png)
脚本实验:
编写脚本利用函数循环检测文件类型:
1 vim file_check.sh
#!/bin/bash
[ -z "$1" ] &&{
echo "Plesse input a filename !!"
exit 1
}
FILE_CHECK()
{
[ "$1" "$2" ] &&{
echo $2 is $3
exit 0
}
}
[ -e "$1" ]||{
echo $1 is not exist!!
exit 0
}
FILE_CHECK -L $1 link
FILE_CHECK -B $1 block
FILE_CHECK -S $1 socket
FILE_CHECK -d $1 directory
FILE_CHECK -c $1 zifu
FILE_CHECK -f $1 common
利用脚本根据函数建立用户删除用户,并设置密码。
1 vim user_ctrl.sh
#!/bin/bash
ACTION_ADD(){
[ "$1" = ADD ] && {
read -p "Please input a username: " USERNAME
read -p "Please input password: " -s PASSWORD
echo " "
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME
}
}
ACTION_DEL(){
[ "$1" = DEL ]&& {
read -p "Please input a username: " USERNAME
echo " "
userdel -r $USERNAME
}
}
USER_CTRL()
{
read -p "Please input action : " ACTION
[ "$ACTION" = exit ] && {
echo bye
exit 1
}
ACTION_ADD $ACTION
ACTION_DEL $ACTION
USER_CTRL
}
USER_CTRL
![](https://i-blog.csdnimg.cn/blog_migrate/96bf15b40709ddd303e9847ba8ae8170.png)
2 sh user_ctrl.sh
Please input action :
Please input action : ADD
Please input a username: linux
Please input 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
![](https://i-blog.csdnimg.cn/blog_migrate/a8eb70c47dc038e578b77a59f5cce1de.png)