Shell的腳本學習

 

第1章 Shell概述

1)需要看懂運維人員編寫的Shell程序。

2)偶爾會編寫一些簡單Shell程序來管理集羣、提高開發效率。

 

第2章 Shell解析器

(1)Linux提供的Shell解析器有:

[atguigu@hadoop101 ~]$ cat /etc/shells

/bin/sh

/bin/bash

/sbin/nologin

/bin/dash

/bin/tcsh

/bin/csh

(2)bash和sh的關係

[atguigu@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

[atguigu@hadoop102 bin]$ echo $SHELL

/bin/bash

第3章 Shell腳本入門

1.腳本格式

腳本以#!/bin/bash開頭(指定解析器)

2.第一個Shell腳本:helloworld

(1)需求:創建一個Shell腳本,輸出helloworld

(2)案例實操:

[atguigu@hadoop101 datas]$ touch helloworld.sh

[atguigu@hadoop101 datas]$ vi helloworld.sh

 

在helloworld.sh中輸入如下內容

#!/bin/bash

echo "helloworld"

(3)腳本的常用執行方式

第一種:採用bash或sh+腳本的相對路徑或絕對路徑(不用賦予腳本+x權限)

       sh+腳本的相對路徑

[atguigu@hadoop101 datas]$ sh helloworld.sh

Helloworld

       sh+腳本的絕對路徑

[atguigu@hadoop101 datas]$ sh /home/atguigu/datas/helloworld.sh

helloworld

       bash+腳本的相對路徑

[atguigu@hadoop101 datas]$ bash helloworld.sh

Helloworld

       bash+腳本的絕對路徑

[atguigu@hadoop101 datas]$ bash /home/atguigu/datas/helloworld.sh

Helloworld

第二種:採用輸入腳本的絕對路徑或相對路徑執行腳本(必須具有可執行權限+x

(a)首先要賦予helloworld.sh 腳本的+x權限

[atguigu@hadoop101 datas]$ chmod 777 helloworld.sh

(b)執行腳本

相對路徑

[atguigu@hadoop101 datas]$ ./helloworld.sh

Helloworld

絕對路徑

[atguigu@hadoop101 datas]$ /home/atguigu/datas/helloworld.sh

Helloworld

注意:第一種執行方法,本質是bash解析器幫你執行腳本,所以腳本本身不需要執行權限。第二種執行方法,本質是腳本需要自己執行,所以需要執行權限。

3.第二個Shell腳本:多命令處理

(1)需求:

在/home/atguigu/目錄下創建一個banzhang.txt,在banzhang.txt文件中增加“I love cls”。

(2)案例實操:

[atguigu@hadoop101 datas]$ touch batch.sh

[atguigu@hadoop101 datas]$ vi batch.sh

 

在batch.sh中輸入如下內容

#!/bin/bash



cd /home/atguigu

touch cls.txt

echo "I love cls" >>cls.txt

第4章 Shell中的變量

4.1 系統變量

1. 常用系統變量

$HOME、$PWD、$SHELL、$USER等

2.案例實操

(1)查看系統變量的值

[atguigu@hadoop101 datas]$ echo $HOME

/home/atguigu

(2)顯示當前Shell中所有變量:set

[atguigu@hadoop101 datas]$ set

BASH=/bin/bash

BASH_ALIASES=()

BASH_ARGC=()

BASH_ARGV=()

4.2 自定義變量

1.基本語法

(1)定義變量:變量=值

(2)撤銷變量:unset 變量

(3)聲明靜態變量:readonly變量,注意:不能unset

2.變量定義規則

       (1)變量名稱可以由字母、數字和下劃線組成,但是不能以數字開頭,環境變量名建議大寫

       2)等號兩側不能有空格

       (3)在bash中,變量默認類型都是字符串類型,無法直接進行數值運算。

       (4)變量的值如果有空格,需要使用雙引號或單引號括起來。

3.案例實操

       (1)定義變量A

[atguigu@hadoop101 datas]$ A=5

[atguigu@hadoop101 datas]$ echo $A

5

       (2)給變量A重新賦值

[atguigu@hadoop101 datas]$ A=8

[atguigu@hadoop101 datas]$ echo $A

8

       (3)撤銷變量A

[atguigu@hadoop101 datas]$ unset A

[atguigu@hadoop101 datas]$ echo $A

       (4)聲明靜態的變量B=2,不能unset

[atguigu@hadoop101 datas]$ readonly B=2

[atguigu@hadoop101 datas]$ echo $B

2

[atguigu@hadoop101 datas]$ B=9

-bash: B: readonly variable

       (5)在bash中,變量默認類型都是字符串類型,無法直接進行數值運算

[atguigu@hadoop102 ~]$ C=1+2

[atguigu@hadoop102 ~]$ echo $C

1+2

(6)變量的值如果有空格,需要使用雙引號或單引號括起來

[atguigu@hadoop102 ~]$ D=I love banzhang

-bash: world: command not found

[atguigu@hadoop102 ~]$ D="I love banzhang"

[atguigu@hadoop102 ~]$ echo $A

I love banzhang

       (7)可把變量提升爲全局環境變量,可供其他Shell程序使用

export 變量名

[atguigu@hadoop101 datas]$ vim helloworld.sh



在helloworld.sh文件中增加echo $B

#!/bin/bash



echo "helloworld"

echo $B



[atguigu@hadoop101 datas]$ ./helloworld.sh

Helloworld

發現並沒有打印輸出變量B的值。

[atguigu@hadoop101 datas]$ export B

[atguigu@hadoop101 datas]$ ./helloworld.sh

helloworld

2

4.3 特殊變量:$n

1.基本語法

       $n    (功能描述:n爲數字,$0代表該腳本名稱,$1-$9代表第一到第九個參數,十以上的參數,十以上的參數需要用大括號包含,如${10})

2.案例實操

(1)輸出該腳本文件名稱、輸入參數1和輸入參數2 的值

[atguigu@hadoop101 datas]$ touch parameter.sh

[atguigu@hadoop101 datas]$ vim parameter.sh
#!/bin/bash

echo "$0  $1   $2"



[atguigu@hadoop101 datas]$ chmod 777 parameter.sh



[atguigu@hadoop101 datas]$ ./parameter.sh cls  xz

./parameter.sh  cls   xz

4.4 特殊變量:$#

1.基本語法

       $#    (功能描述:獲取所有輸入參數個數,常用於循環)。

2.案例實操

(1)獲取輸入參數的個數

#!/bin/bash

echo "$0  $1   $2"



[atguigu@hadoop101 datas]$ chmod 777 parameter.sh



[atguigu@hadoop101 datas]$ ./parameter.sh cls  xz

./parameter.sh  cls   xz

4.5 特殊變量:$*、$@

1.基本語法

       $*    (功能描述:這個變量代表命令行中所有的參數,$*把所有的參數看成一個整體)

       $@  (功能描述:這個變量也代表命令行中所有的參數,不過$@把每個參數區分對待)

2.案例實操

(1)打印輸入的所有參數

[atguigu@hadoop101 datas]$ vim parameter.sh



#!/bin/bash

echo "$0  $1   $2"

echo $#

echo $*

echo $@



[atguigu@hadoop101 datas]$ bash parameter.sh 1 2 3

parameter.sh  1   2

3

1 2 3

1 2 3

4.6 特殊變量:$?

1.基本語法

$?  (功能描述:最後一次執行的命令的返回狀態。如果這個變量的值爲0,證明上一個命令正確執行;如果這個變量的值爲非0(具體是哪個數,由命令自己來決定),則證明上一個命令執行不正確了。)

2.案例實操

       (1)判斷helloworld.sh腳本是否正確執行

[atguigu@hadoop101 datas]$ ./helloworld.sh

hello world

[atguigu@hadoop101 datas]$ echo $?

0

第5章 運算符      

1.基本語法

(1)“$((運算式))”或“$[運算式]”

(2)expr  + , - , \*,  /,  %    加,減,乘,除,取餘

注意:expr運算符間要有空格

2.案例實操:

(1)計算3+2的值

[atguigu@hadoop101 datas]$ expr 2 + 3

5

(2)計算3-2的值

[atguigu@hadoop101 datas]$ expr 3 - 2

1

(3)計算(2+3)X4的值

       (a)expr一步完成計算

[atguigu@hadoop101 datas]$ expr `expr 2 + 3` \* 4

20

(b)採用$[運算式]方式

[atguigu@hadoop101 datas]# S=$[(2+3)*4]

[atguigu@hadoop101 datas]# echo $S

第6章 條件判斷

1.基本語法

[ condition ](注意condition前後要有空格

注意:條件非空即爲true,[ atguigu ]返回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)

3.案例實操  

     (1)23是否大於等於22

[atguigu@hadoop101 datas]$ [ 23 -ge 22 ]

[atguigu@hadoop101 datas]$ echo $?

0

       (2)helloworld.sh是否具有寫權限

[atguigu@hadoop101 datas]$ [ -w helloworld.sh ]

[atguigu@hadoop101 datas]$ echo $?

0

       (3)/home/atguigu/cls.txt目錄中的文件是否存在

[atguigu@hadoop101 datas]$ [ -e /home/atguigu/cls.txt ]

[atguigu@hadoop101 datas]$ echo $?

1

(4)多條件判斷(&& 表示前一條命令執行成功時,才執行後一條命令,|| 表示上一條命令執行失敗後,才執行下一條命令)

[atguigu@hadoop101 ~]$ [ condition ] && echo OK || echo notok

OK

[atguigu@hadoop101 datas]$ [ condition ] && [ ] || echo notok

notok

第7章 流程控制(重點)

7.1 if 判斷

1.基本語法

if [ 條件判斷式 ];then

  程序

fi

或者

if [ 條件判斷式 ]

  then

    程序

fi

       注意事項:

(1)[ 條件判斷式 ],中括號和條件判斷式之間必須有空格

(2)if後要有空格

2.案例實操

(1)輸入一個數字,如果是1,則輸出banzhang zhen shuai,如果是2,則輸出cls zhen mei,如果是其它,什麼也不輸出。

[atguigu@hadoop101 datas]$ touch if.sh

[atguigu@hadoop101 datas]$ vim if.sh



#!/bin/bash



if [ $1 -eq "1" ]

then

        echo "banzhang zhen shuai"

elif [ $1 -eq "2" ]

then

        echo "cls zhen mei"

fi



[atguigu@hadoop101 datas]$ chmod 777 if.sh

[atguigu@hadoop101 datas]$ ./if.sh 1

banzhang zhen shuai

7.2 case 語句

1.基本語法

case $變量名 in

  "值1")

    如果變量的值等於值1,則執行程序1

    ;;

  "值2")

    如果變量的值等於值2,則執行程序2

    ;;

  …省略其他分支…

  *)

    如果變量的值都不是以上的值,則執行此程序

    ;;

