Shell编程之位置变量

       linux中命令可以接受参数,同样的,shell脚本也可以接受参数。这些参数用$1、$2、$3…$n表示。

      $0  表示脚本本身

      $1  传递给脚本的第1个参数

$2  传递给脚本的第2个参数

$3  传递给脚本的第3个参数

      $n  传递给脚本的第n个参数

$#     表示传递的参数个数用

      $*     传递给脚本的全部参数,所有参数合为一个字符串

      $@   传递给脚本的全部参数,每个参数是一个独立的字符串

               $@  $* 只在被双引号包起来的时候才会有差异

示例:不同位置变量的引用方法。

blob.png

1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。

#!/bin/bash
#author:zmingbo
#
system_release=`cat /etc/redhat-release`
ip_addr=`ifconfig  | sed -n -r 's@.*addr:(.*) Bcast.*$@\1@p'`
kernel_version=`uname -r`
cpuinfo=`cat /proc/cpuinfo | sed -r -n 's@model name.*:(.*)$@\1@p'`
memsize=`free -mh | grep Mem |tr -s " " | cut -d " " -f 2`
disksize=`fdisk -l| sed -n "/\/dev\/sd[a-z]:/p"|cut -d : -f 2 | 
            cut -d " " -f 2 | tr -s "\n" ":" | 
            sed  's@:\$@@'| sed  's@:@+@'` 
total_disksize=`echo $disksize |bc`

echo "The hostname is `hostname` "
echo "The release is $system_release "
echo "The kernel version is $kernel_version"
echo "The Disksize is $total_disksize G"
echo "The cpu is $cpuinfo"
echo "The mem size is $memsize"
for i in $ip_addr;do
	echo "IPv4 adress is $i "
done

blob.png

2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中

#!/bin/bash
#
cp -a /etc/.  /root/etc`date +%Y-%m-%d`

blob.png

3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

#!/bin/bash
use=`df -Th | sed -n '/\dev\/sd[a-z]\+[0-9]\+/p' | 
tr -s " " | cut -d " " -f 6 | sort -nr | head -n 1|tr -d "%"`
echo "The bigest utilization is $use"
if [ $use -gt 80 ];then
	wall "Disk will be full"
else
	echo "work well"
fi

blob.png

4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序

#!/bin/bash
#
netstat -nt | sed -n '/^[\(tcp\)]/p' |tr -s " " |cut -d " " -f 5 |
cut -d : -f 1 | sort -n | uniq -c | sort -nr

blob.png

5、写一个脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和

#!/bin/bash
#
user10id=`cat  /etc/passwd | head | tail -n1 | cut -d : -f 3`
user20id=`cat  /etc/passwd | head -n20 | tail -n1 | cut -d : -f 3`
idsum=$[$user10id+$user20id]
echo "The id sum is $idsum"

 blob.png

6、写一个脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和 

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo "Two files needed"
	exit 222
fi

flines=`cat $1 |grep -E "^$"| wc -l`
slines=`cat $2 |grep -E "^$"| wc -l`

let sumlines=$flines+$slines
echo "Total empty lines  is $sumlines"

blob.png

7、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件

#!/bin/bash
#
etc_dir_num=`ls -Al /etc|grep "^d.*" |wc -l`
var_dir_num=`ls -Al /var|grep "^d.*" |wc -l`
usr_dir_num=`ls -Al /usr|grep "^d.*" |wc -l`
total_dir_num=$[$etc_dir_num+$var_dir_num+$usr_dir_num]

etc_file_num=`ls -Al /etc | grep "^-.*" | wc -l`
var_file_num=`ls -Al /var | grep "^-.*" | wc -l`
usr_file_num=`ls -Al /usr | grep "^-.*" | wc -l`
total_file_num=$[$etc_file_num+$var_file_num+$usr_file_num]

etc_total_num=`ls -Al /etc |  wc -l`
var_total_num=`ls -Al /var |  wc -l`
usr_total_num=`ls -Al /usr |  wc -l`
total_num=$[$etc_total_num+$var_total_num+$usr_total_num]

echo "/etc & /var &/usr total  directory is $total_dir_num"
echo "/etc & /var &/usr total  file is $total_file_num"
echo "/etc & /var &/usr total  files and directory is $total_num"

blob.png 

8、写一个脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数 

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo " 1 asrg expected"
	exit 222
else
	sum=`cat $1 | grep "^$" |wc -l`
	echo "$1 has $sum empty lines "
fi

blob.png

9、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问” 

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo -e "\033[31mOne ipaddr is expected\033[0m"
	exit 22
fi

if echo "$1" |grep -E -o "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])" >/dev/null;
then
	ping -c1 -W1 $1 &> /dev/null && echo "$1 is reachable" || echo "$1 is unreachable"
else
	echo -e "\033[31mPlease enter correct ip adress\033[0m"
	exit 22
fi

blob.png

10、chmod -rw /tmp/file1,编写脚本/root/bin/per.sh,判断当前用户对/tmp/fiile1文件是否不可读且不可写 

#!/bin/bash
#
file=/home/hadoop/test.txt
if [ -r $file ] && [ -w $file ];then
	echo "I can read & write $file"
else
	echo "I can't read or write $file"
