shell中的变量

变量,即在程序运行过程中它的值是允许改变的量 
变量是用一串固定的字符来标示不固定的值的一种方法 
变量是一种使用方便的占位符,用于引用计算机内存地址,该地址可以存储 Script 运行时可更改的程序信息 在 shell 
中变量是不能永久保存在系统中的,必须在文件中声明

shell 脚本中变量的定义方法

环境级变量
export A=1

作用域为当前 shell 进程及其子进程,不能影响到其父进程; export varname=value 
“导出”,如果变量已经定义可以只是用变量名 export varname,即 
1. export varname=value 
2. varname=value export varname 脚本在执行命令时会启动一个子 shell 环境变量: 系统自动执行的脚本(非命令行启动)就需要自我定义需要的个环境变量;

用户级变量
vim ~/.bash_profile
export A=1
source ~/.bash_profile 重新加载

系统级变量
vim /etc/profile
export A=1
source /etc/profile 重新加载

变量命名规范

只能含字母、数字和下划线,并且以字母和下划线开头

最好不要跟系统已有的环境变量重名

便于辨认(规范性)

字符转义与变量声明

\        转译单个字符
""       弱引用,批量转译""中出现的字符
''       强引用,批量转译''中出现的字符
''""   两者的区别在于,""不能转译"\","`","!","$"
${}      变量声明
例如:
    A=1
    echo $Ab
    echo ${A}b

变量值传递
$1 脚本后的第一个字符

$2 脚本后的第二个字符

$3 脚本后的第三个字符

$# 脚本后所跟字符串的个数

$* 脚本后跟的所有字符串,模式为 “1 2 3”(一串字符)

$@ 脚本后跟的所有字符串,模式为 “1” “2” “3”(三串字符)

[root@desktop mnt]# vim test.sh
[root@desktop mnt]# cat test.sh 
#!/bin/bash
echo \$0 is $0
echo \$1 is $1
echo \$2 is $2
echo \$3 is $3
echo \$# is $#
echo \$* is $*
echo \$@ is $@
[root@desktop mnt]# sh test.sh
$0 is test.sh
$1 is
$2 is
$3 is
$# is 0
$* is
$@ is
[root@desktop mnt]# sh test.sh hello hai 
$0 is test.sh
$1 is hello
$2 is hai
$3 is
$# is 2
$* is hello hai
$@ is hello hai
[root@desktop mnt]# sh test.sh hello hai nihao
$0 is test.sh
$1 is hello
$2 is hai
$3 is nihao
$# is 3
$* is hello hai nihao
$@ is hello hai nihao

123 ∗ 脚 本 后 跟 的 所 有 字 符 串 , 模 式 为 “ 1 2 3 ” ( 一 串 字 符 ) @ 脚本后跟的所有字符串,模式为 “1” “2” “3”(三串字符)
∗ 、 @之间的比较

[root@desktop mnt]# vim *yu#.sh
[root@desktop mnt]# cat \*yu#.sh 
#!/bin/bash
for name in "$*"
do
    echo $name
done
[root@desktop mnt]# sh -x \*yu#.sh hello hai nihao
+ for name in '"$*"'
+ echo hello hai nihao
hello hai nihao
[root@desktop mnt]# vim *yu#.sh
[root@desktop mnt]# cat \*yu#.sh 
#!/bin/bash
for name in "$@"
do
    echo $name
done
[root@desktop mnt]# sh -x \*yu#.sh hello hai nihao
+ for name in '"$@"'
+ echo hello
hello
+ for name in '"$@"'
+ echo hai
hai
+ for name in '"$@"'
+ echo nihao
nihao

用 read 实现变量传递
read
read -s 加密交互
read -p “input:” 显示提示语言

[root@desktop mnt]# vim read.sh
[root@desktop mnt]# cat read.sh
#!/bin/bash
read -p "Please input a number: " WESTOS
echo $WESTOS
[root@desktop mnt]# sh read.sh 
Please input a number: qwertyukiop
qwertyukiop
[root@desktop mnt]# vim read.sh
[root@desktop mnt]# cat read.sh 
#!/bin/bash
read -p "Please input a IP: " IP
ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down
[root@desktop mnt]# sh read.sh 
Please input a IP: 172.25.254.36
172.25.254.36 is up
[root@desktop mnt]# sh read.sh 
Please input a IP: 172.25.254.253 
172.25.254.253 is down
#!/bin/bash
read -p "Please input a IP: "  -s IP          加-s表示加密 即输入不显示
ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down

linux 系统中命令别名的设定——alias 临时设定

[root@desktop ~]# alias xie='vim'  ##临时设定,重新开启shell后,就会失效
[root@desktop ~]# xie  ##可以进入vim编辑模式
[root@desktop ~]# alias 
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
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@desktop ~]# logout    ##退出当前shell
Connection to 172.25.254.136 closed.
[kiosk@desktop50 Desktop]$ ssh root@172.25.254.136  ##重新开启新shell
root@172.25.254.136's password: 
Last login: Sun Jun 19 21:25:00 2018 from foundation35.ilt.example.com
[root@desktop ~]# alias    ##vim 的别名 xie 已经不存在
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
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'
[root@desktop ~]# xie
bash: xie: command not found...

永久设定

[root@desktop ~]# vim .bashrc    ##超级用户家目录下的文件,只对超级用户生效
[root@desktop ~]# cat .bashrc | head -n 8 | tail -n 1
alias xie='vim'
[root@desktop ~]# source .bashrc
[root@desktop ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
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@desktop ~]# su - student  ##切换用户student
Last login: Thu May 11 20:23:54 EDT 2017 on pts/0
[student@desktop ~]$ xie    ##超级用户下的文件无法对其他用户生效
bash: xie: command not found...
[student@desktop ~]$ logout 
[root@desktop ~]# vim /etc/bashrc   ##系统永久设定,对所有用户生效
[root@desktop ~]# cat /etc/bashrc | tail -n 1
alias xie='vim'
[root@desktop ~]# source /etc/bashrc 
[root@desktop ~]# su - student
Last login: Sun Jun 19 22:42:08 EDT 2018 on pts/1
[student@desktop ~]$ xie
[student@desktop ~]$ 
[student@desktop ~]$ logout 

命令别名的删除

[root@localhost ~]# unalias xie   
[root@localhost ~]# alias 
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
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'
[root@localhost ~]# vim /etc/bashrc   ##删除所添加的内容
[root@localhost ~]# vim .bashrc       ##删除所添加的内容
[root@localhost ~]# source /etc/bashrc 
[root@localhost ~]# source .bashrc
[root@localhost ~]# xie
bash: xie: command not found...
[root@localhost ~]#

exit 退出值

用途说明 
exit 命令用于退出当前 shell,在 shell 脚本中可以终止当前脚本执行。 
常用参数 
格式:exit n 
退出。设置退出码为 n。(Cause the shell to exit with a status of n.) 
格式:exit 
退出。退出码不变,即为最后一个命令的退出码。(If n is omitted, the exit status is that > of the last command executed. ) 
格式:$? 
上一个命令的退出码。 
格式:trap “commands” EXIT 
退出时执行 commands 指定的命令。( A trap on EXIT is executed before the shell terminates.) 
退出码(exit status,或 exit code)的约定: 
0 表示成功(Zero - Success) 
非 0 表示失败(Non-Zero - Failure) 
2 表示用法不当(Incorrect Usage) 
127 表示命令没有找到(Command Not Found) 
126 表示不是可执行的(Not an executable) 
128<= 信号产生
[root@desktop mnt]# vim ip.sh
[root@desktop mnt]# sh ip.sh 
Please input a ipaddress: 172.25.254.24
172.25.254.24 is down
Please input a ipaddress: exit
bye
[root@desktop mnt]# cat ip.sh 
#!/bin/bash
PING()
{
    read -p "Please input 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值