esac

注意事項:

  1. case行尾必須爲單詞“in”,每一個模式匹配必須以右括號“)”結束。
  2. 雙分號“;;”表示命令序列結束,相當於java中的break。
  3. 最後的“*)”表示默認模式,相當於java中的default。

2.案例實操

(1)輸入一個數字,如果是1,則輸出banzhang,如果是2,則輸出cls,如果是其它,輸出renyao。

[atguigu@hadoop101 datas]$ touch case.sh

[atguigu@hadoop101 datas]$ vim case.sh



!/bin/bash



case $1 in

"1")

        echo "banzhang"

;;



"2")

        echo "cls"

;;

*)

        echo "renyao"

;;

esac



[atguigu@hadoop101 datas]$ chmod 777 case.sh

[atguigu@hadoop101 datas]$ ./case.sh 1

1

7.3 for 循環

1.基本語法1

       for (( 初始值;循環控制條件;變量變化 ))

  do

    程序

  done

2.案例實操

(1)從1加到100

[atguigu@hadoop101 datas]$ touch for1.sh

[atguigu@hadoop101 datas]$ vim for1.sh



#!/bin/bash



s=0

for((i=0;i<=100;i++))

do

        s=$[$s+$i]

done

echo $s



[atguigu@hadoop101 datas]$ chmod 777 for1.sh

