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

相关推荐

  • yum –rpm前端工具详解

     1、yum 介绍        YUM: Yellowdog Update Modifier, rpm的前端程序,用来解决软件包相关依赖性,可以在多个库之间定位软件包,up2date的替代工具        &nbsp…

    Linux干货 2016-08-24
  • bash脚本编程实例

    bash脚本编程实例 1.写一个脚本 接受一个以上文件路径作为参数 显示每个文件拥有的行数 总结说明本次共为几个文件统计了其行数 #!/bin/bash # read -p “please input some paths:” paths if [ -z $paths ];then echo “There are not any paths inputtin…

    Linux干货 2017-08-14
  • N25-Week9

    1. 写一个脚本,判断当前系统上所有用户的shell是否为可登陆shell(即用户的shell不是/sbin/nologin);分别统计这两类用户的个数,通过字符串比较来实现 #! /bin/bash#写一个脚本,判断当前系统上所有用户的shell是否为可登陆shell(即用户的shell不是/sbin/nologin);#分别统计这两类用户的个数#通过字符…

    Linux干货 2017-03-10
  • ☞实时数据同步方案{ rsync; inotify; }

    实时数据同步方案{ rsync; inotify; } rsync 传输数据 安装 rsyncd 服务 CentOS 6 CentOS 7 配置 rsyncd 服务 配置 rsync 手动同步示例     启动 rsyncd 服务 CentOS 6 CentOS 7 rsync 客户端命令 rsync 数据传输功能 基…

    Linux干货 2016-10-29
  • LVS集群类型

     lvs:Linux Virtual Server         l4:四层路由、四层交换          根据请求报文的目标IP和目标PORT将其调度转发至后端的某主机;      IPTABLES:  …

    Linux干货 2017-01-10
  • inode概念总结

    一、inode  inode是类UNIX系统上文件系统中的一种数据结构。数据在磁盘上存储是按扇区存放的,几个扇区组成一个块(block),操作系统为高效读取磁盘上的数据,按block来读取。而数据存放的block的地址就包含在inode里。 二、inode的存储信息  每个inode保存一个文件系统对象(目录,文件,管道,软连接,sock…

    Linux干货 2016-08-05

评论列表(1条)

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

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