Vim editor and Shell script

Vim editor and Shell script

Three modes of Vim

Command mode: control cursor movement, copy, paste, delete and search text.
Input mode: normal text entry
Last line mode: save or exit the document and set the editing environment.

Commands commonly used in command mode

dd delete(shear)Whole line of cursor
5dd delete(shear)Line 5 from the cursor
yy Copy the entire line of the cursor
5yy Copy line 5 starting at the cursor
n The next string to which the search command locates
N The last string to which the search command navigates
u Undo the previous step
p Delete previous(dd)Or copy(yy)Pasted data behind the cursor

Commands available in last line mode

:w preservation
:q sign out
:q! forced return(Discard changes to the document)
:wq! Force save exit
:set nu set number 
:set nonu set nonu 
:Command executes the command
:Integer jump to this line
:s/one/two The first line of the current cursor one replace with two
:s/one/two/g All of the lines in which the current cursor is located will be displayed one replace with two
:%s/one/two/g All in the full text one replace with two
?String searches the text for the string from bottom to top
/String searches the text for the string from top to bottom

Simply write text

Once you enter the vim editor, the command mode is used by default. You can use the a, i and o keys to switch from command mode to input mode Switch to last line mode.
In the output mode, to switch other modes, you need to press Esc first to switch commands.

Configure host name

Host name location / etc/hostname

ji@ji-virtual-machine:~$ vim /etc/hostname

Configure network card information

Location of network card configuration file:
The network card configuration of ubuntu is unknown
It should be placed under the etc folder

await a vacancy or job opening

Configuring the Yum software warehouse

# ubuntu does not support the writing of
ji@ji-virtual-machine:~$ cd /etc.yum.repos.d
bash: cd: /etc.yum.repos.d: There is no such file or directory

### Specific vim preparation content
[rhe17] # Unique identification of Yum software warehouse
name=rhe17# The name and description of Yum software warehouse is easy to identify the purpose of the warehouse
baseurl=file://media//cdrom # provides HTTP, FTP and file (local)
enabled=1 # Set whether this source is available, 1 is available and 0 is disabled
gpgcheck=0 # Set whether this source is a verification file, 1 is verification, 0 is no verification.
## Specific code
root@ji-virtual-machine:/etc/yum/repos.d# vim rhel7.repo
root@ji-virtual-machine:/etc/yum/repos.d# mkdir -p /media/cdrom
root@ji-virtual-machine:/etc/yum/repos.d# mount /dev/cdrom /media/cdrom
mount: Block device /dev/sr0 Write protected, will mount as read-only
root@ji-virtual-machine:/etc/yum/repos.d# vim /etc/fstab # I won't set it here
root@ji-virtual-machine:/etc/yum/repos.d# yum install httpd
file://media//cdrom/repodata/repomd.xml: [Errno 14] Could not open/read file://media//cdrom/repodata/repomd.xml
 Try another mirror.
Error: Cannot retrieve repository metadata (repomd.xml) for repository: rhe17. Please verify its path and try again
root@ji-virtual-machine:/etc/yum/repos.d# 

Writing Shell scripts

Two operating modes:
Interactive and batch processing.

The shell uses Bash command line terminal interpreter

root@ji-virtual-machine:/etc/yum/repos.d# echo $SHELL
/bin/bash

Writing shell scripts

The. sh file is the script file, which can be written in vim
Simple example:
View the current working path and list all files and attribute information in the current directory
vim example.sh

! Is a script declaration

Is a script comment

!/bin/bash

pwd
ls -al

Execute script

root@ji-virtual-machine:/etc/yum/repos.d# bash example.sh
/etc/yum/repos.d
Total consumption 20
Drwxr-xr-x 2 root 4096 December 1 16:41
Drwxr-xr-x 3 root 4096 April 17 2021
-Rw-r -- R -- 1 root 23 December 1 16:41 example.sh
-Rw-r -- R -- 1 root 69 December 1 16:28 rhe17.repo
-Rw-r -- R -- 1 root 69 December 1 16:30 rhel7.rep

### Another method of execution
 Output the complete path to execute

root@ji-virtual-machine:/etc/yum/repos.d# ./example.sh
Bash:. / example.sh: insufficient permissions

However, the permission is often insufficient. You can add execution permission for the file. See the next chapter
### Receive user's parameters
$0 Corresponding to current shell The name of the script, $#Corresponding to a total of several parameters, $* corresponds to all unknown parameter values, $? The corresponding value is the return value of the execution of your hi last command$ 1 $2 corresponds to the nth unknown parameter value respectively

