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

相关推荐

  • history          每打开一个终端都会从文件(.bash_history)中加载历史记录           不同终端历史记录不同彼此终端不共享 …

    2017-07-15
  • linux常用网络配置

    网卡名称:  网络接口识别并命名相关的udev配置文件:    /etc/udev/rules.d/70-persistent-net.rules  卸载网卡驱动:    modprobe  -r e1000  装载网卡驱动:    modprobe &nbs…

    Linux干货 2016-09-06
  • python面向对象学习第一周

     面向对象的思想 一个具体对象的属性方法,都有各个来源,来源于类,比如消化类,有各种各样的消化模式,人类的只是其中一种 另一种思想方法,类有各种属性方法,人类有很多的属性,一个人应该包括其中的属性,只是值上可能有不同,不同人群也有他们的特征属性和方法     类是对象的抽象,但是类本身也是对象, 对象是一个类的实例. 类的属性:类变量,对象方法,类方法,静态…

    Linux干货 2017-11-13
  • VimTutor(上)

    柚子翻译,如有理解错误或翻译错误,请指正! VimTutor Vim是一款强大的文本编辑器,拥有多命令的特性,tutor解释了其中大多数命令。 tutor是为了描述足够多的Vim命令帮助你轻松使用Vim而被制作的,使Vim能成为一个可完成你所有工作意图的编辑器。 完成tutor的内容大概需要25-30分钟,所完成时间取决于你通过时间积累的经验。 注意: 1.…

    Linux干货 2015-10-18
  • 软链接和硬链接的区别

          什么是链接文件? 使用windows的朋友们应该会经常接触到快捷方式吧!它也就是能让我们快速的打开一个目标应用程序,文件,或者文件夹来使我们的操作更加快捷。那么下面我们就来简单聊聊linux上的“链接文件”吧!            &n…

    Linux干货 2016-10-19
  • N26 第四周作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost ~]# cp -rv /etc/skel /home/tuser1 [root@localhost ~]# chmod&n…

    Linux干货 2017-01-23

评论列表(1条)

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

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