Vim Editor-shell script

Source: Internet
Author: User
Tags syslog

First, Vim Editor

1.1 concept and its role

Text Editor for writing, modifying text, which is already installed on Linux by default, similar to Windows word


1.2 Vim three modes of Operation

1) Command mode: Vi/vim default mode, cannot input characters, but can control cursor movement, keyword retrieval, copy, paste and other basic functions, through the command mode can enter the last line mode and input mode

2) The last line mode: Enter the colon (:) from the command mode, you can save the text, exit, find and so on, can be returned to the command mode by the ESC key

3) Input mode: Enter by typing a/i/o in command mode, input string, text information, etc., can be returned to command mode by ESC key


1.3 Vim the Edit Save module

I

Edit at cursor position;

A

After the cursor position, one is edited;

O

Restarts a row at the cursor location for editing;

: W

Save the current edit this article;

: Q

Exit the current edit text;

:!

Forced use


1.4 Vim the search replacement module

/string

Top-down search;

? string

Bottom-up search;

: Set Nu

Show line Numbers

: Set Nonu

Do not display line numbers

: Noh

Do not highlight

: Set Ingorecase

Turn on ignore case;

: Set Noingorecase

Off ignoring case;

: [range]s/ string A/ string b/[ parameter]

Replace string A with string b

Range: 1,4----> First row to line fourth

$------> Last line

%------> Full text

Parameters: C----> Ask before each replacement

E----> Do not display error messages

G----> Replace all occurrences in a row

Turn on ignore case:

Replace Windows with CentOS (Note that Windows is lowercase):

Interactive Enquiry:

Replace succeeded:


1.5 vim for the programmer module

: Syntax on

Turn on grammar checking

: Set Autoindent

Auto indent with default of 8 spaces

: Set shiftwidth=4

Set indent characters

Note: You can edit the configuration/etc/vimrc to make certain programmer-specific features automatically take effect



1.6 Other shortcut commands

h/j/k/l

Control cursor movement (lower left upper right)

^

Move the cursor to the beginning of the line

$

Move the cursor to the end of the line

G

Move the cursor to the end of the file

Gg

Move the cursor to the file header

CTRL + F

Page Down

CTRL + B

Page UP

U

Undo Action

X

Delete Content at cursor location

Dd

Delete the cursor in the row

D

Delete the cursor at the end of the line where it is located

Yy

The copy cursor is in the row

P

Pastes the copied or deleted content at the cursor location


Second, Shell Getting started with programming

2.1 Shell the concept and function of

Shell is a Linux system for user commands to connect with the Linux kernel software, is a command interpreter, to recognize the user input of various commands, and passed to the operating system. The Shell version that most Linux versions use by default is bash.


2.2 Shell script

shell script, strictly called BASH programming, relies on the Vim editor, as text saved on Linux, must end with. SH Shell interpreter support variables, command line arguments, interactive input, function modules and flow control statements, etc.


2.3 Shell Programming

2.3.1 category

1) Interactive: Each command entered by the user, the results are displayed on the screen

2) Non-interactive: The results are not displayed on the screen (output redirection) after each command entered by the user

3) Batch processing: The user writes the complete shell script one time after the execution


2.3.2 Execution Mode

1) Mode 1: Add x permissions to the script and execute it in absolute or relative path + script file (because by default all commands are looking for a command through path, and no path is found through the PATH variable)

[email protected] ~]# cat test.sh

Seq 3

[email protected] ~]# ll test.sh

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

[[email protected] ~]# chmod u+x test.sh//Give X permission

[email protected] ~]# ll test.sh

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

[Email protected] ~]#./test.sh

1

2

3

2) Mode 2: Use bash/sh command + script file (no x permission)

[[email protected] ~]# chmod u-x test.sh//Cancel X permission

[email protected] ~]# ll test.sh

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

[Email protected] ~]# bash test.sh

1

2

3


2.3.3 Position Parameters

$

The name of the script

$n

Nth position parameter

$#

Total number of positional parameters

$*

All positional parameters

[Email protected] ~]# bash test.sh Hello World

This script is test.sh///$0

The parameter number is 2,they be Hello World

The first is Hello///$1

The second is World///$2


2.4 determine the user's parameters

1) Two kinds of forms:

①test command: Test parameter + expression

②[] Command: [expression 1 parameter Expression 2]

2) Results:

condition, return number 0; not set, return (not 0) random number; save in $?

2.4.1 file Test

Used to determine if a file exists and what type it is

