Basic analysis of Linux operating system (vii)--bash basic knowledge and basic usage skills (1)

Source: Internet
Author: User
Tags aliases posix terminates

In everyday English, the shell can be translated into shells, most of which means a device or structure that can protect the inner core. In computer science, the shell actually refers to a provider that can use the entire computer's resources through system calls or library calls.
It is both a command parser and a programming language. As a command parser, it can interpret and execute user-entered commands, and can automatically interpret and execute a series of commands that are pre-written and saved in a text file; As a programming language, the shell defines variables and parameters in particular, and provides many control structures in high-level languages. Includes loops and conditional branching, allowing us to use the shell in the same way as a high-level language, enabling it to handle applications in complex logical environments, improving its execution efficiency, and making it easier to perform its automation features.

Similar to the previous content, there are two concepts: interactive shell and non-interactive shell
The interactive mode is where the shell waits for your input and executes the commands you submit. This pattern is called interactive because the shell interacts with the user. This mode is also familiar to most users: Log in, execute some commands, log off. When you log off the login, the shell terminates the run.
The shell can also run in a different mode: non-interactive mode. In this mode, the shell does not interact with you, but instead reads the commands stored in the file and executes them. When it reads the end of the file, the shell terminates.

The main shell types in "UNIX like" are:
         1. Bourne Shell (SH)
        2. Korn Shell (Ksh)
        3. Bourne Again Shell (bash)
         4. POSIX Shell (SH)
         5. C Shell (CSH)
        6. Tenex/tops C Shell (tcsh)

=============================================================
Bourne Shell
The first important standard UNIX shell was introduced at the end of 1970 at V7 Unix (7th edition), and with its founding science and Technology Foundation platform-"National Meteorological Network Computing application Node Building" (2004dka50730) sponsor Stephen Named after the Bourne's name. The Bourne shell is a switch-type command interpreter and command programming language. The Bourne shell can be run as either the login shell or the login Shell's Child shell (Subshell). Only the login command can invoke the Bourne shell as a login shell. At this point, the shell reads the/etc/profile file and the $home/.profile file first. /etc/profile files are custom environments for all users, $HOME/.profile files for this user-defined environment. Finally, the shell waits for your input to be read.

C Shell
Bill Joy developed the C shell at the University of California in Berkeley in the early the 1980s. It is primarily intended to make it easier for users to use interactive features and to turn the grammatical structure of the ALGOL style into a C language style. It adds features such as command history, aliases, file name substitution, job control, and more.

Korn Shell
For a long time, there were only two types of shells for people to choose from, Bourne shell for programming, and C shell for interaction. To change the situation, the Bell lab David Korn developed the Korn shell. Ksh combines all of the C Shell's interactive features and incorporates the syntax of the Bourne shell. As a result, Korn shell is popular with users. It also adds features such as mathematical calculations, process collaboration (coprocess), inline editing (editing), and more. The Korn Shell is an interactive command interpreter and command programming language. It complies with the POSIX standard (portable Operating system, an international standard for an operating system). But POSIX is not actually an operating system (which should have been referred to as the POS standard, but later in order to cater to UNIX, and specifically added the IX suffix behind the POS, so called the POSIX standard), but a goal is to facilitate the application of porting standards, Enabling applications to be compiled across multiple platforms at the source program level is called possible.

Bourne Again Shell (bash)
Bash is part of the GNU program to replace the Bourne shell. It is used for GNU-based systems such as Linux. Most Linux (Red Hat,slackware,caldera) uses bash as the default shell, and when it runs SH, it actually calls bash, because SH is just a symbolic link to bash.

POSIX Shell
The POSIX shell is a variant of the Korn shell. The largest vendor currently providing POSIX shells is Hewlett-Packard. The HP-UX 11.0,posix Shell is/bin/sh.
=============================================================

Note: The contents of the "=" tag are reproduced in the network.

Bash is the default shell for most Linux systems and Mac OS X, which can run on most UNIX class operating systems and even be ported to the Cygwin system on Microsoft Windows to implement the POSIX virtual interface for Windows.
Bash's command syntax is a superset of the Bourne Shell (SH) command syntax. A large number of SH scripts can usually be executed directly in bash without modification, except for scripts that use special variables of SH or built-in commands. Many of Bash's command syntax comes from Korn shell (Ksh) and C Shell (CSH), such as command-line editing, command history, directory stacks, $RANDOM and $ppid variables, and POSIX command substitution syntax: $ (...).

For any Linux distribution operating system, bash is just an application, but it's a very special application. In Linux, for Bash itself, it is an external command. Let's take a look at the effect of this external command in a nutshell.
BASH: external command, command binary files are stored in the following path:/bin/bash, all called: GNU bourne-again SHell
Use format: Bash [options] [file]
Command Description:
Bash is an SH-compatible command language interpreter that can read and execute commands from a standard input or file. Bash also incorporates some of the useful features of Korn and C shell.
Bash is a consistent implementation of the shell and a utility that follows the IEEE POSIX specification. By default, bash is configured to be consistent with POSIX.


