shell概括

Shell

1)Linux提供的Shell解析器有:
[jinghnag@hadoop101 ~]$ cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
(2)bash和sh的關係
[jinghnag@hadoop101 bin]$ ll | grep bash
-rwxr-xr-x. 1 root root 941880 5月  11 2016 bash
lrwxrwxrwx. 1 root root      4 5月  27 2017 sh -> bash
(3)Centos默認的解析器是bash
[jinghnag@hadoop101 bin]$ echo $SHELL
/bin/bash
1.腳本格式
腳本以#!/bin/bash開頭(指定解析器)
2.第一個Shell腳本:helloworld
(1)需求:創建一個Shell腳本,輸出helloworld
(2)案例實操:
	[jinghnag@hadoop101 datas]$ touch helloworld.sh
	[jinghnag@hadoop101 datas]$ vi helloworld.sh
在helloworld.sh中輸入如下內容
#!/bin/bash
echo "helloworld"
(3)腳本的常用執行方式
第一種:採用bash或sh+腳本的相對路徑或絕對路徑(不用賦予腳本+x權限)
	sh+腳本的相對路徑
	[jinghnag@hadoop101 datas]$ sh helloworld.sh 
	Helloworld
	sh+腳本的絕對路徑
	[jinghnag@hadoop101 datas]$ sh /home/jinghnag/datas/helloworld.sh 
	helloworld
	bash+腳本的相對路徑
	[jinghnag@hadoop101 datas]$ bash helloworld.sh 
	Helloworld
	bash+腳本的絕對路徑
	[jinghnag@hadoop101 datas]$ bash /home/jinghnag/datas/helloworld.sh 
	Helloworld
第二種:採用輸入腳本的絕對路徑或相對路徑執行腳本(必須具有可執行權限+x,推薦採用這種方式)
(a)首先要賦予helloworld.sh 腳本的+x權限
	[jinghnag@hadoop101 datas]$ chmod +x helloworld.sh
(b)執行腳本
相對路徑
	[jinghnag@hadoop101 datas]$ ./helloworld.sh 
	Helloworld
絕對路徑
	[jinghnag@hadoop101 datas]$ /home/jinghnag/datas/helloworld.sh 
	Helloworld
注意:第一種執行方法,本質是bash解析器幫你執行腳本,所以腳本本身不需要執行權限。第二種執行方法,本質是腳本需要自己執行,所以需要執行權限。	
3.第二個Shell腳本:多命令處理
(1)需求: 
在/home/jinghnag/目錄下創建一個banzhang.txt,在banzhang.txt文件中增加“I love cls”。
(2)案例實操:
	[jinghnag@hadoop101 datas]$ touch batch.sh
	[jinghnag@hadoop101 datas]$ vi batch.sh
在batch.sh中輸入如下內容
	#!/bin/bash
	cd /home/jinghnag
	touch cls.txt
	echo "I love cls" >>cls.txt
(3)查看腳本的執行流程(一般使用這個命令來查看腳本執行的步驟,錯誤)
	[jinghnag@hadoop101 datas]$ bash -x  batch.sh
(4)查看腳本的語法(一般使用這個命令來查看腳本語法錯誤)
	[jinghnag@hadoop101 datas]$ bash -n  batch.sh

Shell中的變量

常用系統變量
$HOME、$PWD、$SHELL、$USER
eg:
	(1)查看系統變量的值
		[jinghnag@hadoop101 datas]$ echo $HOME
		/home/jinghnag
	(2)顯示當前Shell中所有變量:set
		[jinghnag@hadoop101 datas]$ set
		BASH=/bin/bash
		BASH_ALIASES=()
		BASH_ARGC=()
		BASH_ARGV=()
