Command Line usage, command line usage

Source: Internet
Author: User

Command Line usage, command line usage

(1) $ indicates to reference the variable in the command. You can set the variable in export.

For example, run echo $ PATH to view environment variables.

Here, the PATH variable is referenced through $ (this is a global variable)

For example:

D

When we directly press a command, bash searches for the program in the PATH recorded in the PATH variable. To run a self-installed binary program, you must first add the path of the command.

(2) man command

Commands specific to other commands during Man command

2. Use shell in this way

2.1 understand the directory structure

  • You can press Ctrl + Alt + F1 to open the terminal and press Ctrl + Alt + F7 to return to the graphic interface.

2.2 old account-history command

You can use this command to view commands that have been entered a long time ago. The first 1000 commands are displayed by default. You can also specify the number of command lines to display.

History N

Note: The history command is not an independent command like the ls command. It is a bash keyword. That is to say, the history command is only useful in a specific version of bash.

2.3 more or less -- display the command on pages

[More]

More is a command used to read text. It has the paging function. You can first display a screen from the file or the Retrieved Data Stream. When you press the Enter key, the next screen is displayed until the display is complete. Of course, you can simply press q to launch it after the display is complete. For example, to view all processes currently running:

Ps-A | more

Note: The preceding command has a "|" symbol. This symbol is called a pipe operator. It refers to handing over the output content of the previous command to the next command as the data input of the next command. The above command is to give the heap of ps-A output to the more command, and then the more command is displayed to the user after processing.

[Less]

The less and more commands provide the same functions, but the less command is more powerful than the more command. You cannot go back and view the items you have seen before the more command. You can only go down to the page to view them. The less command can be viewed by the up and down arrows, and will not be automatically launched after reading the command. You must press the q key to exit.

Note: You can directly view the file using both commands.

Less/file path/file name

2.4 wildcard characters

Linux uses "*" to represent any character ,"? "Represents any character.

3. shell programming

3.1 package and execute the command line

Scenario:

Insert an SD card into your computer and run the mount command to mount it:

Sudo mout/dev/sdb1/mnt/

Then copy all the pictures in it.

Cp/mnt/*. jpg/home/zhanglu/Picture

Then, compress these photos in the same directory for backup:

Tar-czvf/home/zhanglu/Backup/pic.tar.gz/mnt/*. jpg

After the backup is complete, delete all files in the card:

Rm/mnt/*. jpg

Finally, uninstall the SD card:

Sudo umount/mnt/

As shown above, You have to input the above several commands each time you copy a photo, which is troublesome. Is there a simple batch processing method?

[Advanced batch processing-shell script]

It is very easy to write a shell script. Find an editor and copy the above commands in order to save them. So we write a file like this:

Sudo mout/dev/sdb1/mnt/

Cp/mnt/*. jpg/home/zhanglu/Picture

Tar-czvf/home/zhanglu/Backup/pic.tar.gz/mnt/*. jpg

Rm/mnt/*. jpg

Sudo umount/mnt/

And save the file with the name dailybackup. sh. So far, this script cannot be run, because we have not given it the permission to run. Execute the following command to grant it the permission to run:

Chmod + x./dailybackup. sh

At this time, the script can be run.

Note: shell scripts do not require a specific extension, as long as they are text files and have executable permissions. But we generally end with sh.

[Flexible shell script]

The script is complete, but what hurts is that the file names of the backups I pack every day may be different, but the script is completely written. In this case, you need to modify the script.

Sudo mout/dev/sdb1/mnt/

Cp/mnt/*. jpg/home/zhanglu/Picture

Tar-czvf/home/zhanglu/Backup/pic 'date policyuncd'.tar.gz/mnt/*. jpg

Rm/mnt/*. jpg

Sudo umount/mnt/

This script uses the "'" symbol. The "'" symbol indicates that two "'" commands are executed first, can I use the output after execution to replace the content between two "'", including "'" itself.

Note: Here "'" is not a single quotation mark "'", but on the keyboard and "~ "The quotation marks are placed in the back of the same key.

The two quotation marks in the above script are used to print the current time in the format of year, month, and day.

3.2 standard shell script

(1) make necessary comments. Use "#" in shell scripts as the beginning of the comments.

(2) Specify the shell used

Sometimes we can see that the beginning of a shell script is like this:

#! /Bin/bash

"#! "Put together and appear in the first line of the script, it is not a comment! Run the shell required by the script in this line.

In our linux system, there are many shells, such as bash, tcsh, and ksh. These different shells have different syntaxes and features. Therefore, you must specify the shell to use when running the script.

Of course, you can also skip this Code. At this time, you need to tell the system the shell you used. For example, to run with bash, you need the following command:

Bash./dailybackup. sh

3.3 Use Functions

Like most programming languages, Shell scripts also support functions. If a piece of code needs to be repeatedly executed in the script multiple times, you do not have to write multiple copies repeatedly. You just need to write them as a function and call them directly when necessary. For example, the automatic backup script:

#! /Bin/bash

# Automatic backup script

Dailybackup (){

Udo mout/dev/sdb1/mnt/

Cp/mnt/*. jpg/home/zhanglu/Picture

Tar-czvf/home/zhanglu/Backup/pic 'date policyuncd'.tar.gz/mnt/*. jpg

Rm/mnt/*. jpg

Sudo umount/mnt/

}

Dailybackup # Do not write parentheses when calling a function

3.4 use variables in shell scripts

(1) User variables-new users

The variables in Shell are relatively casual and simple. They do not need to be declared and can be written as needed, as shown below:

Value = 28

In this case, we get a variable whose content is 28. How can we reference this variable? When referencing a variable, you need to add the "$" symbol before the variable name to indicate that this is a variable. For example:

#! /Bin/bash

Value = 28

Echo value # output value

Echo $ value # output 28

Therefore, you must add "$" before using the variable. Otherwise, the variable will be processed as a general string.

However, sometimes variable references may be followed by strings, which requires special processing:

#! /Bin/bash

Word1 = hello

Word2 = world

Echo $ word1BIG $ word2

At this time, the output is helloworld, not helloBIGworld. Because the system regards $ word1BIG as a reference of a variable, but we do not have this variable at all, so it will print blank. If we want shell to understand our intention, we can add braces.

Echo $ {word1} BIG $ {word2}

(2) variable type-only strings

In shell scripts, there are no integer, floating point, and other data types, and only strings are available. To perform mathematical operations on numbers, use the expr command.

Num = 1

Num = 'expr $ num + 2'

(3) environment variables-available anywhere

The variables we defined above are user variables. In addition to user variables defined by ourselves, there is also a variable that is a system variable. For example, see the PATH, and HOME variables.

The so-called environment variables are a bit similar to the global variables in C language, that is, they are effective throughout the system. User variables are valid only in one script. Cannot be accessed in other scripts. System variables can be accessed anywhere. To define a global variable, you only need to add the export before the variable:

Tip: The environment variable becomes invalid after the current session ends.

(4) Special variables-a pile of symbols

In addition to user variables and system variables, there are other special variables that have the following features:

  • It looks special.
  • Automatically Set during Script Execution
  • Unchangeable

The special variables are mainly the following:

  • $ N -- Here n is a 0 ~ 9. This variable represents the nth parameter added to the execution script, and n = 0 represents the name of the script itself.
  • $ * -- This variable represents all the parameters added to the execution script (excluding the script name itself)
  • $ # -- Number of variables added to the execution script
  • $ -- PID of the script

3.5 shell condition judgment

(1) if and fi

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.