浅谈bash shell 变量

一、简述

变量是指内存空间的命名,指向内存中一个或一组地址。bash shell中的变量属于弱类型变量,使用变量无需声明、不需要指定类型,默认为字符型变量。

二、变量命名、赋值、引用

(1)命名规则:

    1.只能使用字母,数字,下划线;并且不能使用数字开头。

    2.不能使用bash关键字,可用help命令查看,例如:if,for。

    3.见名知意,变量命名人性化,好的命名有助于提升脚步易读性。

    

(2)变量赋值,引用

变量使用等号来赋值;

引用变量时,在变量名前加$,形式有$var,${var},"$var"。像$var这种简便方式容易和上下文混淆变量名

1.当给变量名赋值字符串时,字符串当中不含空格可以不使用引号

~]$ name=tom                #等号两边不能有空格出现
~]$ title="What is Linux?"        #字符串中含有空格,使用引号括起
~]$ title='What is Linux?'

2.变量赋值引用变量时,只能使用弱引用"",使用强引用''不会引用变量值

[root@centos7 ~]$ path="/home/$USER"
[root@centos7 ~]$ echo $path
/home/root
[root@centos7 ~]$ path='/home/$USER'          #使用单引号只会原样赋值
[root@centos7 ~]$ echo $path
/home/$USER

3.变量接收命令结果,使用$()或者反引号` `

[root@centos7 ~]$ userid=$(id -u root)
[root@centos7 ~]$ echo $userid
0
[root@centos7 ~]$ username=`whoami`
[root@centos7 ~]$ echo $username
root

三、变量分类 

按作用范围可划分为:

  1. 局部变量

    生效范围是当前shell中某代码片段,通常指函数中的变量

  2. 本地变量

    生效范围是当前shell进程,及当前shell中(command1;command2;)形式的subshell

  3. 全局变量

    生效范围是当前shell进程,及其子进程

[root@centos7 ~]$ name=tom;echo $name          #定义本地变量name
tom
[root@centos7 ~]$ (echo $name)                 #()形式的subshell可以取得name变量值
tom
[root@centos7 ~]$ (name=bob;echo $name;)       
bob
[root@centos7 ~]$ echo $name                    #重新定义的变量不能传递出来
tom

全局变量  

全局变量的声明方式为:

export variable 或者 declare -x variable       

[root@centos7 ~]$ cat test.sh             #新建一个脚本,看本地变量name是否生效
#!/bin/bash

echo there is subshell
echo name is $name
[root@centos7 ~]$ ./test.sh                 
there is subshell
name is                            #name变量值没有传递进来
[root@centos7 ~]# export name       #将name声明为环境变量
[root@centos7 ~]# ./test.sh 
there is subshell
name is tom                        #name变量值此时生效

查看当前shell环境变量命令为

bash$ env;printenv;declare;

撤销变量命令

bash$ unset variable

定义的环境变量会随着shell退出而失效,可将export variable=value 写入~/.bashrc

四、位置变量,特殊变量

位置变量

$1,$2,$3…用于脚本中,获取通过命令行传递给它的参数

$# 记录命令行参数的个数

$*,$@ 表示所有的参数。当引用所用参数使用""引起时,二者是有区别的,$*将所有参数表示为一个整体,

而$@将所有参数独立表示

[root@centos7 ~]$ cat positionVar.sh      #测试位置参数脚本
#!/bin/bash

echo 1st args is $1
echo 2sec args is $2
echo 3rd args is $3
echo args number is $#
echo all args are $*
echo all args are $@
[root@centos7 ~]$ ./positionVar.sh dog cat fish    #传递三个参数:dog,cat,fish
1st args is dog
2sec args is cat
3rd args is fish
args number is 3
all args are dog cat fish
all args are dog cat fish

测试$*,$@的区别

[root@centos7 ~]# cat positionAll.sh   #新建脚本
#!/bin/bash

echo test '$*'------------
./positionVar.sh "$*"

echo test '$@'------------
./positionVar.sh "$@"
[root@centos7 ~]# ./positionAll.sh dog cat fish   #测试,传递三个参数
test $*------------
1st args is dog cat fish      # $*将多个参数整体传递进来
2sec args is
3rd args is
args number is 1
all args are dog cat fish
all args are dog cat fish
test $@------------
1st args is dog               # $@将多个参数单独传递进来
2sec args is cat
3rd args is fish
args number is 3
all args are dog cat fish
all args are dog cat fish

特殊变量

1.$0 表示执行命令本身

[root@centos7 ~]# ./test.sh 
the script is ./test.sh
[root@centos7 ~]# /root/test.sh 
the script is /root/test.sh

2.$? 表示命令执行状态码;脚本的状态码为最后一条命令的状态码

0 表示执行成功

1-255 表示执行失败

[root@centos7 ~]# ll -d /root
dr-xr-x---. 16 root root 4096 Aug 13 11:34 /root
[root@centos7 ~]# echo $?
0                            #成功
[root@centos7 ~]# ll -d /roots
ls: cannot access /roots: No such file or directory
[root@centos7 ~]# echo $?
2                            #失败
[root@centos7 ~]# lls -d /root
bash: lls: command not found...
Similar command is: 'ls'
[root@centos7 ~]# echo $?
127                           #失败

五、课后作业

1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。

#!/bin/bash
echo "hostname:`hostname`"
echo -e "IPv4:\n`ifconfig|sed -nr 's/inet (([0-9]{1,3}\.){3}[0-9]{1,3})(.*)/\1/p'`"
echo "OS:`cat /etc/centos-release`"
echo "Kernel:`uname -r`"
echo "CPU:`lscpu|sed -nr s/'Model name:[[:space:]]+\<//p'`"
echo "Memory:`free -m |grep Mem|tr -s ' '|cut -d' ' -f2`M"
echo "Disk:`lsblk|grep disk|tr -s ' '|cut -d' ' -f4`"

