shell位置变量解析

什么是位置变量

在脚本代码中调用通过命令行传递给脚本的参数。

有哪些位置变量

$1,$2,...: 对应第1、第2等参数,shift [n]换位置
$0:命令本身
$*:传递给脚本的所有参数,全部参数合为一个字符串
$@:传递给脚本的所有参数,每个参数为独立字符串$#:传递给脚本的参数的个数   
     $@ $* 只在被双引号包起来的时候才会有差异

位置变量示例脚本

1.$n 获取当前执行脚本的第n个参数,n=1..9,$0,为当前脚本名。如果n大于9,使用${10}
echo'echo '$(seq-s " $"1 5|sed's/1/$1/') > test_n.sh
 cattest_n.sh
 #内容如下#echo $1 $2 $3 $4 $5
 bashtest_n.sh arg1 agr2 arg3
 #输出内容:#arg1 agr2 arg3
2.$0获取当前脚本的文件名
#脚本名称如下vim test1.sh 
#脚本内容如下#!/bin/bashecho $0执行后输出如下:test]# bash test1.shtest1.sh
3.$#传递给脚本的参数的个数
脚本内容如下:#!/bin/bashecho $#执行结果如下:
~]$ bash test3.sh a b c 1 25
4.$@传递给脚本的所有参数,每个参数为独立字符串;$*:传递给脚本的所有参数,全部参数合为一个字符串    $@ $* 只在被双引号包起来的时候才会有差异
#脚本test2.sh中的内容#!/bin/bashecho 'This is a test for $* and $@'echo 'The following start printing $*'echo $*
./test4.sh $*echo 'The following start printing $@'echo $@./test4.sh $*echo 'The following start printing $# with ""'echo "$*"./test4.sh "$*"echo 'The following start printing $@ with ""'echo "$@"./test4.sh  "$@"#脚本test4.sh中的内容#!/bin/bashecho $#执行结如下:
[ci@CentOS6 ~]$ ./test2.sh a b c e
This is a test for $* and $@The following start printing $*
a b c e4The following start printing $@a b c e4The following start printing $# with ""a b c e1The following start printing $@ with ""a b c e4

shell脚本习题:

1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。
#!/bin/bashIP=`ifconfig | grep "Bcast"|tr -s ' ' |cut -d ' ' -f 3|cut -d: -f 2`
OsVersion=`cat /proc/cpuinfo |grep "model name"|head -1|cut -d: -f2`
CpuType=`uname -r`
MemSize=`free|sed -n '2p'|tr -s ' '|cut -d' ' -f 2`
DiskSize=`fdisk -l |sed -n '2p'|cut -d ' ' -f 3,4`echo "The system hostname is `hostname`"echo "The system ip is $IP"echo "The system osversion is $OsVersion"echo "The cpu type is $CpuType"echo "The memory total is $MemSize"echo "The disk total is $DiskSize"
2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
#!/bin/bashDir=/etc/
BackDir=/root/

