您的位置:首页 > 运维架构 > Shell

shell位置变量详解

2016-08-14 18:21 288 查看

什么是位置变量

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

有哪些位置变量

$1,$2,...: 对应第1、第2等参数,shift
换位置$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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  脚本 shell 位置变量