1. 程式人生 > >Vim編輯器-Shell腳本

Vim編輯器-Shell腳本

系統/運維 Linux

一、Vim編輯器

1.1 概念及其作用

用於編寫、修改文本,是默認已經安裝在Linux上的文本編輯器,類似於windows的Word


1.2 vim的三個工作模式

1)命令模式:Vi/Vim 默認模式,不能輸入字符,但是可以控制光標移動,關鍵詞檢索,復制,粘貼等基本功能,通過命令模式可以進入末行模式和輸入模式

2)末行模式:從命令模式上輸入冒號(: )進入,可以對文本進行保存、退出、查找等操作,可通過 Esc 鍵退回到命令模式

3)輸入模式:通過命令模式下鍵入 a/i/o 進入,輸入字符串、文本信息等,可通過 Esc 鍵退回到命令模式


1.3 vim的編輯保存模塊

i

在光標所在位置進行編輯;

a

在光標所在位置後一位進行編輯;

o

在光標所在位置重新啟動一行進行編輯;

:w

保存當前編輯本文;

:q

退出當前編輯文本;

:!

強制使用


1.4 vim的搜索替換模塊

/string

自頂向下查找;

?string

自底向上查找;

:set nu

顯示行號

:set nonu

不顯示行號

:noh

不高亮顯示

:set ingorecase

開啟忽略大小寫;

:set noingorecase

關閉忽略大小寫;

:[range]s/字符串 A/字符串 B/[參數]

用字符串B替換字符串A

range表示範圍:1,4 ----> 第一行至第四行

$ ------> 最後一行

%------> 全文

參數:c ----> 每次替換前進行詢問

e ----> 不顯示錯誤信息

g ----> 替換一行中所有匹配項

技術分享圖片

開啟忽略大小寫:

技術分享圖片

把windows替換為Centos(註意windows是小寫):

技術分享圖片

交互詢問:

技術分享圖片

替換成功:

技術分享圖片


1.5 vim的針對程序員模塊

:syntax on

開啟語法檢查

:set autoindent

自動縮進,默認 8 個空格

:set shiftwidth=4

設置縮進字符

註意:可以通過編輯配置/etc/vimrc 使某些針對程序員的功能自動生效



1.6其他快捷命令

h/j/k/l

控制光標移動(左下上右)

^

將光標移至行首

$

將光標移至行尾

G

將光標移至文件尾

gg

將光標移至文件頭

ctrl + f

向下翻頁

ctrl + b

向上翻頁

u

撤銷操作

x

刪除光標所在位置內容

dd

刪除光標所在行

D

刪除光標所在位置到所在行結尾

yy

復制光標所在行

p

在光標所在位置將復制或刪除內容粘貼


二、shell編程入門

2.1 Shell的概念及其作用

Shell 是一款 Linux 系統下,用於用戶命令與 Linux 內核連接的軟件,是一款命令解釋器,能夠識別用戶輸入的各種命令,並傳遞給操作系統。大多數 Linux 版本默認情況下使用的 Shell 版本是BASH。


2.2 Shell 腳本

Shell 腳本,嚴格上叫做 BASH 編程, 依賴於 Vim 編輯器,作為文本保存在 Linux 上,一定以.sh結尾Shell 解釋器支持變量、命令行參數、交互式輸入、函數模塊及流控語句等


2.3 Shell 編程

2.3.1分類

1)交互式:用戶每輸入一條命令,結果顯示在屏幕上

2)非交互式:用戶每輸入一條命令後,結果不顯示在屏幕上(輸出重定向)

3)批處理:用戶編寫好完整shell腳本後一次執行


2.3.2執行方式

1)方式1:給腳本加上x權限並以絕對或相對路徑+腳本文件執行(因為默認情況所有的命令都是通過PATH尋找命令,不加路徑會通過PATH變量查找)

[root@RHEL6 ~]# cat test.sh

seq 3

[root@RHEL6 ~]# ll test.sh

-rw-r--r--. 1 root root 6 May 10 09:10 test.sh

[root@RHEL6 ~]# chmod u+x test.sh //賦予x權限

[root@RHEL6 ~]# ll test.sh

-rwxr--r--. 1 root root 6 May 10 09:10 test.sh

[root@RHEL6 ~]# ./test.sh

1

2

3

2)方式2:使用bash/sh命令+腳本文件(無需x權限)

[root@RHEL6 ~]# chmod u-x test.sh //取消x權限

[root@RHEL6 ~]# ll test.sh

-rw-r--r--. 1 root root 6 May 10 09:10 test.sh

[root@RHEL6 ~]# bash test.sh

1

2

3


2.3.3位置參數

$0

腳本的名稱

$n

第n個位置參數

$#

位置參數總個數

$*

所有的位置參數

技術分享圖片

[root@RHEL6 ~]# bash test.sh hello world

This script is test.sh ///$0

The parameter number is 2,they are hello world

The first is hello ///$1

The second is world ///$2


2.4判斷用戶的參數

1)兩種形式:

① test命令:test 參數+表達式

② [ ]命令:[ 表達式1 參數 表達式2 ]

2)結果:

條件成立,返回數字0;不成立,返回(非零)隨機數;保存在$?中

2.4.1文件測試

用於判斷文件是否存在,並且是什麽類型

操作符

作用

-d

測試文件是否為目錄類型

-e

測試文件是否存在

-f

判斷是否為一般文件

-L

判斷是否為鏈接文件

-r

測試當前用戶是否有權限讀取

-w

測試當前用戶是否有權限寫入

-x

測試當前用戶是否有權限執行

[root@RHEL6 ~]# ls

anaconda-ks.cfg Downloads Music Templates www.mqzzl.fun