自定義變量
(1)定義變量:變量=值 
(2)撤銷變量:unset 變量
(3)聲明靜態變量:readonly變量,注意:不能unset
 變量定義規則
	(1)變量名稱可以由字母、數字和下劃線組成,但是不能以數字開頭,環境變量名建議大寫。
	(2)等號兩側不能有空格
	(3)在bash中,變量默認類型都是字符串類型,無法直接進行數值運算。
	(4)變量的值如果有空格,需要使用雙引號或單引號括起來。
	eg:
		(1)定義變量A
	[jinghnag@hadoop101 datas]$ A=5
		[jinghnag@hadoop101 datas]$ echo $A
		5
		(2)給變量A重新賦值
	[jinghnag@hadoop101 datas]$ A=8
		[jinghnag@hadoop101 datas]$ echo $A
	8
		(3)撤銷變量A
	[jinghnag@hadoop101 datas]$ unset A
		[jinghnag@hadoop101 datas]$ echo $A
		(4)聲明靜態的變量B=2,不能unset
		[jinghnag@hadoop101 datas]$ readonly B=2
		[jinghnag@hadoop101 datas]$ echo $B
		2
		[jinghnag@hadoop101 datas]$ B=9
		-bash: B: readonly variable
		(5)在bash中,變量默認類型都是字符串類型,無法直接進行數值運算
		[jinghnag@hadoop101 ~]$ C=1+2
		[jinghnag@hadoop101 ~]$ echo $C
		1+2
	(6)變量的值如果有空格,需要使用雙引號或單引號括起來
		[jinghnag@hadoop101 ~]$ D=I love banzhang
		-bash: world: command not found
		[jinghnag@hadoop101 ~]$ D="I love banzhang"
		[jinghnag@hadoop101 ~]$ echo $A
		I love banzhang
		(7)可把變量提升爲全局環境變量,可供其他Shell程序使用
	export 變量名
	[jinghnag@hadoop101 datas]$ vim helloworld.sh 
	在helloworld.sh文件中增加echo $B
		#!/bin/bash
		
		echo "helloworld"
		echo $B
		[jinghnag@hadoop101 datas]$ ./helloworld.sh 
		Helloworld
	發現並沒有打印輸出變量B的值。
[jinghnag@hadoop101 datas]$ export B
		[jinghnag@hadoop101 datas]$ ./helloworld.sh 
		helloworld
		2
	###### 特殊變量:$n	(功能描述:n爲數字,$0代表該腳本名稱,$1-$9代表第一到第九個參數,十以上的參數,十以上的參數需要用大括號包含,如${10})
eg:
(1)輸出該腳本文件名稱、輸入參數1和輸入參數2 的值
	[jinghnag@hadoop101 datas]$ touch parameter.sh 
	[jinghnag@hadoop101 datas]$ vim parameter.sh
	
	#!/bin/bash
	echo "$0  $1   $2"
	
	[jinghnag@hadoop101 datas]$ chmod 777 parameter.sh
	
	[jinghnag@hadoop101 datas]$ ./parameter.sh cls  xz
	./parameter.sh  cls   xz
特殊變量:$#
$#	(功能描述:獲取所有輸入參數個數,常用於循環)。
eg:
	(1)獲取輸入參數的個數
		[jinghnag@hadoop101 datas]$ vim parameter.sh
		
		#!/bin/bash
		echo "$0  $1   $2"
		echo $#
		
		[jinghnag@hadoop101 datas]$ chmod 777 parameter.sh
		
		[jinghnag@hadoop101 datas]$ ./parameter.sh cls  xz
		parameter.sh cls xz 
		2
特殊變量:*、@
$*	(功能描述:這個變量代表命令行中所有的參數,$*把所有的參數看成一個整體)
$@	(功能描述:這個變量也代表命令行中所有的參數,不過$@把每個參數區分對待)
eg:
	(1)打印輸入的所有參數
		[jinghnag@hadoop101 datas]$ vim parameter.sh
		
		#!/bin/bash
		echo "$0  $1   $2"
		echo $#
		echo $*
		echo $@
		
		[jinghnag@hadoop101 datas]$ bash parameter.sh 1 2 3
		parameter.sh  1   2
		3
		1 2 3
		1 2 3

	特殊變量:$?
	判斷helloworld.sh腳本是否正確執行
	[jinghnag@hadoop101 datas]$ ./helloworld.sh 
		hello world
		[jinghnag@hadoop101 datas]$ echo $?
		0

Shell 數組

Shell 數組用括號來表示,元素用"空格"符號分割開,語法格式如下:
方式2: arr=(value1 value2 value3)   (這種方式帶值)

注意:往數組裏添加值,數組的長度自動增長
	eg:
		#!/bin/bash
		my_array=(A B "C" D)
		我們也可以使用下標來定義數組:
		array_name[0]=value0
		array_name[1]=value1
		array_name[2]=value2
讀取數組
讀取數組元素值的一般格式是:
$ echo {array_name[index]}
	eg:
		#!/bin/bash
		my_array=(A B "C" D)
	
		echo "第一個元素爲: ${my_array[0]}"
		echo "第二個元素爲: ${my_array[1]}"
		echo "第三個元素爲: ${my_array[2]}"
		echo "第四個元素爲: ${my_array[3]}"
	執行腳本,輸出結果如下所示:
	$ chmod +x test.sh 
	$ ./test.sh第一個元素爲: A第二個元素爲: B第三個元素爲: C第四個元素爲: D
	(3)獲取數組中的所有元素
	使用@ 或 * 可以獲取數組中的所有元素,例如:
