文章目录
1、变量的定义
定义:变量就是内存的一片区域地址
存在的意义:命令无法操作一直变化的目标,用一串固定的字符来表示不固定的目标可以解决此问题
2、shell脚本中变量的定义方法
2.1 环境级别
export a=1
在环境关闭后变量失效
[root@server1 ~]# a=1
[root@server1 ~]# echo $a
1
[root@server1 ~]# vim test.sh
1 ##################
2 # AUthor
3 # Create_Time: 2022/09/28
4 ##################
5
6 #!/bin/bash
7 echo $a
[root@server1 mnt]# sh test.sh 没有结果,因为a=1没有放在公共区域
[root@server1 mnt]# export a=1 设置为公共区域,所有程序i运行都可以访问
[root@server1 mnt]# env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
a=1 a=1已经放到公共区域
PWD=/mnt
XMODIFIERS=@im=ibus
[root@server1 mnt]# export a=1
[root@server1 mnt]# sh test.sh
1
注意:只有在当前shell生效,重新开启shell,执行test.sh又不会生效
2.2用户级别
vim ~/.bash_profile
exprot a=1
[root@server1 mnt]# vim ~/.bash_profile
[root@server1 mnt]# source ~/.bash_profile 生效
[root@server1 mnt]# sh test.sh 运行
1
[root@server1 mnt]# logout 退出shell
Connection to 172.25.50.1 closed.
[westos@foundation50 network-scripts]$ ssh root@172.25.50.1 重新连接shell
root@172.25.50.1's password:
Last login: Sat Oct 8 05:01:33 2022 from 172.25.50.250
[root@server1 mnt]# sh test.sh 运行程序,还是能成功
1
[westos@server1 ~]$ sh /mnt/test.sh 运行,不能成功,因为~/.bash_profile是对root用户执行的
2.3系统级别
vim /etc/profile
export a=1
注意:这个文件设置错误,系统会起不来
vim /etc/profile.d/westos.sh 一般在此路经创建.sh文件,系统会自动识别
export a=1
[root@server1 mnt]# vim /etc/profile.d/westos.sh
#!/bin/bash
export a=1
[root@server1 mnt]# source /etc/profile.d/westos.sh 运行
[root@server1 mnt]# su - westos
Last login: Sat Oct 8 05:16:17 CST 2022 on pts/0
[westos@server1 ~]$ sh /mnt/test.sh 对所有用户可以执行
1
3. 变量的命名规则
3.1变量名称可包含的字符
字母
下划线_
数字
3.2 变量名称定义规则
不能用数字开头
建议
变量名称短全用大写字符
变量名称长用_区分字类
WESTOS
Westos_Linux
westoS_Linux
3.3.变量的转译
\ 转译单个字符
“ ” 弱引用 不能转译\ ’ $ !
‘ ’ 强引用 批量转译
[root@server1 westos]# echo $
$
[root@server1 westos]# echo $2 $为特殊字符,要写$2必须转译
[root@server1 westos]# echo \$2 转译,只能转译单个字符
$2
[root@server1 westos]# echo \$\$2
$$2
[root@server1 westos]# echo '$$' 批量转译,' '为强转译,所有字符都可以转译
$$
[root@server1 ~]# echo "\" 无法转译 , 弱引用 不能转译\ ' $ !
>
[root@server1 ~]# echo "$2" 无法转译
3.4.声名变量
[root@server1 ~]# a=1
[root@server1 ~]# echo $ab 没有显示,此时ab为变量
[root@server1 ~]# echo ${a}b 声名$a为变量,用{}
1b
4. 数组变量的管理
变量的数组
a=(1 2 3 4 5) | |
---|---|
echo ${a[0]} | 获取第一列元素 |
echo ${a[-1]} | 最后一个元素 |
echo ${a[*]} | 所有元素 |
echo ${a[@]} | 所有元素 |
echo ${a[@]:0:3} | 从0开始往后数三个 |
echo ${#a[@]} | 表示获取数组元素的个数 |
[root@server1 ~]# a=(1 2 3 4 5)
[root@server1 ~]# echo $a 默认显示第一个值
1
[root@server1 ~]# echo ${a[0]} 0表示数组中第一个元素
1
[root@server1 ~]# echo ${a[1]} 1表示数组中第二个元素
2
[root@server1 ~]# echo ${a[3]} 3表示数组中第四个元素
4
[root@server1 ~]# echo ${a[2]} 2表示第二个元素
3
[root@server1 ~]# echo ${a[*]} * 表示所有元素,其中*表示一串字符
1 2 3 4 5
[root@server1 ~]# echo ${a[@]} @也表示所有元素 , @表示五串字符
1 2 3 4 5
[root@server1 ~]# echo ${a[-1]} 最后一个元素
5
[root@server1 ~]# echo ${a[-2]} 倒数第二个元素
4
[root@server1 ~]# echo ${a[@]:2:3} 从第三个元素开始顺延三个元素
3 4 5
[root@server1 ~]# echo ${#a[@]} 查看有多少个元素
5
[root@server1 ~]# a[5]=7 添加第6元素为7
[root@server1 ~]# echo ${a[@]}
1 2 3 4 5 7
[root@server1 ~]# unset a[5]=7 删掉第六个元素
[root@server1 ~]# echo ${a[@]}
1 2 3 4 5
[root@server1 ~]# unset a 删除所有元素
[root@server1 ~]# echo ${a[@]}
5. 在shell中设定命名别名
alias xie =‘vim’ 临时设定
[root@server1 ~]# alias xie='vim' 临时设定
[root@server1 ~]# alias
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 xie='vim' 已经加入了
[root@server1 ~]# vim ~/.bashrc 针对当前用户生效
[root@server1 ~]# source ~/.bashrc 生效
[root@server1 ~]# vim /etc/bashrc 针对系统生效
一般文件最后添加
[root@server1 ~]# source /etc/bashrc 生效
[root@server1 westos]# unalias xie 在当前环境中立即删除别名
6. 用户环境变量的设定
[root@server1 ~]# echo $PS1 PS1 变量指的是server1
[\u@\h \W]\$
[root@server1 ~]# PS1="westos >" 设定变量为westos >
westos >ls
test.sh
westos >PS1="[\u@\h \W]\$" 恢复原先的
[root@server1 ~]$
[root@server1 mnt]# chmod +x /mnt/westos.sh
[root@server1 mnt]# /mnt/westos.sh 此时可以执行
Sun Oct 9 04:20:43 CST 2022
October 2022
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
[root@server1 mnt]# westos.sh 不能执行,如何让脚本执行的路经/mnt/westos.sh 变成自动检索的路经
bash: westos.sh: command not found
[root@server1 mnt]# echo $PATH 查看环境变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 发现这里面没有出现/mnt这个路经,需要添加
[root@server1 mnt]# export PATH=$PATH:/mnt 添加,临时设定
[root@server1 mnt]# westos.sh 可以执行了
Sun Oct 9 04:37:41 CST 2022
October 2022
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
当前用户永久设定
[root@server1 mnt]# vim ~/.bash_profile
[root@server1 mnt]# source ~/.bash_profile 执行
针对所有用户设定
[root@server1 mnt]# vim /etc/profile.d/PATH.sh
[root@server1 mnt]# source /etc/profile.d/PATH.sh 执行
root@server1 mnt]# westos.sh 可以执行
Sun Oct 9 05:02:08 CST 2022
October 2022
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
7. 脚本中传参方法
非交互式
[root@server1 mnt]# vim westos.sh
[root@server1 mnt]# sh westos.sh westos linux
$0 is westos.sh $0表示脚本本身
$1 is westos 脚本后输入的第一串字符
$2 is linux 脚本后输入的第二串字符
$3 is
$# is 2 $# 所输入的字符串个数
$* is westos linux $* 脚本后输入的字符串个数,为一串字符
$@ is westos linux $@ 脚本后输入的字符串个数,为三串字符
交互式传参
[root@server1 mnt]# vim westos1.sh
##################
# AUthor
# Create_Time: 2022/10/09
##################
#!/bin/bash
read -p "Please input word:" WORD
echo $WORD
[root@server1 mnt]# sh westos1.sh
Please input word:hello
hello
[root@server1 mnt]# vim westos1.sh
1 ##################
2 # AUthor
3 # Create_Time: 2022/10/09
4 ##################
5
6 #!/bin/bash
7 read -p "Please input word:" -s WORD -s表示隐藏输入的字符
8 echo ""
9 echo $WORD
[root@server1 mnt]# sh westos1.sh
Please input word: 隐藏输入的字符
hello
8.脚本中的函数应用
函数相当于脚本里面的脚本
[root@server1 mnt]# vim westos.sh
1 ##################
2 # AUthor
3 # Create_Time: 2022/10/09
4 ##################
5
6 #!/bin/bash
7 ECHO()
8 {
9 echo "hello westos"
10 echo "linux"
11 }
ECHO
[root@server1 mnt]# sh westos.sh 运行,函数里面的两个动作执行了
hello westos
linux
[root@server1 mnt]# vim westos.sh
1 ##################
2 # AUthor
3 # Create_Time: 2022/10/09
4 ##################
5
6 #!/bin/bash
7 ECHO()
8 {
9 echo "hello westos"
10 echo "linux"
11 ECHO 循环执行echo
12 }
13 ECHO
[root@server1 mnt]# sh westos.sh
hello westos
linux
hello westos
linux
hello westos
linux
hello westos
linux
hello westos
linux
hello westos
linux
hello westos
linux
函数的意义:使脚本更加简便,让函数循环执行
9.练习
[root@localhost Desktop]# ctrl_user1.sh
> please input action:add
> please input username:westos
> please input password:westos
> westos is created \ > please input action :exit
> please input action :del
> please input username :westos
> westos is deleted
> please input action :exit
>
[root@server1 mnt]# vim create_user.sh
##################
# AUthor
# Create_Time: 2022/10/09
##################
#!/bin/bash
[ "$*" != "add" -a "$*" != "del" ] && {
echo "Error: Wrong action!! Please input add|del following $0 !!"
exit
}
read -p "Please input username :" USERNAME
[ "$*" = "add" ] && {
id $USERNAME &> /dev/null && {
echo $USERNAME is exist !!
}||{
read -p "Please input password: " -s PASS
echo ""
useradd $USERNAME
echo $PASS | passwd --stdin $USERNAME &> /dev/null && echo $USERNAME is created
}
}||{
id $USERNAME &> /dev/null && {
userdel -r $USERNAME
}||{
echo $USERNAME is not exist !!
}
}