变量类型:
不同的数据类型在系统中所占资源不同,并且表示的范围也不同
数值型:
短整型short:
占2个字节:-32768~32767
整型int:
占4个字节:-2147483648~-2147483647
长整型long:
占用4个字节(32位)
占用8个字节(64位)
单精度浮点型float:
占4个字节,精度低,有效位7位
双精度浮点型double:
占8个字节,精度高,有效位16位
字符型:
char:
占1个字节
string:
字符串类型,存储的不再是单一的字符,而是字符串
位置变量
$0:脚本文件路径自身
$1:跟随在脚本名后面的第一个参数
$2:跟随在脚本名后面的第二个参数
$#:脚本参数的个数
$@:所有脚本参数,全部参数每个都为独立字符串
$*:所有脚本参数,全部参数合为一个字符串
[root@www bin]# cat test.sh #!/bin/bash #description understanding $0 $1 $2 $# $@ $* #version 0.1 #author gm #date 20160811 # #output $0 echo "this script name is $0" #output $1 echo "this script first arg is $1" #output $2 echo "this script second is $2" #output $# echo "this script all arg number is $#" #output $@ echo "this script all arg is: $@" #output $* echo "this script all arg is: $*" [root@www bin]# ./test.sh root gao this script name is ./test.sh this script first arg is root this script second is gao this script all arg number is 2 this script all arg is: root gao this script all arg is: root gao
脚本练习:
1、编写脚本/root/bin/systeminfo.sh,示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。
#!/bin/bash #descripation #version 0.1 #author gaomeng #date 20160810 #显示hostname echo "this host name is :$(hostname)" #显示系统ip地址 ip=`ifconfig | sed -n '2p' |sed 's@.*addr:\(.*\)B.*@\1@'` echo "ip address is : $ip" unset ip #显示系统版本 echo "this system is :$(cat /etc/centos-release)" #显示内核版本 echo "this kernel is :$(uname -r)" #显示cpu型号 echo "this cpu is :$(lscpu | grep name | sed 's@.*[[:space:]]\+@@')" #显示内存大小 echo "this free is : $(free -m | sed -n 2p | tr -s ' ' | cut -d' ' -f2)MB" #显示硬盘大小 echo "this Hard disk is :$(lsblk | grep '^sda' | tr -s ' ' | cut -d' ' -f4)" [root@www bin]# systeminfo.sh this host name is :www.gao.com ip address is : 10.1.252.103 this system is :CentOS release 6.8 (Final) this kernel is :2.6.32-642.el6.x86_64 this cpu is :Graphics this free is : 980MB this Hard disk is :200G
2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
#!/bin/bash #description every day cp /etc/* to /root/. #version 0.1 #author gm #date 20160810 echo "beginnig copy /etc/* file" cp -a /etc /root/etc`date +%F` echo "finish copy /etc/* file" [root@www bin]# backup.sh beginnig copy /etc/* file finish copy /etc/* file [root@www bin]# ll -d /root/etc2016-08-11/ drwxr-xr-x. 126 root root 12288 Aug 11 11:17 /root/etc2016-08-11/
3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash #description show max shiyonglv of Hard disk #version 0.1 #author gm #date 20160810 echo "begin find max shiyonglv of Hard disk" #find Hard disk is used most echo "Hard disk is used most :`df | grep -v "^/dev/sr0" | tr -s ' ' | cut -d' ' -f5 | cut -d% -f1 | sort -nr | head -1`" echo "END-------------------------" [root@www bin]# disk.sh begin find max shiyonglv of Hard disk Hard disk is used most :26 END-------------------------
4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
#!/bin/bash #description 统计远程连接的ip和连接数 #version 0.1 #author gm #date 20160810 echo "begin find links" #tongji ip and links who | tr -s ' ' | cut -d' ' -f5 | cut -d'(' -f2 | cut -d')' -f1 | sort | uniq -c | sort -rn [root@www bin]# links.sh begin find links 2 10.1.250.25 1 :0
5、写一个脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和
#!/bin/bash #description /etc/passwd user10 sum user20 #version 0.1 #author gm #date 20160810 #get user10 uid and user20 uid id_1=`sed -n '10p' /etc/passwd | cut -d: -f3` id_2=`sed -n '20p' /etc/passwd | cut -d: -f3` #uid10 sum uid20 let sum_id=$id_1+$id_2 echo "user10 sum user20 = $sum_id" #unset unset id_1 unset id_2 unset sum_id [root@www bin]# sumid.sh user10 sum user20 = 180
6、写一个脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
#!/bin/bash #description tongji two file space lines #version 0.1 #author gm #date 20160810 #get file space lines spaceline1=`grep "^[[:space:]]*$" $1 | wc -l | cut -d' ' -f2` spaceline2=`grep "^[[:space:]]*$" $2 | wc -l | cut -d' ' -f2` #sum two files lines let sumspaceline=$spaceline1+$spaceline2 echo "space lines is : $sumspaceline" #unset unset spaceline1 unset spaceline2 unset sumspaceline [root@www bin]# sumspace.sh /etc/fstab /etc/rc.d/init.d/functions space lines is : 107
7、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件
#!/bin/bash #description tongji etc var usr file numbers #version 0.1 #author gm #date 20160810 #get etc var usr file numbers file_etc=`ls -l /etc | wc -l` file_var=`ls -l /var | wc -l` file_usr=`ls -l /usr | wc -l` #sum file numbers file_sum=$[file_etc+file_var+file_usr] echo "etc var usr sum file is : $file_sum" #unset unset file_sum unset file_etc unset file_var unset file_usr [root@www bin]# sumfile.sh etc var usr sum file is : 316
8、写一个脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
#!/bin/bash #description file space lines #version 0.1 #author gm #date 20160810 #file space lines [ $# -lt 1 ] && echo "please give one argments or more " || echo " `basename $1` space lines is :`grep -c '^[[:space:]]*$' $1 `" [root@www bin]# argsnum.sh please give one argments or more [root@www bin]# argsnum.sh /etc/fstab fstab space lines is :1
9、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
#!/bin/bash #description ping ip address #version 0.1 #author gm #date 20160810 #get one arg and ping arg ping $1 -c1 -W1 &> /dev/null && echo "$1 is up" || echo "$1 is down" [root@www bin]# hostping.sh 10.1.0.2 10.1.0.2 is down [root@www bin]# hostping.sh 10.1.0.1 10.1.0.1 is up
10、判断硬盘的每个分区空间和inode的利用率是否大于80,如果是,发邮件通知root磁盘满
#!/bin/bash #description find disk or inode is used more 80% #version 0.3 #author gm #date 20160810 #disk or inode is more 80%? echo "now: finding more 80% disk or inode." df | grep 'sd' | tr -s ' ' | cut -d' ' -f1,5 | grep -E '\<([89][0-9]|100)\>' || df -i | grep 'sd' | tr -s ' ' | cut -d' ' -f1,5 | grep -E '\<([89][0-9]|100)\>' &> /dev/null [ $? -ne 0 ] && echo "no disk and disk inode is used more 80%" && exit #find more 80% of disk or inode diskuse=`df | grep 'sd' | tr -s ' ' | cut -d' ' -f1,5 | sed 's@%@@' | grep -E '\<([89][0-9]|100)\>'` diskuse="DISK:$diskuse " inodeuse=`df -i | grep 'sd' | tr -s ' ' | cut -d' ' -f1,5 | sed 's@%@@'| grep -E '\<([89][0-9]|100)\>'` inodeuse="INODE:$inodeuse" #to root mail of disk or inode information echo "$diskuse :: $inodeuse :: is used 80% or 80%+" > /root/bin/inodeuse.txt && mail -s "System mail,this is every important." root < /root/bin/inodeuse.txt echo "some disk or inode is more 80%, please into mail see." #unset unset diskuse unset inodeusr [root@www bin]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 50264772 10636332 37068440 23% / tmpfs 502068 224 501844 1% /dev/shm /dev/sda1 194241 34192 149809 19% /boot /dev/sda3 20027260 4902976 14100284 26% /testdir [root@www bin]# df -i Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda2 3203072 141793 3061279 5% / tmpfs 125517 5 125512 1% /dev/shm /dev/sda1 51200 39 51161 1% /boot /dev/sda3 1281120 4080 1277040 1% /testdir [root@www bin]# disk-use.sh now: finding more 80% disk or inode. no disk and disk inode is used more 80% [root@www bin]# todisk.sh 120+0 records in 120+0 records out 125829120 bytes (126 MB) copied, 2.91644 s, 43.1 MB/s [root@www bin]# cat todisk.sh #!/bin/bash # dd if=/dev/zero of=/boot/gmtest bs=1M count=120 [root@www bin]# disk-use.sh now: finding more 80% disk or inode. /dev/sda1 86% some disk or inode is more 80%, please into mail see. [root@www bin]# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 12 messages 1 new 1 root Wed Aug 10 22:36 19/652 "System mail,this is every important." 2 root Wed Aug 10 22:36 19/665 "System mail,this is every important." 3 root Wed Aug 10 22:44 19/685 "System mail,this is every important." 4 root Wed Aug 10 22:46 19/674 "System mail,this is every important." 5 Anacron Thu Aug 11 08:35 18/665 "Anacron job 'cron.daily' on CentOS6.localdomain" 6 root Thu Aug 11 15:02 23/683 "System mail,this is every important." 7 root Thu Aug 11 15:05 19/621 "System mail,this is every important." 8 root Thu Aug 11 15:11 19/608 "System mail,this is every important." 9 root Thu Aug 11 15:13 19/620 "System mail,this is every important." 10 root Thu Aug 11 15:14 19/620 "System mail,this is every important." 11 root Thu Aug 11 15:17 19/620 "System mail,this is every important." >N 12 root Thu Aug 11 16:23 18/609 "System mail,this is every important." & 12 Message 12: From root@www.gao.com Thu Aug 11 16:23:53 2016 Return-Path: <root@www.gao.com> X-Original-To: root Delivered-To: root@www.gao.com Date: Thu, 11 Aug 2016 16:23:52 +0800 To: root@www.gao.com Subject: System mail,this is every important. User-Agent: Heirloom mailx 12.4 7/29/08 Content-Type: text/plain; charset=us-ascii From: root@www.gao.com (root) Status: R DISK:/dev/sda1 86 :: INODE: :: is used 80% or 80%+
11、指定文件做为参数,判断文件是否为.sh后缀,如果是,添加x权限
#/bin/bash #description give one *.sh and add x #version 0.1 #author gm #date 20160810 read -p "input one file lujing : " file #if file is not exist, echo xinxi [ ! -e $file ] && echo "please one true file lujing" && exit 20 #if file is exist, test file is .sh file and chmod +x ,else echo xinxi echo `basename $file` | grep '\.sh$' &> /dev/null && ( chmod +x $file ; echo "chmod `basename $file` add x" ) || echo "`basename $file` is not .sh" [root@www bin]# touch /root/nox.sh [root@www bin]# touch /root/nox [root@www bin]# ll /root/nox /root/nox.sh -rw-r--r--. 1 root root 0 Aug 11 16:27 /root/nox -rw-r--r--. 1 root root 0 Aug 11 16:27 /root/nox.sh [root@www bin]# chmod1.sh input one file lujing : /root/no please one true file lujing [root@www bin]# chmod1.sh input one file lujing : /root/nox nox is not .sh [root@www bin]# chmod1.sh input one file lujing : /root/nox.sh chmod nox.sh add x [root@www bin]# ll /root/nox /root/nox.sh -rw-r--r--. 1 root root 0 Aug 11 16:27 /root/nox -rwxr-xr-x. 1 root root 0 Aug 11 16:27 /root/nox.sh
12、判断输入的IP是否为合法IP
#!/bin/bash #description input ip useful or no useful #version 0.1 #author gm #date 20160810 read -p "please input one useful ip:" ip_addr echo $ip_addr | grep -E "(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>" &> /dev/null && echo "this is a useful ip" || echo "this is not useful ip" [root@www bin]# read3.sh 0.0.0.0 please input one useful ip:^C [root@www bin]# read3.sh please input one useful ip:0.0.0.0 this is a useful ip [root@www bin]# read3.sh please input one useful ip:255.255.255.255 this is a useful ip [root@www bin]# read3.sh please input one useful ip:255.256.0.1 this is not useful ip [root@www bin]# read3.sh please input one useful ip:0.255.20.256 this is not useful ip
13、计算1+2+3+…+100的和
#!/bin/bash #description 1 until 100 sum. #version 0.1 #author gm #date 20160810 echo "jisuan 1 until 100 sum." #jisuan 1 dao 100 de sum sum=`seq 1 100` sum=`echo $sum | tr -t ' ' '+'` sum=$[$sum] echo "1 until 100 sum is :$sum" #unset unset sum [root@www bin]# read4.sh jisuan 1 until 100 sum. 1 until 100 sum is :5050
14、输入起始值A和最后值B,计算从A+(A+1)…+(B-1)+B的总和
#!/bin/bash #description jisuan suiji de two number and two zhijian de number sum. #version 0.1 #author gm #date 20160810 #input two number echo "pleaes two numbers; and minnumber dao maxnumber de he" read -p "one number is:" num1 read -p "two number is:" num2 #num1 > num2 exit [ $num1 -gt $num2 ] && echo "num1 > num2" && exit 2 #num1 until num2 sum sum=`seq $num1 $num2` sum=`echo $sum | tr -t ' ' '+'` sum=$[$sum] #input sum echo "$num1 until $num2 sum is :$sum" #unset unset sum unset num1 unset unm2 [root@www bin]# read5.sh pleaes two numbers; and minnumber dao maxnumber de he one number is:8 two number is:4 num1 > num2 [root@www bin]# echo $? 2 [root@www bin]# read5.sh pleaes two numbers; and minnumber dao maxnumber de he one number is:4 two number is:8 4 until 8 sum is :30
原创文章,作者:megedugao,如若转载,请注明出处:http://www.178linux.com/33355
评论列表(1条)
文章层次感清晰,通过练习加深了自己对变量的理解。