[atguigu@hadoop101 datas]$ ./for1.sh

“5050”

3.基本語法2

for 變量 in 值1 值2 值3…

  do

    程序

  done

4.案例實操

       (1)打印所有輸入參數

[atguigu@hadoop101 datas]$ touch for2.sh

[atguigu@hadoop101 datas]$ vim for2.sh



#!/bin/bash

#打印數字



for i in $*

    do

      echo "ban zhang love $i "

    done



[atguigu@hadoop101 datas]$ chmod 777 for2.sh

[atguigu@hadoop101 datas]$ bash for2.sh cls xz bd

ban zhang love cls

ban zhang love xz

ban zhang love bd

(2)比較$*和$@區別

(a)$*和$@都表示傳遞給函數或腳本的所有參數,不被雙引號“”包含時,都以$1 $2 …$n的形式輸出所有參數。

[atguigu@hadoop101 datas]$ touch for.sh

[atguigu@hadoop101 datas]$ vim for.sh

 

#!/bin/bash

 

for i in $*

do

      echo "ban zhang love $i "

done

 

for j in $@

do     

        echo "ban zhang love $j"

done

 

[atguigu@hadoop101 datas]$ bash for.sh cls xz bd

ban zhang love cls

ban zhang love xz

ban zhang love bd

ban zhang love cls