Write parameters like this, but the output does not come

echo"fengexian=======fengeixan"
echo"This is $0"
echo"all $# ,they ary $*"
echo "The first is $1 ,Second is $6"

Execute command statements; This is true with bash or sh

root@ji-virtual-machine:/etc/yum/repos.d# sh example.sh one three
example.sh: 4: example.sh: echofengexian=======fengeixan: not found
example.sh: 5: example.sh: echoThis is example.sh: not found
example.sh: 6: example.sh: echoall 2 ,they ary one three: not found
The first is one ,Second is

### Judge user parameters
 Test statement format:[ Conditional expression ] []There are spaces inside
 By test object:
File test statement
 Logical test statement
 Integer value comparison statement
 String comparison statement
#### Parameters used for file testing

-d test whether the file is of directory type
-e. does the test file exist
-f judge whether it is a general document
-r test whether the current user has permission to read in
-w test whether the current user has permission to write
-x test whether the current user has permission to execute

example

-If the return value of d is 0, the directory exists, and if it is 1, it does not exist

root@ji-virtual-machine:/etc# [ -d etc/fastdb ]

Shell interpreter's built-in $? Variable displays the return value after the execution of the previous command

root@ji-virtual-machine:/etc# echo $?
1

-The same is true for f, which acts on the document

#### Logical statements perform logical analysis on the test results
&&If you succeed in the front, you will execute the back. Similarly, when judging, if the front is False,Then you don't have to look at the back.
Logical or||
Logical non!
#### Available integer comparison operators

-Is eq equal to
-Is ne not equal to
-gt is greater than
-lt is less than
-Whether le is equal to or less than
-Is ge equal to or greater than

example

root@ji-virtual-machine:~# [ 10 -gt 10 ]
root@ji-virtual-machine:~# echo $?
1
root@ji-virtual-machine:~# [ 10 -et 10 ]
bash: [: -et: expecting binary expression
root@ji-virtual-machine:~# [ 10 -gt 10 ]
root@ji-virtual-machine:~# echo $?
1

### Instance to view the current memory remaining

await a vacancy or job opening

### Common string comparison operators

=Equals
! = not equal to
-z judge whether the string content is empty

Instance to judge the environment variables of the current language family

root@ji-virtual-machine:~# echo $LANG
zh_CN.UTF-8
root@ji-virtual-machine:~# [ $LANG!="en.US" ]&& echo "NOt en.US"
NOt en.US

### Process control statement
if Conditional test statement

await a vacancy or job opening

for Circular statement

await a vacancy or job opening

while Conditional loop statement

await a vacancy or job opening

case Conditional test statement

await a vacancy or job opening

### Scheduled task service procedure
####Planned tasks are divided into one-time planned tasks
 Execute only once, use " at The form of "time" is OK
 View scheduled tasks, unexecuted"at -l"
Delete, you can use" atrm Task serial number"
use"at xx"The command terminal adopts interactive method by default

root@ji-virtual-machine:~# at 23:30
warning: commands will be executed using /bin/sh
at> echo "hello world"
at>
job 1 at Wed Dec 1 23:30:00 2021
root@ji-virtual-machine:~# at -l
1 Wed Dec 1 23:30:00 2021 a root

Delete task

root@ji-virtual-machine:~# atrm 1

### Long term planning tasks
 use crond service(linux Enabled by default),
Check“ crontab -l"
delete " crontab -r"
If the administrator logs in, you can use-u Parameter to edit others' scheduled tasks
 Time sharing day month week
crond Set the parameter field description of the task

Score 0 ~ 59
0 ~ 23 hours
Day 1 ~ 31
January to December
Any integer from 0 to 7, where 0 and 7 are Sundays

example
corontab -e Create scheduled task
 But I can't get out after I enter; there may be no path to execute the command.

root@ji-virtual-machine:~# crontab -e
no crontab for root - using an empty one

Select an editor. To change later, run 'select-editor'.

  1. /bin/ed
  2. /bin/nano <---- easiest
  3. /usr/bin/vim.basic
  4. /usr/bin/vim.tiny

Choose 1-4 [2]: 1
888

Time sharing days, months and weeks are separated by spaces
 However, commas can be used to represent multiple time periods, and minus signs can be used to represent a continuous period of time(12-15 Can be used to represent 12 months of a month-15 number)
Divide by(/)Indicates the interval between tasks(*/2 Indicates execution every 2 minutes)
When planning a task, the command path must be written as an absolute path, whereis View the path.

Posted by crisward on Wed, 01 Dec 2021 06:26:02 -0800