Command Line usage

Source: Internet
Author: User

Command Line usage
The command line (1) $ symbol represents a reference variable in the command. You can set a variable in export. For example, to view the environment variable command: echo $ PATH, 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 from the PATH recorded in the PATH variable. To run a self-installed binary program, you must first add the path of the command. (2) The man command is used to explain other commands. 2. in this way, you can use shell 2.1 to understand the directory structure and press Ctrl + Alt + F1 to open the terminal, ctrl + Alt + F7 and then return to the graphic interface 2.2-history command. To view commands that have been entered a long time ago, use this command. 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 as independent as 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 -- the paging display of the command [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 functions of the less and more commands are basically the same, except that the less commands are more powerful than the more commands. 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: Both commands can directly view the file less/file path/file name 2.4 wildcard linux uses "*" to represent any character ,"? "Represents any character. 3. shell programming 3.1 package command line execution scenario: insert an SD card into the computer, and then run the mount command to mount: sudo mout/dev/sdb1/mnt/then copy all the photos in cp/mnt /*. jpg/home/zhanglu/Picture and then compress these photos in the same directory for Backup: tar-czvf/home/zhanglu/Backup/pic.tar.gz/mnt /*. after jpg backup is complete, delete all files in the card: rm/mnt /*. jpg finally uninstalls this SD card: sudo umount/mnt/above. Each time you copy a photo, you have to input the above several commands. This 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 the permission to run: chmod + x. /dailybackup. sh then the script can 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 may be different every day, but the script is dead. 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 policy1_m1_d'.tar.gz/mnt /*. jpg rm/mnt /*. jpg sudo umount/mnt/This script uses the "'" symbol. The "'" symbol indicates that the command between two "'" is 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) requires necessary comments. The shell script starts with "#" (2) specify the shell used. Sometimes, you can see that the beginning of a shell script is as follows :#! /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 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 uses function Shell scripts like most programming languages and supports 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 script for automatic backup :#! /Bin/bash # automatic backup script dailybackup () {udo mout/dev/sdb1/mnt/cp/mnt /*. jpg/home/zhanglu/Picture tar-czvf/home/zhanglu/Backup/pic 'date policy1_m1_d'.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 can use variables in Shell scripts, you do not need to declare it and write it as needed. For example: value = 28, a variable with content of 28 is obtained. How can you reference this variable? When referencing a variable, you need to add the "$" symbol before the variable name to indicate that this is a variable. Example :#! /Bin/bash value = 28 echo value # output value echo $ value # output 28. Therefore, you must add "$ ", otherwise, it 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 word1BIGword2 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. To let shell understand our intention, we can add the braces echo word1BIG {word2} (2) to the variable type. Only strings in shell scripts do not have integer, floating point, or other data types, only strings exist. If you want to perform mathematical operations on numbers, use the expr command num = 1 num = 'expr $ num + 2' (3) environment variables-the variables defined above can be used everywhere. 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. Note: The environment variable will expire after the current session ends (4) special variables-a bunch of symbols in addition to user variables and system variables, there are other special variables that have the following features: when a special script is executed, it is automatically set to not be modified. 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, 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 $ -- condition judgment (1) if and fi in the pid 3.5 shell of the script

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.