一、什么是變量?
變量來源于數學,是計算機語言中能儲存計算結果或能表示值抽象概念。變量可以通過變量名訪問
二、變量的種類有哪些?
本地變量 生效范圍為當前shell進程;對當前shell之外的其它shell進程,包括當前shell的子shell進程均無效 環境變量 生效范圍為當前shell進程及其子進程 局部變量 生效范圍為當前shell進程中某代碼片斷(通常指函數) 位置變量 $1, $2, …來表示,用于讓腳本在腳本代碼中調用通過命令行傳遞給它的參數 特殊變量 :$?, $0, $*, $@, $#
三、變量的詳解與使用
1、本地變量
定義本地變量的格式:name=‘value’
2、可以使用引用value定義:
(1) 可以是直接字串; name=“root" (2) 變量引用:name="$USER" (3) 命令引用:name=`COMMAND`, name=$(COMMAND) 3、變量引用:${name}, $name
"" 弱引用,其中的變量引用會被替換為變量值 '' 強引用,其中的變量引用不會被替換為變量值,而保持原字符串 4、顯示已定義的所有變量
export [root@centos6 ~]# exportcd env [root@centos6 ~]# env printenv [root@centos6 ~]# printenv
5、刪除變量
[root@centos6 ~]# var=www.mageedu.com
[root@centos6 ~]# echo $var
www.mageedu.com
[root@centos6 ~]# unset var
[root@centos6 ~]# echo $var
[root@centos6 ~]#
(1)環境變量
變量聲明、賦值 export name=VALUE declare -x name=VALUE 變量引用 $name, ${name} 顯示所有環境變量 export、env、printenv 刪除變量 unset name bash有許多內建的環境變量 PATH, SHELL, USRE,UID, HISTSIZE, HOME, PWD, OLDPWD, HISTFILE, PS1
(2)只讀位置變量
只讀變量 可以引用和查看,但不能修改和刪除 定義只讀變量 readonlyname; declare -r name
(3)位置變量
$1, $2, …: 對應第1、第2等參數,shift [n]換位置 $0 命令本身 $* 傳遞給腳本的所有參數,全部參數合為一個字符串 $@ 傳遞給腳本的所有參數,每個參數為獨立字符串 $# 傳遞給腳本的參數的個數
四、位置變量的使用
環境變量的詳解
區分:$10與${10}的區別
區分:$*與$@的區別
四、課堂練習和作業
1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小
[root@centos6 tmp]# cat systeminfo.sh #!/bin/bash # echo "HostName: `hostname`" echo "IPv4: `ifconfig |sed -n '2p' |cut -d: -f2|cut -d ' ' -f1`" echo "System Version: `cat /etc/redhat-release`" echo "Kernel: `uname -r`" echo "Cpu: `lscpu |grep "name" |tr -s ' '|cut -d ' ' -f5`" echo "Memory: `free -h|sed -n '2p'|tr -s ' '|cut -d ' ' -f2`" echo "Hard Disk: `fdisk -l | sed -n '2p' |cut -d' ' -f3,4|cut -d',' -f1`" [root@centos6 tmp]# bash systeminfo.sh HostName: centos6 IPv4: 10.1.255.76 System Version: CentOS release 6.8 (Final) Kernel: 2.6.32-642.el6.x86_64 Cpu: i7-4810MQ Memory: 1.8G Hard Disk: 214.7 GB [root@centos6 tmp]#
2、編寫腳本/root/bin/backup.sh,可實現每日將/etc/目錄備份到/root/etcYYYY-mm-dd中
[root@centos6 tmp]# cat backup.sh #!/bin/bash # cp -r /etc/ /root/etc-`date +%F` [root@centos6 tmp]# crontab -l 0 23 */1 * * /bin/bash /tmp/backup.sh [root@centos6 tmp]#
3、編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值
[root@centos6 tmp]# cat disk.sh #!/bin/bash # echo "Hard disk sda is space utilization: `df -h |grep 'sda*' | tr -s ' '|cut -d' ' -f5 |sort -n | cut -d'%' -f1 |tail -1`" [root@centos6 tmp]# bash disk.sh Hard disk sda is space utilization: 22 [root@centos6 tmp]#
4、編寫腳本/root/bin/links.sh,顯示正連接本主機的每個遠程主機的IPv4地址和連接數,并按連接數從大到小排序
[root@centos6 tmp]# cat link.sh #!/bin/bash # echo -e "遠程主機連接統計為:\n\t連接數\t遠程主機IP" netstat -nt | tr -s ' ' | cut -d' ' -f5 | tr -cs '0-9.' '\n' | egrep '([0-9]+.){3}[0-9]+' | sort | uniq -c | sort -nr | tr -s ' ' '\t' [root@centos6 tmp]# bash link.sh 遠程主機連接統計為: 連接數 遠程主機IP 2 10.1.250.48 [root@centos6 tmp]#
5、寫一個腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和
[root@centos6 tmp]# [root@centos6 tmp]# cat sumid.sh #!/bin/bash # Link1=`sed -n '10p' /etc/passwd |cut -d: -f3` Link2=`sed -n '20p' /etc/passwd |cut -d: -f3` echo "第10個用戶和第20用戶的ID之和: $((Link1+Link2))" [root@centos6 tmp]# bash sumid.sh 第10個用戶和第20用戶的ID之和: 180 [root@centos6 tmp]#
6、寫一個腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和
[root@centos6 tmp]# cat sumspace.sh #!/bin/bash # Spacetotal1=`grep '^$' $1 |wc -l` Spacetotal2=`grep '^$' $2 |wc -l` echo "$1和$2空白行之和為: $((Spacetotal1+Spacetotal2))" [root@centos6 tmp]# bash sumspace.sh /etc/fstab /etc/issue /etc/fstab和/etc/issue空白行之和為: 2 [root@centos6 tmp]#
7、寫一個腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級子目錄和文件
[root@centos6 tmp]# [root@centos6 tmp]# cat sumfile.sh #!/bin/bash # Etctotal=`ls -a /etc |wc -l` Vartotal=`ls -a /var |wc -l` Usrtotal=`ls -a /usr |wc -l` echo "Sumfiletotal: $((Etctotal+Vartotal+Usrtotal))" [root@centos6 tmp]# bash sumfile.sh Sumfiletotal: 292 [root@centos6 tmp]#
8、寫一個腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數
[root@centos6 tmp]# [root@centos6 tmp]# cat argsnum.sh #!/bin/bash # [ $# -lt 1 ] && echo "At least to a argument" || grep '^$' $1|wc -l [root@centos6 tmp]# bash argsnum.sh At least to a argument [root@centos6 tmp]# bash argsnum.sh /etc/fstab 1 [root@centos6 tmp]#
9、寫一個腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”
[root@centos6 tmp]# cat hostping.sh #!/bin/bash # ping -c1 -W1 $1 &>/dev/null && echo "The $1 address can access." || echo "The $1 address can not access." [root@centos6 tmp]# bash hostping.sh 10.1.0.1 The 10.1.0.1 address can access. [root@centos6 tmp]# bash hostping.sh 10.1.0.0 The 10.1.0.0 address can not access. [root@centos6 tmp]#
10、chmod -rw /tmp/file1,編寫腳本/root/bin/per.sh,判斷當前用戶對/tmp/fiile1文件是否不可讀且不可寫
[root@centos6 tmp]# su - wang [wang@centos6 ~]$ ls -l /tmp/file1 ----------. 1 root root 1 Aug 7 10:30 /tmp/file1 [wang@centos6 ~]$ cat /tmp/per.sh #!/bin/bash # [ -r /tmp/file1 -a -w /tmp/file1 ] && echo "`whoami` can wrint and read" || echo "`whoami` can not wrint and read" [wang@centos6 ~]$ bash /tmp/per.sh wang can not wrint and read [wang@centos6 ~]$
11、編寫腳本/root/bin/nologin.sh和login.sh,實現禁止和充許普通用戶登錄系統
[root@centos6 tmp]# cat login.sh #/bin/bash # [ -f /etc/nologin ] && (rm -f /etc/nologin;echo user enable login) || echo user disable login already [root@centos6 tmp]# bash login.sh user disable login already
作業:
1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小
[root@centos6 tmp]# cat systeminfo.sh #!/bin/bash # echo "HostName: `hostname`" echo "IPv4: `ifconfig |sed -n '2p' |cut -d: -f2|cut -d ' ' -f1`" echo "System Version: `cat /etc/redhat-release`" echo "Kernel: `uname -r`" echo "Cpu: `lscpu |grep "name" |tr -s ' '|cut -d ' ' -f5`" echo "Memory: `free -h|sed -n '2p'|tr -s ' '|cut -d ' ' -f2`" echo "Hard Disk: `fdisk -l | sed -n '2p' |cut -d' ' -f3,4|cut -d',' -f1`" [root@centos6 tmp]# bash systeminfo.sh HostName: centos6 IPv4: 10.1.255.76 System Version: CentOS release 6.8 (Final) Kernel: 2.6.32-642.el6.x86_64 Cpu: i7-4810MQ Memory: 1.8G Hard Disk: 214.7 GB [root@centos6 tmp]#
2、編寫腳本/root/bin/backup.sh,可實現每日將/etc/目錄備份到/root/etcYYYY-mm-dd中
[root@centos6 tmp]# cat backup.sh #!/bin/bash # cp -r /etc/ /root/etc-`date +%F` [root@centos6 tmp]# crontab -l 0 23 */1 * * /bin/bash /tmp/backup.sh [root@centos6 tmp]#
3、編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值
[root@centos6 tmp]# cat disk.sh #!/bin/bash # echo "Hard disk sda is space utilization: `df -h |grep 'sda*' | tr -s ' '|cut -d' ' -f5 |sort -n | cut -d'%' -f1 |tail -1`" [root@centos6 tmp]# bash disk.sh Hard disk sda is space utilization: 22 [root@centos6 tmp]#
4、編寫腳本/root/bin/links.sh,顯示正連接本主機的每個遠程主機的IPv4地址和連接數,并按連接數從大到小排序
[root@centos6 tmp]# cat link.sh #!/bin/bash # echo -e "遠程主機連接統計為:\n\t連接數\t遠程主機IP" netstat -nt | tr -s ' ' | cut -d' ' -f5 | tr -cs '0-9.' '\n' | egrep '([0-9]+.){3}[0-9]+' | sort | uniq -c | sort -nr | tr -s ' ' '\t' [root@centos6 tmp]# bash link.sh 遠程主機連接統計為: 連接數 遠程主機IP 2 10.1.250.48 [root@centos6 tmp]#
5、寫一個腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和
[root@centos6 tmp]# [root@centos6 tmp]# cat sumid.sh #!/bin/bash # Link1=`sed -n '10p' /etc/passwd |cut -d: -f3` Link2=`sed -n '20p' /etc/passwd |cut -d: -f3` echo "第10個用戶和第20用戶的ID之和: $((Link1+Link2))" [root@centos6 tmp]# bash sumid.sh 第10個用戶和第20用戶的ID之和: 180 [root@centos6 tmp]#
6.1、寫一個腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和
[root@centos6 tmp]# cat sumspace.sh #!/bin/bash # Spacetotal1=`grep '^$' $1 |wc -l` Spacetotal2=`grep '^$' $2 |wc -l` echo "$1和$2空白行之和為: $((Spacetotal1+Spacetotal2))" [root@centos6 tmp]# bash sumspace.sh /etc/fstab /etc/issue /etc/fstab和/etc/issue空白行之和為: 2 [root@centos6 tmp]#
6.2、寫一個腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級子目錄和文件
[root@centos6 tmp]# [root@centos6 tmp]# cat sumfile.sh #!/bin/bash # Etctotal=`ls -a /etc |wc -l` Vartotal=`ls -a /var |wc -l` Usrtotal=`ls -a /usr |wc -l` echo "Sumfiletotal: $((Etctotal+Vartotal+Usrtotal))" [root@centos6 tmp]# bash sumfile.sh Sumfiletotal: 292 [root@centos6 tmp]#
7、寫一個腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數
[root@centos6 tmp]# [root@centos6 tmp]# cat argsnum.sh #!/bin/bash # [ $# -lt 1 ] && echo "At least to a argument" || grep '^$' $1|wc -l [root@centos6 tmp]# bash argsnum.sh At least to a argument [root@centos6 tmp]# bash argsnum.sh /etc/fstab 1 [root@centos6 tmp]#
8、寫一個腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”
[root@centos6 tmp]# cat hostping.sh #!/bin/bash # ping -c1 -W1 $1 &>/dev/null && echo "The $1 address can access." || echo "The $1 address can not access." [root@centos6 tmp]# bash hostping.sh 10.1.0.1 The 10.1.0.1 address can access. [root@centos6 tmp]# bash hostping.sh 10.1.0.0 The 10.1.0.0 address can not access. [root@centos6 tmp]#
9、chmod -rw /tmp/file1,編寫腳本/root/bin/per.sh,判斷當前用戶對/tmp/fiile1文件是否不可讀且不可寫
[root@centos6 tmp]# su - wang [wang@centos6 ~]$ ls -l /tmp/file1 ----------. 1 root root 1 Aug 7 10:30 /tmp/file1 [wang@centos6 ~]$ cat /tmp/per.sh #!/bin/bash # [ -r /tmp/file1 -a -w /tmp/file1 ] && echo "`whoami` can wrint and read" || echo "`whoami` can not wrint and read" [wang@centos6 ~]$ bash /tmp/per.sh wang can not wrint and read [wang@centos6 ~]$
10、編寫腳本/root/bin/nologin.sh和login.sh,實現禁止和充許普通用戶登錄系統。
[root@centos6 tmp]# cat login.sh #/bin/bash # [ -f /etc/nologin ] && (rm -f /etc/nologin;echo user enable login) || echo user disable login already [root@centos6 tmp]# bash login.sh user disable login already
11、寫一個腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,先判斷是否合格IP,否,提示IP格式不合法并退出,是,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”。(只提供源碼,不測試)
[root@centos6 tmp]# cat hostping.sh #!/bin/bash # echo "$1" | egrep '\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[01][0-9]|22[0-3])\>(\.\<([0-9]|[0-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>){2}\.\<([1-9]|[0-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])\>' &> /dev/null if [ $? -eq 0 ];then echo "This $1 is qualified ipv4." ping -c1 -W5 $1 &>/dev/null && echo "The $1 address can access." || echo "The $1 address can not access." else echo "This $1 is not qualified ipv4." fi
12、計算1+2+3+…+100的值
[root@centos6 tmp]# cat addnum.sh #!/bin/bash # declare -i sum=0 declare -i i=1 read -p "Enter in a positive integer:" Number while [ $i -le $Number ]; do let sum+=$i let i++ done echo "Summary: $sum." [root@centos6 tmp]# bash addnum.sh Enter in a positive integer:100 Summary: 5050. [root@centos6 tmp]#
13、計算從腳本第一參數A開始,到第二個參數B的所有數字的總和,判斷B是否大于A,否提示錯誤并退出,是則計算之。(只提供源碼,不測試)
[root@centos6 tmp]# cat last.sh #!/bin/bash # read -p "please input first number:" First read -p "please input second number:" Second if [ $First -ge $Second ]; then echo "$First greater than $Second,calculation error." && exit 1 else for i in `seq $First $Second`; do let sum+=$i let i++ done echo "Summary: $sum." fi [root@centos6 tmp]#
原創文章,作者:Aleen,如若轉載,請注明出處:http://www.www58058.com/33534
文章結構層次清晰,內容充實,有理論有操作,排版也很用心,希望能堅持下去,寫出更好的博客。