BASH command and usage summary, bash usage Summary

Source: Internet
Author: User

BASH command and usage summary, bash usage Summary

I need to write a Linux script in my recent work and use a lot of BASH commands. To avoid forgetting it later, I will write them down here one by one. It may be messy. Just take a look. If something is wrong, you are welcome to correct it.

1, export VAR =...

This command runs in Shell to make the script that runs later know the VAR. However, if this command is run in a script, parameters other than the script are not affected. For example, if the environment variable VAR is not present before a script is run, and the command export VAR = 3 is run in the script, after the script is complete, if echo $ VAR is run, no display is displayed. This indicates that export is only valid in the script.

2, if...; then...; else...; fi

You can write the judgment in different rows, so that no ";" is required. If followed by [], there is an expression in the brackets, and a space must be left at the beginning and end of the expression and the brackets. An example is used to judge whether VAR is null:

If [-z "$ VAR"]; then echo "empty"; else echo "not empty"; fi

Note that the two ends of $ VAR must be enclosed in double quotation marks.

3. Double quotation marks and single quotation marks

When it comes to double quotation marks, the difference between it and single quotation marks is that if there is a variable between them, you must use double quotation marks. BASH does not detect the content in single quotes, that is, there are no special characters in single quotes. The output of echo '$ VAR' is $ VAR, and there is no problem in using double quotation marks in single quotes. On the contrary, if you need to print special characters in double quotes, you need to use '\' to escape them. For example, echo "$ VAR" outputs null characters, while echo "\ $ VAR" outputs \ $ VAR.

4, $ ()

If you need to assign the return values of one or more commands to a variable, you can use $ () and put the commands in parentheses. For example:

LINES = $ (cat filename | wc-l)

5 ,#! /Bin/sh

I almost forgot about this important one. This is the first line of the script file. It indicates that the script is run using/bin/sh. It seems that some people like to ask questions during the interview... Then comment out a line in the script. I wonder if there is any comment on multiple rows...

6, sed's/abc/CBA /'

Sed can be used to search and replace regular expressions, either for text files or for string variables. Here I mainly use string variables, so the general format is as follows:

TEMP = $ (echo $ SVAR | sed's/. * \ (regex \). */\ 1 /')

In this way, you can find the corresponding regex content in the variable SVAR and extract it. The \ (and \) on both sides of the regex are used to form a group for the content of the regex, and \ 1 is used to replace all content of the SVAR with the content of the first group, that is the part that regex can match. If the part corresponding to regex is not found, all content of SVAR is returned.

7, "$ {VAR/" substring "}" = "$ VAR"

The preceding equation can be used to determine whether substring exists in a string. If they are equal, it indicates that they do not exist. The left side of the equation returns the substring starting with the first substring found. The original string is returned only when the substring does not exist.

8, curl

Curl is a very powerful command used to send http requests. I only used some parameters, such

-S indicates silent running, without displaying statistics such as the speed, completion status, and error of the curl command

-S is usually used together with-s, indicating that an error is displayed.

-K when an https request needs to be sent, add this to allow insecure requests

-X is followed by the request method, such as POST, PUT, and DELETE. If it is GET, it will not be used, because this is the default operation.

-H is followed by the header parameter. A curl command can use multiple-H to define multiple parameters.

-D followed by the body, must start and end with single quotes

Therefore, a curl command may look like this:

Curl-sS-H 'content-Type: application/json'-d' {"name": "'$ VAR'"} 'http://www.where.com

Note that if a variable is used in the body, you need to end the control range of the previous single quotation mark with a single quotation mark first, and then use a single quotation mark after the variable to correspond to the following single quotation marks.

9. definition method

Function can be defined in the script. The specific form can be:

Func (){

...

}

During definition, you do not need to specify the input parameters, because you can directly obtain and use the parameters following this method or command in the method. For example:

Func abc 30

Then $1 is abc and $2 is 30. If you have more input parameters, you can use $3, $4 ,... Keep going.

Methods can also return values, such

Func (){

...

Return 35

}