2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中

#!/bin/bash

echo "backup dir /etc"
cp -a /etc /root/etc`date +%F`
echo "backup finish `date +%F`"

3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

#!/bin/bash

echo "the max ratio of partiton used is:`df |grep /dev/sd|cut -c 45-46|sort -n|tail -1`%"

4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序

#!/bin/bash
echo "links is :`netstat -tn|grep tcp|tr -s ' '|cut -d: -f2|cut -d' ' -f2|sort|uniq -c|sort -nr`"

5、写一个脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和

6、写一个脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和

#!/bin/bash

num1=`grep '^$' $1 |wc -l`
num2=`grep '^$' $2 |wc -l`
echo "sumspace is :$[num1+num2]"

7、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件

#!/bin/bash

num1=`(ls $1;ls $2;ls $3)|wc -l`
echo "files total :$num1"

8、写一个脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数

#!/bin/bash
read -p "please input at least one args :" filepath
[[ -n $filepath ]]&&echo "blank lines number:`grep -c '^$' $filepath`"||echo "at least one args"

9、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”

#!/bin/bash
read -p "please input a IPv4:" ipaddr
ping -W1 -c1 $ipaddr &>/dev/null&&echo "the IP is reachble"||echo "the IP is unreachble"

10、判断硬盘的每个分区空间和inode的利用率是否大于80,如果是,发邮件通知root磁盘满

#!/bin/bash
diskmax=`df|grep /dev/sd|cut -c 45-46|sort -n|tail -1`
inodemax=`df -i|grep /dev/sd|cut -c 43-44|sort -n|tail -1`
[[ $diskmax -gt 80 || $inodemax -gt 80 ]]&&mail -s 'disk warning' root /etc/issue||echo "disk space is ok"

11、指定文件做为参数,判断文件是否为.sh后缀,如果是,添加x权限

#!/bin/bash
[ ! -f $1 ]&&echo no such file or not a common file&&exit
[[ $1 =~ .*\.sh$ ]]&&chmod +x $1&&echo chmod +x ok!||echo this is not a shellscript

12、判断输入的IP是否为合法IP

#!/bin/bash
read -p "input a IP:" ipaddr
echo $ipaddr |egrep -q '^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$' &&echo "IP is correct"||echo "IP is illegal"

13、计算1+2+3+…+100

$ seq s + 1 100

14、输入起始值A和最后值B,计算从A+(A+1)…+(B-1)+B的总和

#!/bin/bash

read -p "input a start number:" startnum && grep '^[0-9]+$' $startnum &>/dev/null||echo not a number&&exit
read -p "input a end number:" endnum &&grep '^[0-9]+$' $endnum ||(echo not a number&&exit)
[[ $startnum -ge $endnum ]]&&echo start number greater than end number,illegal||echo sum from start number to end number is:`seq -s + $startnum $endnum|bc`




原创文章,作者:cutemsyu,如若转载,请注明出处:http://www.178linux.com/34273

(0)
cutemsyucutemsyu
上一篇 2016-08-15 09:25
下一篇 2016-08-15 09:25

相关推荐

  • N25期–第十三周作业

    1、 建立samba共享,共享目录为/data,要求:(描述完整的过程)  1)共享名为shared,工作组为magedu;  2)添加组develop,添加用户gentoo,centos和ubuntu,其中gentoo和centos以develop为附加组,ubuntu不属于develop组;密码均为用户名;  3)添加sam…

    Linux干货 2017-04-05
  • M20 – 1- 第二周(2):课堂练习与作业

    课堂练习: 1、显示/var目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件和目录 [root@centos6 ~]# ls /var/l*[[:digit:]]*[[:lower:]] ls: cannot access /var/l*[[:digit:]]*[[:lower:…

    Linux干货 2016-08-02
  • Linux用户和组管理常用命令

    Linux用户和组管理常用命令 1、useradd:创建用户   useradd [options] LOGIN     -u UID: [UID_MIN, UID_MAX]指定uid,(默认500|1000开头)定义在/etc/login.defs     -o 配合-u 选项, 不检查…

    Linux干货 2017-04-04
  • N25第六周 vim,crontab命令的使用

    请详细总结vim编辑器的使用并完成以下练习题 vim是一款功能强大的文本编辑器,是程序员的必备神器。 vim工作模式分为三种:编辑模式,输入模式,末行模式,三种工作模式可以进行来自由切换     编辑模式—》输入模式: 直接键i      输入模式—》…

    Linux干货 2016-12-29
  • 第六周作业

    请详细总结vim编辑器的使用 模式化的编辑器 基本模式: 编辑模式,命令模式 输入模式 末行模式: 内置的命令行接口; 打开文件: # vim [options] [file ..] +#:打开文件后,直接让光标处于第#行的行首; +/PATTERN:打开文件后,直接让光标处于第一个被PATTERN匹配到的行的首; 模式转换: 编辑模式:默认模式 编辑模式 …

    Linux干货 2017-03-21
  • lamp+nfs搭建wordpress

    一、前言 lamp是大多上公司常用的架构,本文将针对分离式的lamp+nfs来搭建一个简单的wordpress网站。 二、架构图 三、基本实现过程 3.1:dns搭建 由于这这是一个实验,故使用yum搭建dns服务器 yum install -y bind 配置如下 dns主配置文件 dns辅助配置文件 zone文件 对所有服务…

    Linux干货 2015-10-16

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-15 10:01

    总结的很好,并且通过实验验证了自己的想法,这对自己理解变量来说,是一个更好的理解方式。