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

相关推荐

  • RPM及YUM工具介绍及使用(下)

    YUM工具 YUM工作原理 YUM工具的出现就是为了解决rpm工具不能够自动解决软件包之间的依赖关系这一难题的。它的工作原理大致如下:YUM透过分析RPM的标头资料,根据各软件的相关性找出软件所依赖的软件列表,然后去下载速度最快的yum仓库中下载所有相关RPM软件包,然后完成相应软件的安装。同时yum工具和rpm工具一样,也可以提供对软件包的查询,安装、升级…

    Linux干货 2016-12-26
  • N25 第一周作业 2016/12/5

       1.描述计算机的组成及其功能         计算机组成由:CPU 内存 IO设备           功能:                cpu就是中…

    Linux干货 2016-12-05
  • 马哥教育网络班22期+第16周课程练习

    1、源码编译安装LNMP架构环境; 安装开发包组 yum groupinstall "Development Tools" "Server Platform Development" -y yum install -y …

    Linux干货 2017-01-03
  • 马哥教育网络班22期第1周课程作业

    一、基础中的基础————–计算机的组成部分         计算机组成分为硬件系统与软件系统         硬件包括五个部分     1.控制器:控制器 I/O 存储器 &nbs…

    Linux干货 2016-08-15
  • SHELL流程控制之循环

    当进行脚本编程时,语句执行的流程控制通常有三种: l  顺序执行 l  选择执行 l  循环执行   条件选择if: if语句可以进行嵌套 if 判断条件;then          条件为真的分支代码 elif  判断条件;th…

    Linux干货 2016-08-18
  • linux-系统启动和内核管理

    一、Linux 组成及启动过程 Linux: kernel+rootfs    kernel:进程管理、内存管理、网络管理、驱动程序、文件系统、安全功能      IPC:Inter Process Communication  &nbsp…

    Linux干货 2016-09-26

评论列表(1条)

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

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