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 21:01
下一篇 2016-08-14 17:44

相關推薦

  • 文件查看和正則表達式

    文件查看工具         cat,tac,rev,more,less,head,tail cat:查看文本          cat [OPTION]… [FILE]… &…

    Linux干貨 2016-08-07
  • 優云軟件老司機:如何讓運維操作更輕松、高效

    講師介紹 龐輝富 ?廣通軟件技術總監 ?擁有10多年IT運維管理軟件研發經驗 ?致力于自動化運維解決方案的研究和推廣 ?主導研發的產品廣泛應用于海關、公安、能源等多個行業 技術發展給運維帶來的挑戰 當前的IT建設在這些新技術的演進下,我們看到的是呈現“雙態IT”特征。Gartner也提出雙模IT理論,與現在談的雙態IT是異曲同工的,不再是一種單純的形態,而是…

    系統運維 2017-01-09
  • find 和 壓縮工具

    find命令 一、find命令基本介紹 1、find作用 通過遍歷指定路徑實時查找符合條件的文件。 find工作特點 2、find工作特點 ?查找速度較慢 ?精確查找 ?實時查找 ?用戶只能搜索有讀取和執行權限的目錄 3、語法 find [OPTIONS]… [查找路徑] [查找條件] [處理方式] 查找路徑:指定…

    Linux干貨 2016-08-16
  • Yacc 與 Lex 快速入門(詞法分析和語法分析)

    Lex 代表 Lexical Analyzar。Yacc 代表 Yet Another Compiler Compiler。 讓我們從 Lex 開始吧。 Lex Lex 是一種生成掃描器的工具。掃描器是一種識別文本中的詞匯模式的程序。 這些詞匯模式(或者常規表達式)在一種特殊的句子結構中定義,這個我們一會兒就要討論。 一種匹配的常規表達式可能會包含相關的動作…

    Linux干貨 2015-06-08
  • 高級文件系統管理

    高級文件系統管理 一、如何創建新的swap分區  答:1.在/dev/sdc上創建新的分區。并使得ID為82的linux swap;類型。   2.創建文件系統 mkswap  -L  SWAP_SDC1  /dev/sdc1     3.在/etc/fstab中進行修改   4…

    Linux干貨 2016-08-30
  • AWK文本工具和軟件包管理

    AWK文本工具 兩種版本1.nawk ??2.gawk gawk ?? 模式掃描和處理語言 選項: -F 指明輸入時用到的字段分隔符 -v ?var=value:自定義變量 基本格式: awk [options] ’program’???file…. program:pattern{action statrments;………

    Linux干貨 2018-03-15

評論列表(1條)

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

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

欧美性久久久久