fi

blob.png

11、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统。

1、nologin.sh
#!/bin/bash
#
file=/etc/nologin
if [ -e $file ];then
	echo "Common user can't login system already"
else
	touch $file
	echo "Common user can't login system now"
fi
2、login.sh
#!/bin/bash
#
file=/etc/nologin
if [ -e $file ];then
	rm -fr /$file > /dev/null
	echo "Common user could login system now!"
else
	echo "Common user could login system already"
fi

 

blob.png

12、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,先判断是否合格IP,否,提示IP格式不合法并退出,是,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo -e "\033[31mOne ipaddr is expected\033[0m"
	exit 22
fi

if echo "$1" |grep -E -o "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])" >/dev/null;
then
	ping -c1 -W1 $1 &> /dev/null && echo "$1 is reachable" || echo "$1 is unreachable"
else
	echo -e "\033[31mPlease enter correct ip adress\033[0m"
	exit 22
fi

 

blob.png

13、计算1+2+3+…+100的值

#!/bin/bash
#
num=1
sum=0
while [ $num -le 100 ];do
	sum=$[$num+$sum]
	num=$[$num+1]
done
echo $sum

 

blob.png

14、计算从脚本第一参数A开始,到第二个参数B的所有数字的总和,判断B是否大于A,否提示错误并退出,是则计算之

#!/bin/bash
#
sum=0
if [ $# -lt 2 ];then
	echo "Two int number needed"
	exit 222
elif [ $1 -eq 0 ] &> /dev/null || [ $1 -lt 0 ] &> /dev/null;then
	echo "Please enter two int number,and the second arg must bigger than the first arg"
	exit 22
elif [ $1 -gt 0 ] &> /dev/null && [ $2 -gt 0 ]  &>/dev/null && [ $2 -gt $1 ] &> /dev/null;then
	for  ((i=$1;i<=$2;i++));do
		sum=$[$sum+$i]
	done
	echo $sum
else
	echo "Please enter two int number,and the second arg must bigger than the first arg"
	exit 22
fi

blob.png

原创文章,作者:M20-1钟明波,如若转载,请注明出处:http://www.178linux.com/33575

(0)
M20-1钟明波M20-1钟明波
上一篇 2016-08-12
下一篇 2016-08-12

相关推荐

  • 权限命令总结

    修改文件权限:chmod 1 2 3 4 [root@centos7 ~]# man chmod        chmod [OPTION]… MODE[,MODE]… FILE…    …

    Linux干货 2016-08-04
  • Shell脚本编程基础中() (()) [ ] [[ ]] 的使用

    Shell脚本编程基础中() (()) [ ] [[ ]] 的使用 () 生成子进程,括号内的命令将会在子进程中运行,父进程不能够读取在子进程中创建的变量 例: 新建个脚本文件,写入 则执行结果为 $()相当于 ` `  ,返回括号内命令执行结果 (( )) 用作四则运算和逻辑运算,并且支持多个表达式 例: 当 (( )) 加$,则是将获得表达式值,赋值给左…

    2017-11-26
  • Nginx基础整理

    目录结构如下: Nginx基础知识Nginx HTTP服务器的特色及优点 Nginx的主要企业功能 Nginx作为web服务器的主要应用场景包括: Nginx的安装安装环境 快速安装命令集合 各个命令解释 脚本 注意 安装故障总结故障一:没有安装pcre或pcre-devel 故障二:没有安装openssl和openssl-devel 常用的Nginx ht…

    Linux干货 2017-08-29
  • 权限管理

    权限 权限就是用户或者组对文件或者目录所拥有的能力,所能执行的操作。 权限的分配: 通过ls -l file这个命令可以查看文件或者目录的详细信息:     [root@localhost home]# ls -l /root/   &nb…

    Linux干货 2016-08-05
  • 谷歌三大核心技术(一)The Google File System中文版

    摘要 我们设计并实现了Google GFS文件系统,一个面向大规模数据密集型应用的、可伸缩的分布式文件系统。GFS虽然运行在廉价的普遍硬件设备上,但是它依然了提供灾难冗余的能力,为大量客户机提供了高性能的服务。 虽然GFS的设计目标与许多传统的分布式文件系统有很多相同之处,但是,我们的设计还是以我们对自己的应用的负载情况和技术环境的分析为基础的,不管现在还是…

    Linux干货 2015-04-13
  • N21天天第十二周课程练习

    1、请描述一次完整的http请求处理过程; 1、建立TCP连接 2、Web浏览器向Web服务器发送请求 3、Web浏览器发送请求头信息   建立连接后,客户机发送一个请求给服务器,请求方式的格式为:统一资源标识符(URL)、协议版本号,后边是MIME   信息包括请求修饰符、客户机信息和可能的内容 4、Web服务器应答…

    Linux干货 2016-10-31

评论列表(2条)

  • 马哥教育
    马哥教育 2016-08-16 15:52

    作业完成的很好,总结这里需要用点心哦,慢慢来,可以先模仿别人的写,尝试着写出优秀的博客。加油!!!

    • M20-1钟明波
      M20-1钟明波 2016-08-16 20:19

      @马哥教育好的,以后会加强知识点的总结。