Desktop install.log Pictures test.sh

Documents install.log.syslog Public Videos

[root@RHEL6 ~]# test -f Music/

[root@RHEL6 ~]# echo $?

1

[root@RHEL6 ~]# [ -w test.sh ]

[root@RHEL6 ~]# echo $?

0


2.4.2邏輯測試(與、或、非)

1)”與 &&”:符號兩邊的表達式同時成立,才是真

[root@RHEL6 ~]# [ -d Music ] && [ -w test.sh ]

[root@RHEL6 ~]# echo $?

0

[root@RHEL6 ~]# [ -e music ] && [ -w test.sh ]

[root@RHEL6 ~]# echo $?

1

2)”或 ||”:符號兩邊的表達式有一個成立就認為是真

[root@RHEL6 ~]# [ -e music ] || [ -w test.sh ]

[root@RHEL6 ~]# echo $?

0

3)” 非 !”: 對表達式取反

[ $USER != roo ] && echo $USER

root

[root@RHEL6 ~]# echo $?

0


2.4.3 整數值測試(比較大小,不能用 > <和=

操作符

作用

-eq

是否等於

-ne

是否不等於

-gt

是否大於

-lt

是否小於

-le

是否等於或小於

-ge

是否大於或等於

[root@RHEL6 ~]# a=10

[root@RHEL6 ~]# b=100

[root@RHEL6 ~]# if [ $a -lt $b ]; then echo "true"; fi

true


2.4.4字符串測試(比較區別)

操作符

作用

=

比較字符串內容是否相同

!=

比較字符串內容是否不同

-z

判斷字符串內容是否為空

[root@RHEL6 ~]# ls

anaconda-ks.cfg Downloads Music Templates www.mqzzl.fun

Desktop install.log Pictures test.sh

Documents install.log.syslog Public Videos

[root@RHEL6 ~]# [ -z $test.sh ]

[root@RHEL6 ~]# echo $?

1

[root@RHEL6 ~]# echo $test.sh

.sh


2.5流程控制語句

2.5.1 if語句--判斷語句

1)用於對條件表達式進行判斷,執行滿足條件表達式的語句

2)語法結構:if-then-fi,if-then-else-fi,if-then-elif-then-else-fi

[root@RHEL6 ~]# cat ping.sh

#!/bin/bash

ping -c 4 -i 0.2 -W 3 $1 &> /dev/null

if [ $? -eq 0 ]

then

echo "Host $1 is online"

else

echo "Host $1 is offline"

fi

[root@RHEL6 ~]# bash ping.sh 192.168.100.1

Host 192.168.100.1 is online

[root@RHEL6 ~]# bash ping.sh 192.168.100.2

Host 192.168.100.2 is offline

-------------------------------------------------------------------------------------

[root@RHEL6 ~]# cat Grade.sh

#!/bin/bash

read -p "Enter your score(0-100):" GRADE

if [ $GRADE -ge 80 ];then

echo "The grade is excellent!!"

elif [ $GRADE -ge 60 ]&&[ $GRADE -lt 80 ];then

echo "The grade is pass"

else

echo "The grade is failure"

fi

[root@RHEL6 ~]# bash Grade.sh

Enter your score(0-100):88

The grade is excellent!!

[root@RHEL6 ~]# bash Grade.sh

Enter your score(0-100):59

The grade is failure


2.5.2 for語句--循環語句

1)根據取值列表,循環執行命令,循環徹底後結束

2)語法結構:

for 變量 in {list}

do

循環體

done

[root@RHEL6 ~]# cat user.txt

tom

jack

lilei

lucy

[root@RHEL6 ~]# cat for.sh

#!/bin/bash

for USER in `cat /root/user.txt`

do

id $USER &> /dev/null

if [ $? -eq 0 ];then

echo "$USER exists"

else

useradd $USER &> /dev/null

echo "1234" | passwd --stdin $USER &> /dev/null

if [ $? -eq 0 ];then

echo "$USER,Create success"

else

echo "$USER,Create failure"

fi

fi

done

[root@RHEL6 ~]# bash for.sh

tom exists

jack,Create success

lilei,Create success

lucy exists


2.5.3 while語句--循環語句

1)用於進行條件判斷,當條件不滿足時,結束命令!

2)語法結構:

while 表達式

do

循環體

done

[root@RHEL6 ~]# cat while.sh

#!/bin/bash

NUMBER=$(expr $RANDOM % 1000)

TIMES=0

while true

do

read -p "Please input a number(0-999):" INT

let TIMES++

if [ $INT -gt $NUMBER ];then

echo "too big"

elif [ $INT -lt $NUMBER ];then

echo "too small"

else

echo "It is right,the number is $NUMBER"

echo "TIMES=$TIMES"

exit 0

fi

done

Please input a number(0-999):959

too small

Please input a number(0-999):960

It is right,the number is 960

TIMES=2


2.5.4 case語句--判斷語句

1)用於對條件表達式進行判斷,執行滿足條件表達式的語句

2)語法結構:

case 變量 in

條件表達式 1)

語句;;

條件表達式 2)

語句;;

*)

語句;;

esac

[root@RHEL6 ~]# cat case.sh

#!/bin/bash

read -p "Please input a char:" KEY

case $KEY in

[a-z]|[A-Z])

echo "This is a char"

;;

[0-9])

echo "This is a num"

;;

*)

echo "This is a special char"

esac

[root@RHEL6 ~]# bash case.sh

Please input a char:9

This is a num

[root@RHEL6 ~]# bash case.sh

Please input a char:h

This is a char

[root@RHEL6 ~]# bash case.sh

Please input a char:!

This is a special char

Vim編輯器-Shell腳本