ban zhang love xz

ban zhang love bd

(b)當它們被雙引號“”包含時,“$*”會將所有的參數作爲一個整體,以“$1 $2 …$n”的形式輸出所有參數;“$@”會將各個參數分開,以“$1” “$2”…”$n”的形式輸出所有參數。

[atguigu@hadoop101 datas]$ vim for.sh



#!/bin/bash



for i in "$*"

#$*中的所有參數看成是一個整體,所以這個for循環只會循環一次

        do

                echo "ban zhang love $i"

        done



for j in "$@"

#$@中的每個參數都看成是獨立的,所以“$@”中有幾個參數,就會循環幾次

        do

                echo "ban zhang love $j"

done



[atguigu@hadoop101 datas]$ chmod 777 for.sh

[atguigu@hadoop101 datas]$ bash for.sh cls xz bd

ban zhang love cls xz bd

ban zhang love cls

ban zhang love xz

ban zhang love bd

7.4 while 循環

1.基本語法

while [ 條件判斷式 ]

  do

    程序

  done

2.案例實操

       (1)從1加到100

[atguigu@hadoop101 datas]$ touch while.sh

[atguigu@hadoop101 datas]$ vim while.sh



#!/bin/bash

s=0

i=1

while [ $i -le 100 ]

do

        s=$[$s+$i]

        i=$[$i+1]

done



echo $s



[atguigu@hadoop101 datas]$ chmod 777 while.sh

[atguigu@hadoop101 datas]$ ./while.sh

5050

第8章 read讀取控制檯輸入

1.基本語法

       read(選項)(參數)

       選項:

-p:指定讀取值時的提示符;

-t:指定讀取值時等待的時間(秒)。

參數

       變量:指定讀取值的變量名

2.案例實操

       (1)提示7秒內,讀取控制檯輸入的名稱

[atguigu@hadoop101 datas]$ touch read.sh

[atguigu@hadoop101 datas]$ vim read.sh



#!/bin/bash



read -t 7 -p "Enter your name in 7 seconds " NAME

echo $NAME



[atguigu@hadoop101 datas]$ ./read.sh

Enter your name in 7 seconds xiaoze

xiaoze

第9章 函數

9.1 系統函數

1.basename基本語法