#!/bin/bash
		my_array[0]=A
		my_array[1]=B
		my_array[2]=C
		my_array[3]=D
		echo "數組的元素爲: ${my_array[*]}"
		echo "數組的元素爲: ${my_array[@]}"
	(4)獲取數組的長度
	獲取數組長度的方法與獲取字符串長度的方法相同,例如:
		#!/bin/bash
		my_array[0]=A
		my_array[1]=B
		my_array[2]=C
		my_array[3]=D
		echo "數組元素個數爲: ${#my_array[*]}"
		echo "數組元素個數爲: ${#my_array[@]}"
	執行腳本,輸出結果如下所示:
		$ chmod +x test.sh 
		$ ./test.sh數組元素個數爲: 4數組元素個數爲: 4
	(5)遍歷數組
	案例實操
	#!/bin/bash
		arr=(1 2 3 4 5 6 7)
		for i in ${arr[*]}
		do
		echo $i
		done

運算符

1.基本語法
(1)“$((運算式))”或“$[運算式]”  + , - , *,  /,  %    加,減,乘,除,取餘
(2)expr  + , - , \*,  /,  %    加,減,乘,除,取餘
注意:expr運算符間要有空格
eg:
	(1)計算3+2的值
		[jinghnag@hadoop101 datas]$ expr 2 + 3
		5
	(2)計算3-2的值
	[jinghnag@hadoop101 datas]$ expr 3 - 2 
		1
	(3)計算(2+3)X4的值
		(a)expr一步完成計算
	[jinghnag@hadoop101 datas]$ expr `expr 2 + 3` \* 4
		20
	(b)採用$[運算式]方式
[jinghnag@hadoop101 datas]# S=$[(2+3)*4]
		[jinghnag@hadoop101 datas]# echo $S

條件判斷

1.基本語法
[ condition ](注意condition前後要有空格)
注意:條件非空即爲true,[ jinghnag ]返回true,[] 返回false。
2. 常用判斷條件
(1)兩個整數之間比較
= 字符串比較
-lt 小於(less than)			-le 小於等於(less equal)
-eq 等於(equal)				-gt 大於(greater than)
-ge 大於等於(greater equal)	-ne 不等於(Not equal)
(2)按照文件權限進行判斷
-r 有讀的權限(read)			-w 有寫的權限(write)
-x 有執行的權限(execute)
(3)按照文件類型進行判斷
-f 文件存在並且是一個常規的文件(file)
-e 文件存在(existence)		-d 文件存在並是一個目錄(directory)
(4).案例實操
(1)23是否大於等於22
	[jinghnag@hadoop101 datas]$ [ 23 -ge 22 ]
	[jinghnag@hadoop101 datas]$ echo $?
	0
	(2)helloworld.sh是否具有寫權限
	[jinghnag@hadoop101 datas]$ [ -w helloworld.sh ]
	[jinghnag@hadoop101 datas]$ echo $?
	0
(3)/home/jinghnag/cls.txt目錄中的文件是否存在
	[jinghnag@hadoop101 datas]$ [ -e /home/jinghnag/cls.txt ]
	[jinghnag@hadoop101 datas]$ echo $?
	1
(4)多條件判斷(&& 表示前一條命令執行成功時,才執行後一條命令,|| 表示上一條命令執行失敗後,才執行下一條命令)
	[jinghnag@hadoop101 ~]$ [ condition ] && echo OK || echo notok
	OK
	[jinghnag@hadoop101 datas]$ [ condition ] && [ ] || echo notok
	notok
###### read讀取控制檯輸入
read(選項)(參數)
	選項:
-p:指定讀取值時的提示符;
-t:指定讀取值時等待的時間(秒)。
參數
	變量:指定讀取值的變量名
2.案例實操
	(1)提示7秒內,讀取控制檯輸入的名稱
	[jinghnag@hadoop101 datas]$ touch read.sh
	[jinghnag@hadoop101 datas]$ vim read.sh
	
	#!/bin/bash
	
	read -t 7 -p "Enter your name in 7 seconds " NAME
	echo $NAME
	
	[jinghnag@hadoop101 datas]$ ./read.sh 
	Enter your name in 7 seconds xiaoze
	xiaoze