Then after func abc 30 is run, $? The memory is 35.

10, tr

It can be used to split a character in a string into another character. For example, there is a string SVAR = "a, B, c ". Run the following command:

Arr = $ (echo $ SVAR | tr "," "\ n ")

You can convert a string into three strings.

11. String operations

Add characters to the end of the string: SVAR = $ SVAR 'abc'. Then, you can add three abc characters to the end of the original string of SVAR.

Delete the last character at the end of the string: SVAR =$ {SVAR % ?}

12, case "$ VAR" in

A)

...

;;

B)

...

;;

*)

...

;;

Esac

This is similar to the switch in C language, which performs different operations on different content in VAR.

 


In Linux bash, how does one use the command alias?

If you frequently use a command with the same parameters, You can have bash create an alias for it. This alias combines the required parameters, so you do not need to remember them or manually enter them each time. For example, if you frequently use ls-l to obtain a detailed directory list, you can use this command:

Bash> alias ls = 'LS-l'

In this way, an alias that automatically contains the-l option is created. Once this alias is created, input ls at the bash prompt to call this alias and output ls-l.

You can obtain a list of available aliases by calling aliases without any parameters. You can also use the unalias command to delete aliases.

Bash shell Command

At ---- schedule, check, and delete jobs in the queue
Cat ---- connect the file and print it to the standard output
Cd ---- Change directory to be able to process absolute and relative paths
Chmod ---- change the file protection permission
Chown-change the owner and group of Files
Clear ---- clear the screen (like cls in DOS)
Cp ---- copy an object
Crontab-operate the daemon and execution schedule of each user
Date ---- print or set the system date and time
Dd ---- copy an object (and convert it at the same time)
Df ---- report the remaining disk space
Du ---- report disk space usage
File ---- test file and directory types
Find ---- search for files in a directory
Grep ---- search for matched rows in the file and Output
Gzip-compress or expand a file
Head ---- display the first few lines of the file
History ---- display historical Command records
Kill ---- abort a process
Last ---- shows how many users or terminals have logged on to the machine
Less ---- you want to use the more command to display files by PAGE
Ln ---- establish a connection Between Files
Ls ---- list files in the current directory
Man ---- display online manuals with certain formats
More ---- the command can read the text page by page
Mount ---- install a File System
Mv ---- rename the file
Passwd ---- set the User Password
Ps ---- command to list Running Processes
Reboot ---- restart the system
Rm ---- command used to delete files
Su ---- create and edit Unix archive files
Tar ---- create and edit Unix archive files
Touch ---- change the time parameter of the file
Wc ---- count the number of bytes, number of words, and number of rows in a file
Who ---- view other login users
Which ---- which -- display the full path of the command
Ping ---- command-determine whether the machine on the network can reach or reach the speed
Multi-task
Output redirection
Understanding processes

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

In Linux, there are two operation interfaces: X Window mode (KDE by default) and Console mode (Console ). The two interfaces are similar to Windows and DOS.

If you want to change to Console mode in X Window mode, you only need to press Alt + F1 (or F2... F6) key. A new waiting window will appear on the screen. You can log on to the system again in Console mode. If you want to return to X Window mode in Console mode, press Ctrl + Alt + F7 at the same time.

If the X Window mode is not enabled and you want to start the X Window mode from the console mode, enter startx (or kdm) in the Console mode to start the X Window.

In the above two chapters, we have introduced the operation of the X Window (KDE by default). In this chapter and the next chapter, we will mainly discuss the operations in the Console mode, the corresponding KDE operation will also be inserted between them.

6.1 start and close the system
Start the system
If you have installed LILO during the installation process, you can start the system. When LILO is started, the screen displays:

LILO:

Click the Tab key to display all operating systems that can be started. Enter the name of the displayed operating system to start the corresponding operating system. Type Linux to start BluePoint Linux & 127. If you press the Enter key directly or wait for 5 seconds, you can also start the BluePoint Linux & 127; operating system.

Another way to start linux is to run the command in DOS/WINDOWS... the remaining full text>

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.