basename [string / pathname] [suffix]        (功能描述:basename命令會刪掉所有的前綴包括最後一個(‘/’)字符,然後將字符串顯示出來。

選項:

suffix爲後綴,如果suffix被指定了,basename會將pathname或string中的suffix去掉。

2.案例實操

(1)截取該/home/atguigu/banzhang.txt路徑的文件名稱

[atguigu@hadoop101 datas]$ basename /home/atguigu/banzhang.txt

banzhang.txt

[atguigu@hadoop101 datas]$ basename /home/atguigu/banzhang.txt .txt

banzhang

3.    dirname基本語法

       dirname 文件絕對路徑        (功能描述:從給定的包含絕對路徑的文件名中去除文件名(非目錄的部分),然後返回剩下的路徑(目錄的部分))

4.案例實操

(1)獲取banzhang.txt文件的路徑

[atguigu@hadoop101 ~]$ dirname /home/atguigu/banzhang.txt

/home/atguigu

9.2 自定義函數

1.基本語法

[ function ] funname[()]

{

       Action;

       [return int;]

}

funname

2.經驗技巧

       (1)必須在調用函數地方之前,先聲明函數,shell腳本是逐行運行。不會像其它語言一樣先編譯。

       (2)函數返回值,只能通過$?系統變量獲得,可以顯示加:return返回,如果不加,將以最後一條命令運行結果,作爲返回值。return後跟數值n(0-255)

3.案例實操

       (1)計算兩個輸入參數的和

[atguigu@hadoop101 datas]$ touch fun.sh

[atguigu@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;



[atguigu@hadoop101 datas]$ chmod 777 fun.sh

[atguigu@hadoop101 datas]$ ./fun.sh

Please input the number1: 2

Please input the number2: 5

7

第10章 Shell工具(重點)

10.1 cut

cut的工作就是“剪”,具體的說就是在文件中負責剪切數據用的。cut 命令從文件的每一行剪切字節、字符和字段並將這些字節、字符和字段輸出。

1.基本用法

cut [選項參數]  filename

說明:默認分隔符是製表符

2.選項參數說明

選項參數

功能

-f

列號,提取第幾列

-d

分隔符,按照指定分隔符分割列

3.案例實操

(0)數據準備

[atguigu@hadoop101 datas]$ touch cut.txt

[atguigu@hadoop101 datas]$ vim cut.txt

dong shen

guan zhen

wo  wo

lai  lai

le  le

(1)切割cut.txt第一列

[atguigu@hadoop101 datas]$ cut -d " " -f 1 cut.txt

dong

guan

wo

lai

le

(2)切割cut.txt第二、三列

[atguigu@hadoop101 datas]$ cut -d " " -f 2,3 cut.txt

shen

zhen

 wo

 lai

 le

(3)在cut.txt文件中切割出guan

[atguigu@hadoop101 datas]$ cat cut.txt | grep "guan" | cut -d " " -f 1

guan

(4)選取系統PATH變量值,第2個“:”開始後的所有路徑:

[atguigu@hadoop101 datas]$ echo $PATH

/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/atguigu/bin



[atguigu@hadoop102 datas]$ echo $PATH | cut -d: -f 2-  (2-:包含第二個":"後所以路徑)

/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/atguigu/bin

(5)切割ifconfig 後打印的IP地址

[atguigu@hadoop101 datas]$ ifconfig eth0 | grep "inet addr" | cut -d: -f 2 | cut -d" " -f1

192.168.1.102

10.2 sed

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

  1. 基本用法
sed [選項參數]  ‘command’  filename
  1. 選項參數說明

選項參數

功能

-e

直接在指令列模式上進行sed的動作編輯。

  1. 命令功能描述

命令

功能描述

a

新增,a的後面可以接字串,在下一行出現

d

刪除

s

查找並替換 

  1. 案例實操
(0)數據準備

[atguigu@hadoop102 datas]$ touch sed.txt

[atguigu@hadoop102 datas]$ vim sed.txt

dong shen

guan zhen

wo  wo

lai  lai



le  le

(1)將“mei nv”這個單詞插入到sed.txt第二行下,打印。

[atguigu@hadoop102 datas]$ sed '2a mei nv' sed.txt

dong shen

guan zhen

mei nv

wo  wo

lai  lai



le  le

[atguigu@hadoop102 datas]$ cat sed.txt

dong shen

guan zhen

wo  wo

lai  lai



le  le

注意:文件並沒有改變

(2)刪除sed.txt文件所有包含wo的行

[atguigu@hadoop102 datas]$ sed '/wo/d' sed.txt

dong shen

guan zhen

lai  lai



le  le

(3)將sed.txt文件中wo替換爲ni

[atguigu@hadoop102 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

[atguigu@hadoop102 datas]$ sed -e '2d' -e 's/wo/ni/g' sed.txt

dong shen

ni  ni

lai  lai



le  le

10.3 awk

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

  1. 基本用法
awk [選項參數] ‘pattern1{action1}  pattern2{action2}...’ filename

pattern:表示AWK在數據中查找的內容,就是匹配模式

action:在找到匹配內容時所執行的一系列命令
  1. 選項參數說明

選項參數

功能

-F

指定輸入文件折分隔符

-v

賦值一個用戶定義變量

  1. 案例實操
(0)數據準備

[atguigu@hadoop102 datas]$ sudo cp /etc/passwd ./

(1)搜索passwd文件以root關鍵字開頭的所有行,並輸出該行的第7列。

[atguigu@hadoop102 datas]$ awk -F: '/^root/{print $7}' passwd

/bin/bash

(2)搜索passwd文件以root關鍵字開頭的所有行,並輸出該行的第1列和第7列,中間以“,”號分割。

[atguigu@hadoop102 datas]$ awk -F: '/^root/{print $1","$7}' passwd

root,/bin/bash

注意:只有匹配了pattern的行纔會執行action

(3)只顯示/etc/passwd的第一列和第七列,以逗號分割,且在所有行前面添加列名user,shell在最後一行添加"dahaige,/bin/zuishuai"。

[atguigu@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

。。。

atguigu,/bin/bash

dahaige,/bin/zuishuai

注意:BEGIN 在所有數據讀取行之前執行;END 在所有數據執行之後執行。

(4)將passwd文件中的用戶id增加數值1並輸出

[atguigu@hadoop102 datas]$ awk -v i=1 -F: '{print $3+i}' passwd

1

2

3

4
  1. awk的內置變量

變量

說明

FILENAME

文件名

NR

已讀的記錄數

NF

瀏覽記錄的域的個數(切割後,列的個數)

  1. 案例實操
(1)統計passwd文件名,每行的行號,每行的列數

[atguigu@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

[atguigu@hadoop102 datas]$ ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk -F " " '{print $1}'

192.168.1.102

         (3)查詢sed.txt中空行所在的行號

[atguigu@hadoop102 datas]$ awk '/^$/{print NR}' sed.txt

5

10.4 sort

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

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

表1-57

選項

說明

-n

依照數值的大小排序

-r

以相反的順序來排序

-t

設置排序時所用的分隔字符

-k

指定需要排序的列

參數:指定待排序的文件列表

2. 案例實操

(0)數據準備

[atguigu@hadoop102 datas]$ touch sort.sh

[atguigu@hadoop102 datas]$ vim sort.sh

bb:40:5.4

bd:20:4.2

xz:50:2.3

cls:10:3.5

ss:30:1.6

(1)按照“:”分割後的第三列倒序排序。

[atguigu@hadoop102 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

第11章 企業真實面試題(重點)

11.1 京東

問題1:使用Linux命令查詢file1中空行所在的行號

答案:

[atguigu@hadoop102 datas]$ awk '/^$/{print NR}' sed.txt

5

問題2:有文件chengji.txt內容如下:

張三 40

李四 50

王五 60

使用Linux命令計算第二列的和並輸出

[atguigu@hadoop102 datas]$ cat chengji.txt | awk -F " " '{sum+=$2} END{print sum}'

150

11.2 搜狐&和訊網

問題1:Shell腳本里如何檢查一個文件是否存在?如果不存在該如何處理?

#!/bin/bash



if [ -f file.txt ]; then

   echo "文件存在!"

else

   echo "文件不存在!"

fi

11.3 新浪

問題1:用shell寫一個腳本,對文本中無序的一列數字排序

[root@CentOS6-2 ~]# cat test.txt

9

8

7

6

5

4

3

2

10

1

[root@CentOS6-2 ~]# sort -n test.txt|awk '{a+=$0;print $0}END{print "SUM="a}'

1

2

3

4

5

6

7

8

9

10

SUM=55

11.3 金和網絡

問題1:請用shell腳本寫出查找當前文件夾(/home)下所有的文本文件內容中包含有字符”shen”的文件名稱

[atguigu@hadoop102 datas]$ grep -r "shen" /home | cut -d ":" -f 1

/home/atguigu/datas/sed.txt

/home/atguigu/datas/cut.txt

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章