The great God teaches you how to use the shell

Source: Internet
Author: User

The shell is used to solve the problem of how users communicate with the operating system.
  
In fact, we all have a misunderstanding of the concept of shell, as if the command line shell is called shell.
  
The shell, in fact, is the shell.
  
As the name implies is a layer of shell outside the machine, for human-computer interaction, as long as the interface between people and computers, it can be called the shell.
  
So we are familiar with the GNOME, KDE and other graphical interface is the shell, but the GUI shell. So, like bash and other Shell invented the reason of course it is easy to understand, is for the human and machine interaction between the problem, but then the technology can not make a GUI, so it made the command line form.
  
The kernel kernel of Linux, there are many kinds of shells,
  
Command-line: Bash, sh, csh, Ksh
  
Graphical: KDE/GNOME/CDE/XFCE
  
It is said that the Linus are only responsible for the development of nuclear.
  
The shell can also be interpreted as--command interpreter.
  
Now teach everyone to write simple shell scripts
  
The way you write shell scripts is very simple. Just use a common text editor like Vim to create a file and then enter the command in the file. For example, if you want to see the current working path and list all the file and attribute information in the current directory, the script that implements this function can be written.

  
Well, let's explain the meaning of this one.
  
The first line of #!/bin/bash represents the script declaration, which tells the system which command interpreter to use to execute the script.
  
The second line # is the comment information. If you have studied C or other programming languages, you should be able to learn it quickly. The comment information is not executed. is an explanation of the functionality of this script. The ability to see the script when it's convenient for others to view it.
  
The following is the Linux command, will not repeat it. Other than that. Linux does not later prefix the file type. It can also be said that Linux does not have a suffix name concept. So the script name of the. SH is a conventional rule that indicates that this is an executable script.
  
A well-written script file can be executed by bash. The results are as follows

  
In addition to using bash in this way, you can also use "./" to execute. However, there may be an error indicating insufficient permissions. This way, you first use the chmod command to execute permissions on the script. The knowledge of the permission section is not mentioned here. Here you just need to know another way to execute the script.

  
Receive User's parameters
  
The simple shell script above can only perform some pre-defined functions. Sometimes you need to receive input from the user in order to better meet the needs.
  
In the shell language, there are variables that are used to receive parameters. Spaces can be used between variables. For example, 0 represents the name of the current shell script, and 0 represents the name of the current shell script, #对应的是总共有几个参数. ,? corresponding to the parameter values for all locations, corresponding to the parameter values for all positions, corresponds to the return value of the previous command execution. 112 3 ... 3 ... n represents the first, the second, the third .... The value of the nth parameter
  
Try to write a script example that looks at the actual effect by referencing the variable argument above:

  
Determine the user's parameters
  
When the system executes the mkdir command, it will judge the information entered by the user, that is, whether the user-specified folder name already exists, if there is an error, and the other is automatically created. The conditional test syntax in the shell script can determine if the expression is true, and returns the number 0 if the condition is true, otherwise it returns other random values.
  
The execution format of the conditional test syntax is as follows:
  
[Conditional expression] Note that there must be spaces on both sides
  
According to the test object to divide, the condition test statement can divide into 4 kinds
  
1. File test statements
  
2. Logical test statements
  
3. Integer value comparison statement
  
4. String comparison statements
  
A file test is an operator that uses a specified condition to determine whether a file exists or if the permission satisfies the condition. The parameters are as follows:

  
Use the file test statement below to determine if/etc/fstab is a directory-type file, and then through the shell interpreter, the $? variable displays the return value after the previous command was executed. If the return value is 0, the directory exists, or if the return value is nonzero, it means that the directory does not exist:

  
Then use the file test statement to determine whether/etc/fstab is a generic file, and if the return value is 0, the file exists and is a generic file:

  
When one day, the machine control all mankind, human completely become the slave of the machine, I think the shell can be replaced, should say that does not exist, because the machine will call its own function directly. At that time, they would do many kinds of shell to communicate with the human brain, to tell the human at a certain moment, with what behavior, accomplish what, achieve what effect.
  
Of course, the best shell is human language in the case of machine-controlled human beings. In turn, the shell is, in fact, what humans tell the machine at some point, what it does, what it does, and what it does, except that the operating system is made up of the basic elements of the file. Therefore, the shell is to find the required files, that is, what behavior to use.
  
And a certain moment, the completion of what can be understood as parameters, to achieve the effect depends on the user's familiarity with a variety of commands, because a number of commands (a number of files with a certain function) can be used flexibly combined, such as the Linux pipe character can be used to achieve a combination of multiple commands.
  
The end of this sharing, Kim Shi-run Day will not be regularly launched a variety of technical dry to welcome you to exchange Learning!

  
  
This article Lee Xu
  
Hcie-r&s, Ccie-r&s
  
Good at routing, security, wireless and other technologies
  
Hubei Vocational College Computer Network Competition award-winning instructor
  
Main technical direction Cisco and Huawei
  
Courses taught: HCIE, CCNA, CCNP, HCNA, HCNP, etc.

The great God teaches you how to use the shell

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.