系統函數
1.basename基本語法
basename [string / pathname] [suffix]  	(功能描述:basename命令會刪掉所有的前綴包括最後一個(‘/’)字符,然後將字符串顯示出來。
選項:
suffix爲後綴,如果suffix被指定了,basename會將pathname或string中的suffix去掉。
2.案例實操
(1)截取該/home/jinghnag/banzhang.txt路徑的文件名稱
	[jinghnag@hadoop101 datas]$ basename /home/jinghnag/banzhang.txt 
	banzhang.txt
	[jinghnag@hadoop101 datas]$ basename /home/jinghnag/banzhang.txt .txt
	banzhang
3.	dirname基本語法
	dirname 文件絕對路徑		(功能描述:從給定的包含絕對路徑的文件名中去除文件名(非目錄的部分),然後返回剩下的路徑(目錄的部分))
4.案例實操
(1)獲取banzhang.txt文件的路徑
	[jinghnag@hadoop101 ~]$ dirname /home/jinghnag/banzhang.txt 
	/home/jinghnag
	自定義函數
	[ function ] funname[()]
	{
		Action;
		[return int;]
	}
	funname
2.經驗技巧
	(1)必須在調用函數地方之前,先聲明函數,shell腳本是逐行運行。不會像其它語言一樣先編譯。
	(2)函數返回值,只能通過$?系統變量獲得,可以顯示加:return返回,如果不加,將以最後一條命令運行結果,作爲返回值。return後跟數值n(0-255)
3.案例實操
	(1)計算兩個輸入參數的和
	[jinghnag@hadoop101 datas]$ touch fun.sh
	[jinghnag@hadoop101 datas]$ vim fun.sh
	
	#!/bin/bash
	function sum()
	{
	    s=0
	    s=$[ $1 + $2 ]
	    echo "$s"
	}
	
	read -p "Please input the number1: " n1;
	read -p "Please input the number2: " n2;
	sum $n1 $n2;
	
	[jinghnag@hadoop101 datas]$ chmod 777 fun.sh
	[jinghnag@hadoop101 datas]$ ./fun.sh 
	Please input the number1: 2
	Please input the number2: 5
	7
	 cut
cut [選項參數]  filename
說明:默認分隔符是製表符
2.選項參數說明

選項參數	功能
-f	列號,提取第幾列
-d	分隔符,按照指定分隔符分割列
-c	指定具體的字符
3.案例實操
(0)數據準備
	[jinghnag@hadoop101 datas]$ touch cut.txt
	[jinghnag@hadoop101 datas]$ vim cut.txt
	dong shen
	guan zhen
	wo  wo
	lai  lai
	le  le
(1)切割cut.txt第一列
	[jinghnag@hadoop101 datas]$ cut -d " " -f 1 cut.txt 
	dong
	guan
	wo
	lai
	le
(2)切割cut.txt第二、三列
	[jinghnag@hadoop101 datas]$ cut -d " " -f 2,3 cut.txt 
	shen
	zhen
	 wo
	 lai
	 le
(3)在cut.txt文件中切割出guan
	[jinghnag@hadoop101 datas]$ cat cut.txt | grep "guan" | cut -d " " -f 1
	guan
(4)切割ifconfig 後打印的IP地址
[jinghnag@hadoop101 datas]$ ifconfig eth0 | grep "inet addr" | cut -d: -f 2 | cut -d" " -f 1
	192.168.1.102

sed

sed是一種流編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩衝區中,稱爲“模式空間”,接着用sed命令處理緩衝區中的內容,處理完成後,把緩衝區的內容送往屏幕。接着處理下一行,這樣不斷重複,直到文件末尾。文件內容並沒有改變,除非你使用重定向存儲輸出。

1.基本用法
sed [選項參數]  ‘command’  filename
2.選項參數說明
選項參數	   功能
  -e	   直接在指令列模式上進行sed的動作編輯。
  -i	   直接編輯文件
3.命令功能描述
命令	   功能描述
 a 	   新增,a的後面可以接字串,在下一行出現
 d	   刪除
 s	   查找並替換 
4.案例實操
(0)數據準備
	[jinghnag@hadoop101 datas]$ touch sed.txt
	[jinghnag@hadoop101 datas]$ vim sed.txt
	dong shen
	guan zhen
	wo  wo
	lai  lai
	
	le  le
(1)將“mei nv”這個單詞插入到sed.txt第二行下,打印。
	[jinghnag@hadoop101 datas]$ sed '2a mei nv' sed.txt 
	dong shen
	guan zhen
	mei nv
	wo  wo
	lai  lai
	
	le  le
	[jinghnag@hadoop101 datas]$ cat sed.txt 
	dong shen
	guan zhen
	wo  wo
	lai  lai
	
	le  le
注意:文件並沒有改變
(2)刪除sed.txt文件所有包含wo的行
	[jinghnag@hadoop101 datas]$ sed '/wo/d' sed.txt
	dong shen
	guan zhen
	lai  lai
	
	le  le
(3)將sed.txt文件中wo替換爲ni
	[jinghnag@hadoop101 datas]$ sed 's/wo/ni/g' sed.txt 
	dong shen
	guan zhen
	ni  ni
	lai  lai
	
	le  le
注意:‘g’表示global,全部替換
(4)將sed.txt文件中的第二行刪除並將wo替換爲ni
	[jinghnag@hadoop101 datas]$ sed -e '2d' -e 's/wo/ni/g' sed.txt 
	dong shen
	ni  ni
	lai  lai
	
	le  le

awk

一個強大的文本分析工具,把文件逐行的讀入,以空格爲默認分隔符將每行切片,切開的部分再進行分析處理。

1.基本用法
awk [選項參數] ‘pattern1{action1}  pattern2{action2}...’ filename
pattern:表示AWK在數據中查找的內容,就是匹配模式
action:在找到匹配內容時所執行的一系列命令
2.選項參數說明
選項參數	 功能
 -F	     指定輸入文件折分隔符
 -v	     賦值一個用戶定義變量
3.案例實操
(0)數據準備
	[jinghnag@hadoop102 datas]$ sudo cp /etc/passwd ./
(1)搜索passwd文件以root關鍵字開頭的所有行,並輸出該行的第7列。
	[jinghnag@hadoop102 datas]$ awk -F: '/^root/{print $7}' passwd 
	/bin/bash
(2)搜索passwd文件以root關鍵字開頭的所有行,並輸出該行的第1列和第7列,中間以“,”號分割。
[jinghnag@hadoop102 datas]$ awk -F: '/^root/{print $1","$7}' passwd 
	root,/bin/bash
注意:只有匹配了pattern的行纔會執行action
(3)只顯示/etc/passwd的第一列和第七列,以逗號分割,且在所有行前面添加列名user,shell在最後一行添加"dahaige,/bin/zuishuai"。
	[jinghnag@hadoop102 datas]$ awk -F : 'BEGIN{print "user, shell"} {print $1","$7} END{print "dahaige,/bin/zuishuai"}' passwd
	user, shell
	root,/bin/bash
	bin,/sbin/nologin
	。。。
	jinghnag,/bin/bash
	dahaige,/bin/zuishuai
注意:BEGIN 在所有數據讀取行之前執行;END 在所有數據執行之後執行。
(4)將passwd文件中的用戶id增加數值1並輸出
	[jinghnag@hadoop102 datas]$ awk -v i=1 -F: '{print $3+i}' passwd
	1
	2
	3
	4
4.awk的內置變量
變量	        說明
FILENAME	文件名
NR	        已讀的記錄數
NF	        瀏覽記錄的域的個數(切割後,列的個數)
5.案例實操
(1)統計passwd文件名,每行的行號,每行的列數
[jinghnag@hadoop102 datas]$ awk -F: '{print "filename:"  FILENAME ", linenumber:" NR  ",columns:" NF}' passwd 
	filename:passwd, linenumber:1,columns:7
	filename:passwd, linenumber:2,columns:7
	filename:passwd, linenumber:3,columns:7
	  (2)切割IP
	[jinghnag@hadoop102 datas]$ ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk -F " " '{print $1}' 
	192.168.1.102
	  (3)查詢sed.txt中空行所在的行號
	[jinghnag@hadoop102 datas]$ awk '/^$/{print NR}' sed.txt 
	5

sort

sort命令是在Linux裏非常有用,它將文件進行排序,並將排序結果標準輸出。

1.基本語法
sort(選項)(參數)

選項	  說明
-n	  依照數值的大小排序
-r	  以相反的順序來排序
-t	  設置排序時所用的分隔字符
-k	  指定需要排序的列
參數:指定待排序的文件列表
2. 案例實操
(0)數據準備
	[jinghnag@hadoop101 datas]$ touch sort.sh
	[jinghnag@hadoop101 datas]$ vim sort.sh 
	bb:40:5.4
	bd:20:4.2
	xz:50:2.3
	cls:10:3.5
	ss:30:1.61)按照“:”分割後的第三列倒序排序。
	[jinghnag@hadoop101 datas]$ sort -t : -nrk 3  sort.sh 
	bb:40:5.4
	bd:20:4.2
	cls:10:3.5
	xz:50:2.3
	ss:30:1.6
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章