Operator

function

-D

Test whether the file is a directory type

-E

Test whether the file exists

-F

Determine if it is a generic file

-L

Determine if the file is linked

-R

Tests whether the current user has permission to read

-W

Tests whether the current user has permission to write

-X

Tests whether the current user has permission to execute

[[email protected] ~]# ls

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

Desktop Install.log Pictures test.sh

Documents Install.log.syslog Public Videos

[Email protected] ~]# test-f music/

[[email protected] ~]# echo $?

1

[Email protected] ~]# [-W test.sh]

[[email protected] ~]# echo $?

0


2.4.2 logic Test (with, or, not)

1) "and &&": the expression on both sides of the symbol is true

[Email protected] ~]# [-D Music] && [-W test.sh]

[[email protected] ~]# echo $?

0

[Email protected] ~]# [-E Music] && [-W test.sh]

[[email protected] ~]# echo $?

1

2) "or | |" : The expression on both sides of the symbol has a set up that is true

[Email protected] ~]# [-e Music] | | [-W test.sh]

[[email protected] ~]# echo $?

0

3) "Non-!" : Inverse of an expression

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

Root

[[email protected] ~]# echo $?

0


2.4.3 integer value Test (compare size, cannot use > < and = )

function

-ge

TD width= "312" valign= "top" > TD width= "246" valign= "top" >

operator

-eq

equals

-ne

is not equal to

-gt

is greater than

-lt

is less than

-le

is equal to or less than

is greater than or equal to

[Email protected] ~]# a=10

[Email protected] ~]# b=100

[Email protected] ~]# if [$a-lt $b]; Then echo "true"; Fi

True


2.4.4 string Test (compare differences)

Operator

Role

=

Compare strings for the same content

!=

Compare strings for different content

-Z

Determines whether the string contents are empty

[[email protected] ~]# ls

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

Desktop Install.log Pictures test.sh

Documents Install.log.syslog Public Videos

[[Email protected] ~]# [-Z $test. Sh]

[[email protected] ~]# echo $?

1

[Email protected] ~]# echo $test. sh

. sh


2.5 Process Control Statements

2.5.1 If Statement-- Judgment Statement

1) used to judge a conditional expression to execute a statement that satisfies a conditional expression

2) Grammatical structure: If-then-fi,if-then-else-fi,if-then-elif-then-else-fi

[email protected] ~]# cat ping.sh

#!/bin/bash

Ping-c 4-i 0.2-w 3 $ &>/dev/null

If [$?-eq 0]

Then

echo "Host is Online"

Else

echo "Host is offline"

Fi

[Email protected] ~]# bash ping.sh 192.168.100.1

Host 192.168.100.1 is online

[Email protected] ~]# bash ping.sh 192.168.100.2

Host 192.168.100.2 is offline

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

[email protected] ~]# cat grade.sh

#!/bin/bash

Read-p "Enter your Score (0-100):" GRADE

If [$GRADE-ge];then

echo "The Grade is excellent!!"

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

echo "The Grade is pass"

Else

echo "The Grade is failure"

Fi

[Email protected] ~]# bash grade.sh

Enter your Score (0-100): 88

The grade is excellent!!

[Email protected] ~]# bash grade.sh

Enter your Score (0-100): 59

The grade is failure


2.5.2 for Statement-- Looping Statements

1) According to the list of values, loop to execute the command, loop thoroughly and end

2) Grammatical structure:

For variable in {list}

Do

Loop body

Done

[email protected] ~]# cat User.txt

Tom

Jack

Lilei

Lucy

[email protected] ~]# 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

[Email protected] ~]# bash for.sh

Tom exists

Jack,create success

Lilei,create success

Lucy exists


2.5.3 while Statement-- Looping Statements

1) for the condition judgment, when the condition is not satisfied, the end of the command!

2) Grammatical structure:

While expression

Do

Loop body

Done

[email protected] ~]# 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 Statement-- Judgment Statement

1) used to judge a conditional expression to execute a statement that satisfies a conditional expression

2) Grammatical structure:

Case variable in

Conditional expression 1)

statement;;

Conditional expression 2)

statement;;

*)

statement;;

Esac

[email protected] ~]# 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

[Email protected] ~]# bash case.sh

Please input a char:9

This is a num

[Email protected] ~]# bash case.sh

Please input a char:h

This is a char

[Email protected] ~]# bash case.sh

Please input a char:!

This is a special char

Vim Editor-shell script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.