shell脚本中的变量

本文详细介绍了Shell脚本中变量的定义方法,包括环境级别(如系统、用户和环境关闭后)、变量命名规则及建议。讲解了变量转译、数组的使用,以及Linux命令别名设定和用户环境变量的更改。还涵盖了脚本中的参数传递和脚本函数的定义与应用。

一、变量的定义

#### 定义本身
变量就是内存一片区域的地址
#### 变量存在的意义
命令无法操作一直变化的目标
用一串固定的字符来表示不固定的目标可以解决此问题

二、shell脚本中变量的定义方法

环境级别

export a=1
在环境关闭后变量失效

用户级别

vim ~/.bash_profile
export a=1

 切换用户后失效

系统级别

vim /etc/profile
export a=2
vim /etc/profile.d/westos.sh
export b=3

##可以写在配置文件中但不建议,容易破坏配置文件

##写在配置文件子文件中比较安全

变量名称

变量名称可包含的字符

字母
下划线_
数字

 

变量名称定义规则

不能用数字开头

建议:

变量名称短全用大写字符
变量名称长用_区分子类
WESTOS
Westos_Linux
westoS_Linux

三、变量的转译

1、转译

\            #转译单个字符
" "         #弱引用,批量转译个数字符 不能转译"\ " "" "$" "!"
' '          #强引用

 2、声明

a=1
echo $ab
echo ${a}b

 3、变量的数组

