Shell腳本編程中的變量

一、什么是變量?

  變量來源于數學,是計算機語言中能儲存計算結果或能表示值抽象概念。變量可以通過變量名訪問

二、變量的種類有哪些?

本地變量 生效范圍為當前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 命令本身
$* 傳遞給腳本的所有參數,全部參數合為一個字符串
$@ 傳遞給腳本的所有參數,每個參數為獨立字符串
$# 傳遞給腳本的參數的個數

四、位置變量的使用

環境變量的詳解

blob.png

區分:$10與${10}的區別

blob.png

區分:$*與$@的區別

blob.png

四、課堂練習和作業

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

(0)
AleenAleen
上一篇 2016-08-13
下一篇 2016-08-14

相關推薦

  • 第五周學習總結–任務計劃

    寫在前面 隨著學習的不斷深入,漸漸的已經意識到不再是開始一天只知道幾個簡單的命令而已了,但是呢,命令還必須要繼續學習的,修房子哪有不用磚的,所以,要想有一棟堅實穩固的房子,還必須靜下心來繼續認識命令。 命令注解 今天要講的幾個命令是跟工作息息相關的,他們都是在以后的生活中使用率比較高的命令之一,比如馬上要講的這個,如果你的老板讓你在每天凌晨三點執行一次數據備…

    2017-12-29
  • CentOS系統啟動流程

       開機不是只要單擊電源鈕,而關機只要關掉電源鈕就可以了嗎?話是這樣沒錯啦,但是由于 Linux 是一套多人多任務的操作系統,你難保你在關機時沒有人在在線,如果你關機的時候碰巧一大群人在在線工作, 那會讓當時在在線工作的人馬上斷線的!那不是害死人了!一些數據可是無價之寶。    另外 Linux 在執行的時候,雖然你…

    Linux干貨 2016-09-19
  • 文本處理三劍客之一的sed

    處理文本的工具sed Stream EDitor, 行編輯器 sed是一種流編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩沖區中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區中的內容,處理完成后,把緩沖區的內容送往屏幕。接著處理下一行,這樣不斷重復,直到文件末尾。文件內容并沒有改變,除非你使用重定向存儲輸出。Se…

    Linux干貨 2017-04-27
  • hello 小伙伴們

    一起跟著馬哥,努力向前!不管多么累、不管有多忙,不要間斷、不要放棄,為了實現我們心中所想的,努力吧,騷年們!

    Linux干貨 2016-08-08
  • N22-?Linux系統的基礎命令

                                     Linux系統的基礎命令 命令的語法通用格式:# COMMAND OPTIONS  ARGUMENTS COMMA…

    Linux干貨 2016-08-15
  • 文件的權限、擴展屬性以及facl

    大綱: 一、前言 二、普通權限 三、特殊權限 四、ext文件的擴展屬性 五、文件的訪問控制列表(facl) 一、前言 linux中常見的權限有讀(r)、寫(w)、執行(x),還有3個特殊的權限。因此下面就從普通權限開始介紹起 二、普通權限 rwx:讀 寫 執行 rwxr-xr-x : 讀寫執行 讀_執行 讀_執行  (分別對應)屬主 屬組…

    Linux干貨 2015-05-04

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-08-16 17:22

    文章結構層次清晰,內容充實,有理論有操作,排版也很用心,希望能堅持下去,寫出更好的博客。

欧美性久久久久