變量類型:
不同的數據類型在系統中所占資源不同,并且表示的范圍也不同
數值型:
短整型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.www58058.com/33355
文章層次感清晰,通過練習加深了自己對變量的理解。