Time=`date +%F`echo "Backup is start,Please wait the alert of end"Backup=`cp -a $Dir ${BackDir}/etc${Time}`echo "All is finished"
3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bashMaxUse=`df -h|sed -nr 's@.*(\b[[:digit:]]+)%.*@\1@p'|sort|tail -1`echo "The max use of disk is $MaxUse"
4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
#!/bin/bashecho "The link is `netstat -nt|tr -s ' '|cut -d' ' -f 5|sed '1,2d'|cut -d: -f1|sort |uniq -c|sort -nr`"
5、写一个脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和
#!/bin/bashID10=`cat /etc/passwd|sed -n '10p'|cut -d: -f 3`
ID20=`cat /etc/passwd|sed -n '20p'|cut -d: -f 3`
SumId=$((ID10+ID20))echo "Sum of the two uid is $SumId "
6、写一个脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
#!/bin/bashSpaceLine1=`cat $1|grep "^$"|wc -l`
SpaceLine2=`cat $2 |grep "^$"|wc -l`let SumSpace=${SpaceLine1}+${SpaceLine2}echo "Spcace line of the two file is $SumSpace"
7、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件
#!/bin/bashSum=$((`ls /etc/|wc -l`  + `ls /var/|wc -l` + `ls /usr/|wc -l`))echo "Sum file of the three dir is $Sum "
8、写一个脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
#!/bin/bash[ $# -lt 1 ] && echo "Please give at least one args" || echo "`cat $1 |grep '^$' |wc -l` is the first args's space line"
9、chmod -rw /tmp/file1,编写脚本/root/bin/per.sh,判断当前用户对/tmp/fiile1文件是否不可读且不可写
#!/bin/bashFile1=/tmp/file1
[ ! -r ${File1} -a ! -w ${File1} ] && echo "Current user can't read and write the file" ||echo "Current user can read and write the file"
10、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统。
#/root/bin/nologin.sh#!/bin/bash[ -e /etc/nologin ] && echo "The common user is already can't login" && exittouch /etc/nologin && echo "Limit the common user ok"#login.sh#!/bin/bash[ ! -e /etc/nologin ] && echo "The common user is already can login" && exitrm -rf /etc/nologin && echo "The limit of common have remove"
11、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,先判断是否合格IP,否,提示IP格式不合法并退出,是,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
#!/bin/bashIpAddr=`echo "$1"|egrep -o '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'`
[ ! -n IpAddr  ] && echo "Please give a legal ip address"&&exitping -c1 -W1 $1 &> /dev/null && echo "The ip is access" ||echo "Cant't access the ip address"
12、计算1+2+3+...+100的值
#!/bin/bashsum=$((`echo {1..100}|tr ' ' '+'`))echo $sum
13、计算从脚本第一参数A开始,到第二个参数B的所有数字的总和,判断B是否大于A,否提示错误并退出,是则计算之
#!/bin/bashF=$[$1-$2]
[ $F -gt 0 ] && exitsum=$((`seq $1 $2 |tr '\n' ' '|tr ' ' '+'|sed -r 's@(.*[^+])\+@\1@'`))echo $sum

原创文章,作者:提着酱油瓶打醋,如若转载,请注明出处:http://www.178linux.com/35432

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

相关推荐

  • 日志管理

    日志管理学习笔记

    2018-02-08
  • 用户 组 和权限 以及权限的 分类

    关于文件的安全我们要从3A验证 说起     1  Authentication:认证      2  Authorization:授权      3  Accouting|Audition:审计 由于系统文件的用户太多为了便于管理 我们便把系统的用…

    系统运维 2016-08-04
  • CentOS6 编译安装httpd-2.4

       CentOS7的镜像中已经提供了httpd-2.4的rpm包,httpd-2.4相对于httpd2.2有较大的改进,在CentOS6下只能通过Apache官方网站提供的源码包编译安装。    有时候,我们需要应用http-2.4的某些特性,但是因为当前的发行版没有支持,所以我们需要自己编译安装。 httpd-2….

    Linux干货 2016-07-27
  • 磁盘管理和文件系统

    磁盘结构 设备文件:关联至一个设备驱动程序,进而能够跟与之对应硬件设备进行通信 一切皆文件:open(), read(), write(), close() 设备类型: 块设备:block,存取单位“块”,磁盘 字符设备:char,存取单位“字符”,键盘 设备号码: 主设备号:major number, 标识设备类型 次设备号:minor number, 标…

    Linux干货 2017-04-23
  • 马哥教育网络班21期+第8周课程练习

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别 集线器(HUB)是在OSI模型的第一层——物理层——连接多台主机、延长网络的设备。其主要功能是将从一个端口接收到的数据包转发给所有端口。中继器(Repeater)是将由电缆传过来的电信号或光信号调整波形和放大再传给另一个电缆的设备,提供多端口服务的中继器称作集线器。二者的主要区别是…

    Linux干货 2016-08-29
  • Linux基础学习总结(一)

    一、计算机的组成与功能 二、Linux的版本 三、Linux的哲学思想 四、Linux基础命令(一) 五、Linux帮助信息 六、Linux目录结构

    Linux干货 2016-09-20

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-16 17:01

    完成的不错,以后希望能尽量多写一些,注重实战,但是理论也不可简略哦。