shell作為用戶和Unix/Linux溝通的橋梁,既可以是一個可執行的二進制程序,同時也是一個具備了編程能力的程序設計語言,定義了各種各樣的變量和參數,下面介紹一下shell之上的各種變量。
1、本地變量
特性:只對當前shell生效
[root@centos7 ~/bin]#cat localvar1.sh #!/bin/bash # var1=hei echo "localvar1:$var1" localvar2.sh [root@centos7 ~/bin]#cat localvar2.sh #!/bin/bash var2=ha echo "localvar2:var2 is "$var2"" echo "localvar2:var1 is "$var1"" [root@centos7 ~/bin]#localvar1.sh localvar1:hei localvar2:var2 is ha localvar2:var1 is
localvar1腳本中輸出var1變量之外,還執行了localvar2腳本,同時打開了一個子shell,而在localvar2中,不僅輸出var2變量,還要輸出前一個shell中定義的var1。因為本地變量只對當前shell有效,所以子shell中輸出不成功。
2、環境變量
[root@centos7 ~/bin]#cat localvar1.sh #!/bin/bash # export var1=hei echo "localvar1:$var1" localvar2.sh [root@centos7 ~/bin]#localvar1.sh localvar1:hei localvar2:var2 is ha localvar2:var1 is hei
注意:還是上述例子,只需將localvar1中的var1定義為環境變量,localvar1中定義的var1的作用范圍將擴展至當前shell及其子shell,所以localvar2中可以輸出var1。
3、特殊變量
<1>區分$*和$@
[root@centos7 ~/bin]#cat arg1.sh #!/bin/bash # arg2.sh "$*" echo -------------- arg2.sh "$@" [root@centos7 ~/bin]#cat arg2.sh #!/bin/bash echo "First parameter is $1" echo "Second parameter is $2" [root@centos7 ~/bin]#arg1.sh a b First parameter is a b Second parameter is -------------- First parameter is a Second parameter is b
執行arg1腳本時,實際上是通過將$*和$#作為參數賦予腳本arg2,來執行arg2,子shell中執行的arg2腳本再通過位置變量將這倆個參數輸出,進而區分$*和$@的區別。得出結論:$*將全部參數作為一個字符串,而$#將全部參數識別為獨立的字符串。
<2>認識$#,$0,$?
[root@centos7 ~/bin]#cat test.sh #!/bin/bash # echo $0 echo $# [root@centos7 ~/bin]#echo $? 0
上面可以看出:$0是腳本的名稱,$#是參數的個數,而$?是測試上一條命令是否執行成功。
4、位置變量
[root@centos7 ~/bin]#cat countdao.sh #!/bin/bash # echo "$1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} $11 $12" [root@centos7 ~/bin]#countdao.sh a b c a b c a1 a2 [root@centos7 ~/bin]#countdao.sh z b c z b c z1 z2
位置變量中$1..$9正常識別,$10將被識別為$1+1,解決方法$10–>${10}
注意:在bash中位置變量也是有上限的,如果要測試,可使用以下代碼
[root@centos7 /tmp]#cat testpa.sh #!/bin/bash #Author:Zhao #Name:testpa.sh #Function:Test the maximum value of the script can accept echo $200000 [root@centos7 /tmp]#bash testpa.sh `echo {1..200000}` 200000 -----------------此處小編只測試到200000,大家可以加大數值
5、局部變量
特點:只對特定的代碼片段生效
強化練習
1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小
#!/bin/bash #Author:Zhao #Name:Systeminfo #Function:A brief description of the system infomation #--------- IP=`ifconfig | sed -n '2p' | tr -s ' ' | cut -d' ' -f3` OS_Version=`cat /etc/centos-release` Kernel=`cat /etc/centos-release` CPU_Model=`cat /proc/cpuinfo | grep "model name" | sed -r 's@.*[[:space:]](i7.*)@\1@'` Disk_Available=`fdisk -l | sed -n '2p' | tr ',' ':' | cut -d: -f2` Memory=`cat /proc/meminfo | head -1 | sed -r 's@^.*[[:space:]]([[:digit:]]+.*)@\1@'` echo "IP:$IP" echo "OS_Version:$OS_Verson" echo "Kernel:$Kernel" echo "CPU_Model:$CPU_Model" echo "Disk_Available=$Disk_Available" echo "Memory:$Memory" unset IP unset OS_Version unset Kernel unset CPU_Model unset Disk_Available unset Memory
2、編寫腳本/root/bin/backup.sh,可實現手動將/etc/目錄備份到/root/etcYYYY-mm-dd中
#!/bin/bash #Author:Zhao #Name:backup.sh #Function:Backup /etc directory cp -a /etc/ /root/etc`date +%F`
3、編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值
#!/bin/bash #Author:Zhao #Name:disk.sh #Function:Query disk usage Maxdisk=`df | grep -v "media" | grep dev | tr ' ' ':' | tr -s ':' | cut -d: -f1,5 | sort -t: -k2 | tail -1` echo "Maximum disk usage is:$Maxdisk" unset Maxdisk
4、編寫腳本/root/bin/links.sh,顯示正連接本主機的每個遠程主機的IPv4地址和連接數,并按連接數從大到小排序
#!/bin/bash #Author:Zhao #Name:links.sh #Function:Query current numbers of connection links=`netstat -tn | grep tcp | tr -s ' ' | cut -d' ' -f5 | sort -r` echo -e "The numbers of connection are\n$links" unset links
5、寫一個腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和
#!/bin/bash #Author:Zhao #Name:sumid.sh #Function:Calculate the sum of ID line1=10 line2=20 ID1=`sed -n ''$line1'p' /etc/passwd | cut -d: -f3` -----特別注意sed處的變量替換的寫法 ID2=`sed -n ''$line2'p' /etc/passwd | cut -d: -f3` sumid=$[$ID1+$ID2] echo "The sum of ID is:$sumid" unset line1 unset line2 unset ID1 unset ID2 unset sumid
6、寫一個腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和
#!/bin/bash #Author:Zhao #Name:sumspace.sh #Function:Calculate the number of blank lines line1=`grep "^$" $1 | uniq -c` line2=`grep "^$" $2 | uniq -c` linesum=$[$line1+$line2] echo "The number of blank lines is: $linesum" unset line1 unset line2 unset linesum
7、寫一個腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級子目錄和文件
#Author:Zhao #Name:sumspace.sh #Function:Calculate the number of directorys and files etc_file=`ls -A /etc | wc -l` var_file=`ls -A /var | wc -l` usr_file=`ls -A /var | wc -l` file_sum=$[$etc_file+$var_file+$usr_file] echo "The sum of directory and files is:$file_sum" unset etc_file unset var_file unset usr_file
7、寫一個腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行
#!/bin/bash #Author:Zhao #Name:links.sh #Function:Query current numbers of connection [ $# -lt 1 ] && (echo "At least input a parameter"; exit) || (`grep "^$" $1 | wc -l` && echo "The blank lines is:$blank") unset blank
8、寫一個腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”
#!/bin/bash #Author:Zhao #Name:hostping.sh #Function:Test whether the IP address can communicate IP1=`echo $1 | grep -E -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])"` [[ $1 == $IP1 ]] && (echo "legal IP"; ping -c1 -W1 $1 &> /dev/null && echo "The IP address can be accessed" || echo "The IP address can't be accessed") || echo "Illegal IP"; exit unset IP1
9、chmod -rw /tmp/file1,編寫腳本/root/bin/per.sh,判斷當前用戶對/tmp/fiile1文件是否不可讀且不可寫
#!/bin/bash #Author=Zhao #Name:per.sh #Function:Test user's permissions for file File=/tmp/file1 Who=`whoami` [ ! -r $File -a ! -w $File ] && echo "Don't have read and write permissions,$Who" || [ ! -r $File ] && echo "No read permission,$Who" || [ ! -w $File ] && echo "No write permission,$Who" unset File unset Who
注意:上面這個例子如果拿root測試會出現問題
10、編寫腳本/root/bin/nologin.sh和login.sh,實現禁止和充許普通用戶登錄系統。
#!/bin/bash #Author=Zhao #Name:per.sh #Function:Test user's permissions for file File=/etc/nologin [ -f $File ] && echo "No login system" || (touch $File; echo "System Locked") unset File
#!/bin/bash #Author:Zhao #Name:login.sh #Function:Make users can login system File=/etc/nologin [ -f $File ] && (rm -f $File; echo "System Unlocked") || echo "You can login system" unset File
12、計算1+2+3+…+100的值
#!/bin/bash #Author:Zhao #Name:sum.sh #Function:Calculate the sum of figures sum=`echo {1..100} | tr ' ' '+' | bc` echo "{1..100} sum is:$sum" unset sum
13、計算從腳本第一參數A開始,到第二個參數B的所有數字的總和,判斷B是否大于A,否提示錯誤并退出,是則計算之
#!/bin/bash #Author:Zhao #Name:sum2.sh #Function:Calculate the sum of figures sum=`seq $1 $2 | xargs | tr ' ' '+' | bc` [ $2 -gt $1 ] && echo "{A..B} sum is:$sum" || echo "Error input" unset sum
原創文章,作者:mfwing,如若轉載,請注明出處:http://www.www58058.com/34649
通過示例演示了各個變量之間的區別,最好能總結一下實驗中結果,通過一句話能描述出來,這樣以后自己復習過程中也會輕松很多哦