How to batch comment and cancel comment in shell

Time:2021-1-21

#!/bin/bash
echo "1"
echo "2"
echo "3"
echo "4"
echo "5"
echo "6"

The shell above will print 1-6 in turn

Suppose we want to print only 5 and 6, and we want to comment out 1-4, except for the normal practice of adding # in front of each line

1、 Add comments manually

We can also use a clever way, or we can write the following


#!/bin/bash
:<<EOF
echo "1"
echo "2"
echo "3"
echo "4"
EOF
echo "5"
echo "6"

A colon is a command in the shell, which means to do nothing

in addition

: > data.log Equivalent to cat / dev / null > data.log It can clear the contents of the file

You can also append 1-4 input redirection to the black hole


#!/bin/bash
cat >/dev/null<<EOF
echo "1"
echo "2"
echo "3"
echo "4"
EOF
echo "5"
echo "6"

2、 Batch annotation with VIM

1. Block selection mode

insert Comment
First, open the script we want to operate with vim,

Then move the cursor to the first line we want to operate (here we move to the left of echo “1”) and press V to enter the — visual — mode

Then use the up and down keys to select the number of lines to be commented (here we use the down key to move to the line echo “4”)

Then press Ctrl + V (Ctrl + Q under win) to enter column mode

Press capital “I” to enter the insert mode, enter the comment character “#” or “/ /”, and then immediately press ESC (twice)

The final effect is as follows

note off

Enter the block selection mode with Ctrl + V and select the comment symbol at the beginning of the line you want to delete. Note / / you need to select two,

After selecting, press D to delete the comment

Method 2: replace the command

Batch comments:

Use the following command to add a comment at the beginning of the specified line:

: start line number, end line number s / ^ / annotator / g

Here we are going to operate lines 2 to 5, so after opening the script with VIM, enter

:2,5s/^/#/g

Then press enter to complete the replacement

Uncomment:

: start line number, end line number s / ^ annotator / / g

Here we use

:2,5s/^#//g

enter

The above is the details of how to batch comment and cancel comment in shell. For more information about shell comment, please pay attention to other related articles in developer!

Recommended Today

Video compatibility in wechat

1. In line properties of video tag SRC: URL of video Poster: Video cover, no picture displayed when playing Preload: preload Autoplay: autoplay Loop: loop playback Controls: browser’s own control bar Width: video width Height: video height style=”object-fit:fill” /Adding this style will make the Android / Web video full screen in wechat. If it is […]