In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
The characteristics of Bash 1. What is shell?
Human-computer interaction interface can be divided into two types: GUI (graphical user interface) and CLI (command line interface).
A command interpreter or shell program is a mechanism through which each interactive user can issue commands to the operating system; the operating system also uses them to return the corresponding results directly to the user. The function of shell is only an interface for users to operate the kernel. After the user successfully logs in to the computer (when the user starts the login process, a process will be assigned to the user), the operating system causes the user process to execute the shell.
The shell text receiver can receive commands typed by the user. The text receiver will first analyze and interpret the typed string, identify some special symbols and deal with them accordingly. Note that it only recognizes wildcards, not regular expressions.
The operating system usually does not have a built-in window interface, but a simple character-oriented interface in which the user enters a string of characters (ending with the "enter key"); and the operating system also outputs lines of characters to the screen as a result. The user logs in to the process to execute shell, first shell analyzes the command line, then according to the setting of the environment variable PATH (will not find the current directory), look for the system file directory, find a file name or the full path name of a file, when the file is found, execute the file according to other parameter lists.
Second, the common shellShell NameA Bit of Historysh (Bourne) The original shell from early versions ofUNIX.csh,tcsh, zsh
The C shell, and its derivatives, originally created by Bill 喜悦 of Berkeley UNIX fame. The C shell isprobably the third most popular type of shell after bash and the Korn shell.
Ksh, pdksh
The Korn shell and its public domaincousin. Written by David Korn, this is the default shell on many commercialUNIX versions.
Bash
The Linux staple shell from the GNU project. Bash, or Bourne AgainSHell, has the advantage that thesource code is freely available, and even if it's not currently running on yourUNIX system, it has probably been ported to it. Bash has many similaritiesto the Korn shell.
How do I view the shell types supported by the current system?
[zbj@localhost ~] $cat / etc/shells / bin/sh/bin/bash/sbin/nologin/usr/bin/sh/usr/bin/bash/usr/sbin/nologin [zbj@localhost ~] $3. Historical command mechanism (history)
The commands we type on the command line are cached in memory, and in order to keep the command history for the user the next time we boot, bash creates a .bash _ history file for each user's home directory. When the user exits the terminal normally, the in-memory command record cache is saved to the .bash _ history file.
Normally, the reading and recording of history commands is as follows:
When we log in to the Linux host with bash, the system will actively read the previous commands from the home directory ~ /. Bash_history, then how many pieces of data will bash_history record? This has something to do with the variable configuration value of your bash HISTFILESIZE.
Suppose I have issued a total of 100 orders after logging in to the host this time, "when I log out, the system will update a total of 1000 historical commands to ~ /. Bash_history. "in other words, when I log out, the history command records the HISTFILESIZE pen in the most recent cache in my record file!
3.1 history-related environment variables
HISTFILE is used to specify the file in which historical command records are saved. When bash starts, it reads the ~ / .bash_history file and loads it into memory. This variable is used to set the .bash _ history file, and when bash exits, it writes back the memory history to the .bash _ history file.
~ /. Bash_history records the commands executed in the previous login, while the commands executed in this login are stored in memory, and the history of these commands will not be recorded in .bash _ history until you successfully log out of the system.
HISTFILESIZE defines the total number of records that save commands in the file ~ / .bash_history
HISTSIZE defines the number of records output by the history command.
HISTCONTROL controls how history is retained
Ignorespace commands that begin with a white space character are not logged
Ignoredups ignores repetition, and consecutive repeated commands are recorded only once.
Ignoreboth is valid for both (ignorespace + ignoredups)
HISTTIMEFORMAT Historical Command record format
Use HISTTIMEFORMAT to display timestamps
$export HISTTIMEFORMAT='%F% T'$history | more
3.2 related commands
Bash's history command management function allows history commands to review, modify, and reuse previously used historical commands.
# # display history command # history displays all history # history N shows several commands executed before, for example: history 2 shows two commands recently executed # # clear history cache # history-caches # write history# history-w let bash write history commands immediately from memory to .bash _ history file # history-a write the newly added history history commands to .bash _ history file
3.3event indicator (event designators)
In shell! Called the event prompt, or Event Designators, the event indicator (event designator) is a reference to a command line entry in the history list.
! Start a history command replacement, unless it is followed by spaces, tabs, line Terminators, "=", "(". [when the shell option for extglob is enabled using the built-in command shopt].
! n will refer to the nth command in history, such as entering! 100, to execute the 100th command in the history list.
On the command line, put the exclamation point "!" Executing a command in double quotation marks can go wrong (e.g., echo "hello!"). Because the exclamation point is interpreted as a historical command. However, in a script file, this is correct because the history mechanism of Bash is disabled in the script file.
! n execute the nth command! Execute the last command! STRING executes the last command that begins with STRING # # execute history command #! Run the previous command #! 88 run the command #! ca run the last command containing ca # # search history command use ctrl+r to search for strings in history, press ctrl+r repeatedly to search forward the command containing strings in the list of historical commands, and enter will execute the search command
1.2 heavy recommendation
Ctrl-r
Sometimes, what if you want to re-enter a command that you have previously entered? I've seen two approaches:
L keep pressing the up arrow button, trying to find out which command
Type the history command, and then find the command, or grep the output of the history command
In fact, you have a better choice, that is, press Cmurr, and then enter the word contained in the command you want, and a command containing that word will appear. If it is not the command you want, press Cmurr until the command you want appears.
Sometimes you need to edit a history command before executing it. For example, you can search for "httpd" like this, display the history command "service httpd stop" on the terminal, select it, change "stop" to "start" and execute it.
[note: pressing Ctrl+R at the command prompt will display the prompt "reverse-i-search"]
(reverse-i-search) `httpd`: service httpd stop
[note: press the left or right button after you see the command you want, and you can edit it before executing the command] Cmurr effect:
(reverse-i-search) `ls': ls a b c
Alt-.
I often see other people use mkdir long-long-long-name-dir, and then type cd, followed by that long can no longer grow the directory name, then I will tell him, in fact, after you type cd, you can press Alt-., can automatically enter that long can no longer directory name. Actually, Alt-. The real function is to input the last argument of the previous command to the current command line. It is very convenient and highly recommended. If you continue to press Alt-., you will get the last parameter of the previous command. Similarly, what if you want to take the first parameter of the previous command? To use Alt-0 Alt-., is to enter Alt-0 first and then Alt-.. What if it is the first parameter of the previous command? Alt-0 Alt-., of course. Alt-. Yes.
The command history mechanism in UNIX systems is only suitable for interactively accessing Shell and cannot be used in Shell scripts.
This means that the command history function (the function that presses the up key to call up the history command) can only be used in interactive mode (interactive mode). When running a script, the script is in non-interactive mode by default and cannot use the command history feature.
4. Completion of command and file name (TAB | 2 consecutive TAB TAB)
Command line automatic completion (automatic command line completion)
Bash provides the following standard completion commands by default for linux users.
Variable name completion (Variablename completion)
User name completion (Username completion)
Hostname completion (Hostname completion)
Path path completion (Pathname completion)
File name completion (Filename completion)
Commands include: internal commands and external commands. Internal commands are provided by shell itself, which is equivalent to the function of a function. An external command is an executable file under a path.
Executing external commands requires searching according to the path specified in the PATH environment variable, searching from left to right, matching what is found first, and stopping the search process. But when it is executed once, it will no longer look under the PATH path. In order to speed up the search command, the command path is directly saved in the cache (hash table) to improve the search efficiency.
[zbj@localhost ~] $help hash # hash is the bash built-in command hash:hash [- lr] [- p pathname] [- dt] [name.] Remember or display program locations. [zbj@localhost] $hashhits command 1 / usr/bin/last 5 / usr/bin/sort 1 / usr/bin/rm 1 / usr/bin/cat 2 / usr/bin/vim 5. Command aliases (alias) # set aliases alias name='command [option] [argument]'# cancel the specified aliases settings unalias name# list set aliases alias# escape aliases alias rm='rm-i'\ rm # escape aliases using the original command [root@localhost ~] # rm tmp.txtrm: remove regular file `tmp.txt'? N [root@localhost ~] #\ rm tmp.txt #\ rm, using\ to escape aliases
1. How do I cancel the alias?
two。 Are aliases valid in shell scripts?
3. How do I list all aliases?
4. How do I cancel all aliases?
5. How do I execute the ls command itself instead of an alias?
Normally, bash checks to see if the first token of the instruction is an alias, and if so, replaces the alias with the real instruction.
Aliases defined in shell take effect only in the current shell lifecycle. Usually we use the command configuration to temporarily take effect in memory, if we want to take effect permanently, we need to modify the configuration file of the program.
Aliases can be nested, but they are not infinitely recursive, causing an endless loop.
The first word of each simple command, token, is checked for aliases if referenced. If it exists, the token is replaced by the text of the alias. The alias name and replacement text may contain any incorrect shell input, including shell special characters, except that the alias name cannot contain "=". The first token of the replaced text is tested with an alias, but the token with the same alias has been extended without a second extension. This means that ls can become ls-F, and Bash does not attempt to recursively extend the replacement text. If the last character of the alias value is a space or TAB, the next command following the alias will check for alias extension.
Aliases are useful when you need to specify a command that has multiple versions on your system as the default version, or to specify a default option for the command. Another use of aliases is to correct incorrect spelling.
When shell is not interactive, aliases are not extended unless the expand_aliases option is set to use the shopt shell built-in command.
6. Wildcard (wildcard, gblobbing) [root@localhost ~] # ls * .shinit_use.sh mysql_bak.sh
Wildcards are the system shell hierarchy (level) and are interpreted by shell, while regular expressions need to be supported by related tools: egrep, awk, vi, perl.
In text filtering tools, regular expressions, such as awk,sed, are used for the contents of the file.
Wildcards are often used on file names, such as looking up find,ls,cp, and so on. Wildcards are used to describe [file name matching] and must fully match the entire file name. The function is very simple, so there is no regular expression cow X.
The wildcard is handled by shell itself (not by the command statement involved), and it only appears in the "parameters" of the command (it is not used in the command name or on the operator). When shell encounters a wildcard in the parameters, shell will use it as a file name to search the disk for possible matches: if a match exists, it will be replaced (path extension); otherwise, the wildcard will be passed to the command as a normal character, and then processed by the command. In short, wildcards are actually a kind of path extension implemented by shell. After the wildcard is processed, shell first completes the reorganization of the command, and then continues to process the reorganized command until the command is executed.
This support, also known as "globbing" (for historical reasons), allows you to specify more than one file at a time by using wildcard mode. Bash and other Linux commands will explain this pattern by looking for and finding any matching files on disk.
Let's go back and analyze the above command: * .sh actual shell search file, found the file that meets the criteria, the command will become: ls init_use.sh mysql_bak.sh, actually passed to it when the ls is init_use.sh mysql_bak.sh.
Now that we know about shell wildcards, let's take a look at what common shell wildcards are.
* match any character
? Match any single character
[] matches any single character within the specified range
Generally speaking, it is precisely because the meta and wildcard in shell are sometimes the same as the meta in command, in order to make the meta in command not parsed by shell so that it can be changed, it is necessary to use shell quoting to ensure its text invariance.
The wildcard matches the file name, but does not contain "." At the beginning (so the wildcard cannot match the hidden file name).
There are three quoting mechanisms: the escape character, single quotes, and double quotes.
Keyboard shortcuts (command line editing)
When entering commands on the command line, we can use keyboard shortcuts to edit the command line.
Ctrl + a: move to the beginning of the command line (the beginning of the letter a)
Ctrl + e: move to the end of the command line (end)
Ctrl + u: delete from the cursor to the beginning of the command line
Ctrl + k: delete from cursor to end of command line
Ctrl + w: delete a word (word)
Ctrl + l: clear the screen (clear)
Ctrl + c: terminate the command (SIGINT)
Ctrl + z: suspend command
Ctrl + D: exit OR EOF (End Of File)
Ctrl + s: pause terminal transmission
Ctrl + Q: resume terminal transmission
7. Shell (scripts)
The accumulation of commands.
8. Basic Istroke O redirection
Standard input and output: the program should have the source side of the data, the destination side of the data, and the place to report the problem. A program does not have to know what device is behind its input and output: a file on disk, a terminal, a network connection, or another program. When the program starts, it can be expected that standard output and input are turned on and ready for use.
By default, they read standard input, write to standard output, and pass error messages to standard error output, which is called a filter. The default standard input, standard output, and standard error output are all terminals.
So who initializes the standard input, output and error output for the executed program? System initialization.
The answer is that when you log in, UNIX will arrange the default standard input, output and error output as your terminal. ICompO redirection is where you rearrange where to input or output by interacting with the terminal or by setting it in a shell script. Ipaw O redirection is a multi-task coordination mechanism provided by Linux.
Basic concepts:
I File descriptor O redirection is usually related to FD (File descriptor). The FD of shell is usually 10, that is, 0 to 9.
There are 3 commonly used FD, which are 0 (stdin,STDIN_FILENO standard input), 1 (stdout,STDOUT_FILENO standard output) and 2 (stderr,STDERR_FILENO standard error output), and are associated with keyboard, monitor and monitor by default
Use
< 来改变读进的数据信道(stdin),使之从指定的档案读进;0 是 来改变送出的数据信道(stdout, stderr),使之输出到指定的档案; 在IO重定向 中,stdout 与 stderr 的管道会先准备好,才会从 stdin 读进资料; 管道"|"(pipe line):上一个命令的 stdout(不包括stderr) 接到下一个命令的 stdin; tee 命令是在不影响原本 I/O 的情况下,将 stdout 复制一份到档案去; bash(ksh)执行命令的过程:分析命令-变量求值-命令替代(``和$( ))-重定向-通配符展开-确定路径-执行命令; ( ) 将 command group 置于 sub-shell 去执行,也称 nested sub-shell,它有一点非常重要的特性是:继承父shell的Standard input, output, and error plus any other open file descriptors。 exec 命令:常用来替代当前 shell 并重新启动一个 shell,换句话说,并没有启动子 shell。使用这一命令时任何现有环境都将会被清除。exec 在对文件描述符进行操作的时候,也只有在这时,exec 不会覆盖你当前的 shell 环境。 默认情况下,>If the original file exists, the default action is to overwrite. How to prohibit this kind of behavior?
Disallow existing regular files to be overwritten by redirection of output.# set-C # turn off this feature # set + C # on
How to write all the data of standard output stdout and standard error output stderr to the same file?
Find / home-name .bashrc > file.out 2 > file.out # error can achieve the effect, but the information intertwined find / home-name .bashrc & > file.out # rightfind / home-name .bashrc > file.out 2 > & 1 # right
. 2 > & 1 runs a command and merges its standard output with its output. (strictly speaking, file descriptor 2 is created by copying file descriptor 1, but the effect is usually to merge two streams.)
Let's elaborate on 2 > & 1: 2 > & 1, that is, FD2=FD1, this does not mean that the value of FD2 is equal to the value of FD1, because > is to change the data channel sent, that is, to change the "data output channel" of FD2 to the "data output channel" of FD1. If that's all, this change doesn't seem to work, because the default output of FD2 and the default output of FD1 are originally monitor, the same! However, this has a special purpose when FD1 is other files, or even other FD. Please be sure to understand this.
Why should 2 > & 1 be written at the end?
Command > file 2 > & 1 # use dup2 ()
First, command > file redirects standard output to file. 2 > & 1 is the act of standard error copying standard output, that is, it is also redirected to file, and the end result is that both standard output and error are redirected to file.
Command 2 > & 1 > file
2 > & 1 standard error copies the behavior of standard output, but at this time the standard output is still on the terminal. The output is redirected to file after file, but the standard error remains on the terminal.
Consider what kind of file sharing structure will result from different dup2 () call sequences. Please refer to APUE 3.10,3.12
IX. Pipeline (pipe)
Program1 | program2 can modify the standard output of program1 to standard input of program2. Pipes can connect multiple running programs together (pipes can make execution ten times faster than programs that use temporary files). When constructing pipes, you should try to reduce the amount of data in each phase to improve performance.
Pipes can only handle the standard output (standard out) from the previous command
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.