浅谈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
下一篇 2016-08-15

相关推荐

  • centos7编译安装zabbix

    centos7源码编译安装zabbix3 centos7源码编译安装zabbix3 centos7源码编译安装zabbix3 系统配置和必要的安装包 编译安装nginx 编译安装php 安装MariaDB 安装JDK 安装Zabbix3.0 前戏准备 开始编译 开始安装 zabbix虚拟主机创建 前戏准备 开始编译 开始安装 php配置 前戏准备 开始编译 …

    Linux干货 2016-11-25
  • 第二十二周作业

    1、请描述本地文件系统和分布式文件系统的特点 本地文件系统 本地文件系统主要是指Ext2,Ext3,Btrfs,XFS这类,它们通常提供以下功能: 扩展性:随着系统容量的增加保持性能,不随容量变化而导致性能震荡。比如一个目录下的海量文件,在EXT2/3中由于目录设计问题会导致较大的性能问题。再比如EXT2/3中的Metadata的占用和inode的划分可能会…

    2017-08-06
  • LVS模型练习

    本次涉及到的练习:nat练习、dr练习、FWM机制练习、mysql集群练习、httpd的集群练习、httpd和mysql结合调度的练习 yum install ipvsadm nat练习     设置:     (1)centos7.2作为lvs调度,有两块网卡,(公网地址)其中…

    Linux干货 2016-10-30
  • 文本处理工具之AWK

    概述:     在之前的文章中,我们介绍过文本处理三剑客的grep、sed,本篇就简要说明下awk的用法。主要是围绕常见的一些用法进行展开,分为以下几个部分:     1、awk的基础语法     2、awk的进阶语法  &nbs…

    Linux干货 2016-09-20
  • 马哥教育网络20期+第7周练习博客

    1、创建一个10G分区,并格式为ext4文件系统;    (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;    (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; [root@7b ~]#&nbsp…

    Linux干货 2016-07-24
  • 第九周shell脚本编程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; 2、写一个脚本 (1) 获取当前主机的主机名,保存于hostname变量中; (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改www.magedu.com; (3…

    2017-11-23

评论列表(1条)

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

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