Common options:
-X: Debug executes a command or script detailing the execution process and the returned execution results.
-N: Check for syntax errors in the command or script.

Since the shell is so important and so indispensable, how should we use the shell? In fact, it is very simple to learn to use the shell and you have to understand what features the shell provides to the operator.

Let's take a look at bash as an example to introduce its basic features:
One, the order explanation:
This feature can be said to be any kind of shell or club any interface program must have the basic functions, and even should be said that its innate mission.
So how does bash explain the order?
1. In bash, Bash tries to interpret the command only by typing a carriage return;
2. After typing the carriage return, bash attempts to segment the entire command line with whitespace characters, and Bash will assume that the leftmost segment is the command to be executed;
3.bash after you have identified a string that might be a command, you need to know whether this is an internal command or an external command. The so-called internal command, which is the command that comes with bash, is usually loaded into memory after bash is running, because such commands are part of the bash, and so-called external commands are the commands that are stored in some chunks of external memory by installing other applications. There's no direct relationship with bash. If this is an internal command, bash can run directly, and then return execution results and execution status information, and if this is not an internal command, bash invokes the built-in path variable that holds the executable binary, reading the contents of the variable. To find out if there is a file with the same name as the command executed this time, if any, execute it, if not, it will report an error message such as:-bash:commamd:command not found. The "command" may be replaced with the command being executed. As shown in the following example:

[Email protected] ~]# Sss-bash:sss:command not found

4.bash When judging the type of command and giving an explanation, the other fields in the entire command line are sent as a selection and parameters to the command, and if they are correct, the command executes correctly, and bash outputs the execution of the command to the standard input in the format required by the command. But if such an option or parameter is not correct or is not available, you will also need to treat it in the following two cases:
        1) If it is an internal command, bash will always be in charge to help the command report error messages;
        2) If it is an external command, it is not bash can decide, usually this time, will be executed by the application to determine whether the options and parameters are correct, if not correct, the application will report the error message.
        3) Whether it is an internal command or an external command, even if the external command was launched by Bash, Bash is responsible for whether the command is successful or not. At the end of execution, Bash uses an exit code to identify its execution status information and save that state information to a name of "?". In a special variable. If you want to see this state, we can use the general variable content reading method: Echo $? Can. Generally speaking, the return value of such a command execution state is represented by an integer of 0-255, where:
        (1) 0: Indicates successful command execution
(2) 1, 2, 127: The default error code for bash
(3) 3-126: User-customizable error codes
(4) 128-255: A command quits abnormally, but exits because a signal is received, so the status return value is 128+n, where "N" indicates the number of the signal received by the command

Please refer to the following example:

[Email protected] ~]# cd-a-bash:cd:-a:invalid option CD:USAGE:CD [-l|-p] [dir]

(the option for internal commands is wrong here)

[Email protected] ~]# CD/TESTTT-BASH:CD:/testtt:no such file or directory

(the parameter for the internal command is wrong here)

[[email protected] ~]# echo $? 1

(here is the status return value for the command execution.) In general, if the return value is "0", the command executes successfully or the command execution fails)

[[email protected] ~]# ls--p ls:unrecognized option '--p ' Try ' ls--help ' for more information.

(the option for external commands is wrong here)

[Email protected] ~]# ls/testtt ls:cannot access/testtt:no such file or directory

(the parameter for the external command is incorrect here)

[[email protected] ~]# echo $? 2

(here is the status return value for the external command execution.) In general, if the return value is "0", the command executes successfully or the command execution fails)

Second, the command alias
Bash gives us the privilege of creating aliases for commands, whether it's a system administrator or an ordinary user, and you can define the alias command, and the method is simple:
    1. You can use the alias command to define the aliases command;
2. You can use the Unalias command to cancel an alias command that has already been defined;
3. Whether you cancel using the alias command definition or the Unalias command, the bash run parameter in memory is changed, so as long as bash is off, the feature is invalidated. Therefore, in order to make the alias command permanent, you can write such a definition in the. bashrc file in your home directory, each user has this file in their home, fair trade.
This feature can be very useful in some cases, because depending on this feature, we are able to:
1. Simplify complex commands, such as:

[[email protected] ~]# alias l.= ' ls-d. *--color=auto ' [[email protected] ~]# alias ll= ' ls-l--color=auto ' [[Email Protected] ~]# alias ls= ' ls--color=auto '

2. Secure dangerous commands, such as:

[[email protected] ~]# alias cp= ' cp-i ' [[email protected] ~]# alias mv= ' mv-i ' [[email protected] ~]# alias rm= ' Rm-i '

3. Define different styles of commands according to your preferences and usage habits, such as:

[[email protected] ~]# alias cdnet= ' cd/etc/sysconfig/network-scripts/' [[email protected] ~]# alias viht= ' vim/etc/h Ttpd/conf/httpd.conf '


(To be Continued ...)

This article is from the "home of the Ops" blog, so be sure to keep this source http://zhaotianyu.blog.51cto.com/132212/1783467

Basic analysis of Linux operating system (vii)--bash basic knowledge and basic usage skills (1)

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.