a=(1 2 3 4 5)
a$[a[@]:起始元素id:元素个数]
echo ${a[0]}        ##数组中第一个元素
echo ${a[1]}        ##数组中最后一个元素
echo ${a[*]}        ##数组中所有元素
echo ${a[@]}        ##数组中所有元素
echo ${a[@]:0:3}        ##数组中13个元素
echo ${#a[@]}        ##数组中元素的个数
unset a[n]        ##删除数组中的第n1个元素

unset a        ##删除a这个数组

[root@westoslinux ~]# a=1
[root@westoslinux ~]# a=(1 2 3 4 5)
[root@westoslinux ~]# echo $a
1
[root@westoslinux ~]# echo ${a[0]}
1
[root@westoslinux ~]# echo ${a[1]}
2
[root@westoslinux ~]# echo ${a[-1]}
5
[root@westoslinux ~]# echo ${a[@]} ##这里显示的是“1”“2”“3”“4”“5”
1 2 3 4 5
[root@westoslinux ~]# echo ${a[*]} ##这里显示的是“1 2 3 4 5”
1 2 3 4 5
[root@westoslinux ~]# echo ${a[@]:0:3} ##从0位开始数3位
1 2 3
[root@westoslinux ~]# echo ${a[@]:0:4}
1 2 3 4
[root@westoslinux ~]# echo ${#a[@]} ##元素个数
5
[root@westoslinux ~]# a[5]=6  ##添加
[root@westoslinux ~]# echo ${a[*]}
1 2 3 4 6
[root@westoslinux ~]# unset a[4] ##删除
[root@westoslinux ~]# echo ${a[*]}
1 2 3 4

[root@westoslinux ~]# unset a  ##删除a
[root@westoslinux ~]# echo ${a[*]}

四、Linux中命令的别名设定

alias xie='vim'        ##临时设定
vim ~/.bashrc
alias xie='vim'        ##只针对与用户生效
vim /etc/bashrc        ##针对系统所以用户生效
alias xie='vim'
unalias xie        ##删除当前环境中的alias

[root@westoslinux ~]# alias  ##查看所有别名设定

[root@westoslinux ~]# alias xie='vim'  ##临时设定
[root@westoslinux ~]# xie
[root@westoslinux ~]# exit
logout
Connection to 172.25.254.111 closed.
[westos@westos_student11 Desktop]$ ssh -l root 172.25.254.111
root@172.25.254.111's password: 
Activate the web console with: systemctl enable --now cockpit.socket

This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register

Last login: Fri Nov 19 13:55:14 2021 from 172.25.254.11
[root@westoslinux ~]# xie
bash: xie: command not found...  ##换环境后失效

[root@westoslinux ~]# vim ~/.bashrc 
[root@westoslinux ~]# cat ~/.bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias xie='vim'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
[root@westoslinux ~]# source ~/.bashrc
[root@westoslinux ~]# xie
[root@westoslinux ~]# su - westos
Last login: Fri Nov 19 13:59:37 CST 2021 on pts/0
[westos@westoslinux ~]$ xie
bash: xie: command not found...  ##其他用户失效

[westos@westoslinux ~]$ vim /etc/bashrc 
alias xie='vim'

[westos@westoslinux ~]$ exit
logout
[root@westoslinux ~]# vim /etc/bashrc
[root@westoslinux ~]# source /etc/bashrc
[root@westoslinux ~]# su - westos
Last login: Fri Nov 19 15:08:31 CST 2021 on pts/0
[westos@westoslinux ~]$ xie  ##其他用户生效

[root@westoslinux ~]# vim /etc/bashrc  ##删除
[root@westoslinux ~]# vim ~/.bashrc
[root@westoslinux ~]# unalias xie
[root@westoslinux ~]# xie
bash: xie: command not found...



[root@westoslinux ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@westoslinux ~]# vim ~/.bash_profile 
PATH=$PATH:$HOME/bin:/mnt

[root@westoslinux ~]# source ~/.bash_profile
[root@westoslinux ~]# chmod +x /mnt/test.sh
[root@westoslinux ~]# test.sh  ##切换用户失效
1

[root@westoslinux ~]# vim /etc/profile.d/path.sh  ##切换用户依然生效
[root@westoslinux ~]# cat /etc/profile.d/path.sh
#!/bin/bash
export PATH=$PATH:/mnt
[root@westoslinux ~]# source /etc/profile.d/path.sh
[root@westoslinux ~]# su - westos
Last login: Fri Nov 19 15:10:50 CST 2021 on pts/0
[westos@westoslinux ~]$ test.sh
1

五、用户环境变量的更改

设定方式:

~/.bash_profile
export PATH=$PATH:/mnt
/etc/bash_profile
export PATH=$PATH:/mnt

1、直接利用命令执行结果

$( )|
##优先执行
TEST=hostname TEST=$(hostname)

 

2、脚本中的传参

非交互模式:
$0 is /mnt/test.sh                        <!脚本本身>
$1 is westos                                <!脚本后所输入的第一串字符>
$2 is linux
$3 is redhat
$* is westos linux redhat        <!脚本后所输入的所有字符"westos linux redhat">
$@ is westos linux redhat        <!脚本后所输入的所有字符'westos' 'linux' 'redhat'>
$# is 3                                        <!脚本后所输入的字符串个数>

交互模式传参:

read WESTOS                ##对westos赋值
read p "please input word:" ##输出提示语
s                      ##隐藏输入内容

 

 六、脚本函数

定义:
程序的别名
设定方式:
WORD()
{
action1
action2
}
WORD 在脚本中就代表action1 action2这两个动作

# 练习脚本 #
sh create_user.sh
Please input username: westos
westos exist<output>> westos is exist>Please input username:
westos not existplease input password: 无回显密码
此用户会自动建立并且密码为提示后设定的密码
并显示:westos is created
并再次提示Please input username:
当Please input username:exit
此脚本退出

#!/bin/bash
ACTION()
{
        read -p "Please input username:" WORD
        [ "$WORD" = "exit" ]&&{
                echo bye
                exit
        }||{
        id "$WORD" &> /dev/null && {
        echo "$WORD is exist"

        }||{

        read -p "$WORD not exist.please input password:" -s PASSWD
        useradd $WORD
        echo $PASSWD | passwd --stdin $WORD &> /dev/null &&{
        echo "" 
        echo "$WORD is created"
                }
        }


        }
         ACTION
        }
ACTION

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值