In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what is the use of the shell script, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
What is shell?
To put it simply, it is the intermediate medium used by the system to interact with computer hardware, which is just a tool of the system. In fact, there is another layer between shell and computer hardware, and that is the system kernel. For example, if you compare computer hardware to a person's body, while the system kernel is a person's brain, as for shell, it seems more appropriate to compare it to a person's facial features. Back to the computer, the user directly faces not the computer hardware but the shell. The user tells the shell the instructions, and then the shell transmits to the system kernel, and then the kernel dominates the computer hardware to perform various operations.
The shell installed by default in the linux release (Redhat/CentOS) system that I came into contact with is called bash, or Bourne Again Shell, which is an enhanced version of sh (Bourne Shell). Bourn Shell is the earliest shell. The founder's name is Steven Bourne, so it is called Bourn Shell in memory of him, and it is called sh. So what are the characteristics of this bash?
1) record command history
Linux will record the commands we have typed, and 1000 historical commands can be recorded by default. These commands are saved in the. bash_history file in the user's home directory. One thing you need to know is that commands running in the current shell will be saved to the .bash _ history file only when the user exits the current shell normally.
There is an interesting character related to the history of commands, which is "!" Yes. There are several commonly used applications: (1)! (two consecutive "!"), indicating the execution of the previous instruction; (2)! n (where n is a number), indicating the execution of the nth instruction in the history of the command, for example, "! 100" means the 100th command in the history of the command; (3)! A string (a string greater than or equal to 1), such as! ta, represents the last instruction in the execution history that begins with ta.
2) instruction and file name completion
I introduced this feature at the beginning of this tutorial, remember? By the way, press the tab key, which can help you complete an instruction, a path or a file name. Press the tab key twice in a row and the system will list all instructions or file names.
3) Alias
Alias has also been introduced earlier, and this is one of the unique features of bash. We can use alias to alias a commonly used and very long instruction a concise and easy-to-remember instruction. If you don't want to use it, you can also use unalias to disable the alias function. If you tap alias directly, you will see the preset alias of the system:
See, there are only a few alias instructions that are preset by the system, and you can also customize the aliases of the instructions you want. The alias syntax is simple: alias [command alias] = ['specific command'].
4) wildcards
Under bash, you can use * to match zero or more characters, while using? Matches one character.
5) input and output from orientation
Input redirection is used to change the input of a command, and output redirection is used to change the output of a command. Output redirection is more common and is often used to enter the results of a command into a file rather than on the screen. Enter the command for redirection, plus error redirection 2 >, and overload redirection > >, which will be described in more detail later.
6) Pipe symbol
The pipe character "|" has been mentioned earlier, which is to throw the result of the previous command to the following command.
7) Job control.
When running a process, you can pause it (press Ctrl+z), then use the fg command to resume it, use the bg command to make it run in the background, or you can make it stop (press Ctrl+c).
[variable]
In the previous section, the author introduced the environment variable PATH, which is a variable preset by shell, which is usually capitalized by shell. Variables, to put it simply, is to use a simple string to replace some special settings and data. In the case of PATH, this PATH replaces the setting of the absolute path of all commonly used commands. Because of the variable PATH, instead of entering the global path when we run a command, we just type the name of the command. You can use the echo command to display the value of the variable.
In addition to PATH, HOME, LOGNAME, what are the default environment variables for the system?
Use the env command to list all the system variables that are preset by the system. However, the values of these environment variables are not the same for different users. What is currently displayed is the environment variable for the root account. Here is a brief introduction to common environment variables:
PATH determines in which directories shell will look for commands or programs.
HOME current user home directory
Number of HISTSIZE history records
Login name of the current user of LOGNAME
HOSTNAME refers to the name of the host
SHELL pre-user Shell type
LANG language-related environment variable, which can be modified by multiple languages
Email storage directory of the current user of MAIL
PWD current directory
The variables displayed by the env command are only environment variables. In fact, there are many variables preset by the system. You can use the set command to display all the variables preset by the system.
Limited to space, the author did not take screenshots of all the display results in the above example. Set can display not only system preset variables, but also user-defined variables. User-defined variables? Yes, users can also define variables themselves.
Although you can customize the variable, the variable can only take effect in the current shell. Why don't you try logging in to another shell?
Use the bash command to open another shell, at which point the previously set myname variable no longer exists, exit the current shell and return to the original shell,myname variable is still there. What if the variable you want to set is always in effect? There are two situations:
1) this variable can be used by all users in the system after logging in.
You need to add "export myname=Aming" to the last line of the / etc/profile file and then run "source / etc/profile" to take effect. At this point, you can run the bash command or go directly to the su-test account.
2) only want the current user to use this variable
You need to add "export myname=Aming" to the last line of the .bashrc file in the user's home directory and run "source .bashrc" to take effect. Log in to the test account at this time, and the myname variable will not take effect. The function of the source command used above is to refresh the configuration currently set, that is, it can take effect without having to log out and log in again.
In the above example, the author uses "myname=Aming" to set the variable myname, so what are the rules for setting custom variables under linux?
a. Set the format of the variable to "aquib", where an is the name of the variable and b is the content of the variable. There can be no spaces on both sides of the equal sign.
b. Variable names can only consist of letters, numbers, and underscores, and cannot start with a number
c. When a variable contains special characters (such as spaces), it needs to be enclosed in single quotation marks
In one case, you should note that the variable itself is enclosed in single quotation marks, which requires the use of double quotation marks.
d. If you need to use other commands to run the results in the contents of the variable, you can use backquotes.
e. The contents of variables can be added to the contents of other variables, requiring double quotation marks
Here, if you accidentally add double quotation marks to single quotation marks, you will not get the result you want.
From the above examples, you may be able to see the difference between single quotation marks and double quotation marks: the use of double quotation marks does not cancel the function of the special characters that appear in them (the $here). While using single quotation marks, all the special characters inside lose their own function.
In the previous example, the author used the bash command many times. If you run the bash instruction in the current shell, you will enter a new shell. This shell is the child shell of the original shell, so you might as well use the pstree instruction to check it.
The pstree instruction prints out all processes in the linux system through a tree structure. Due to the limited space, the author has not listed all of them, so you can enter pstree directly to view it. After setting a variable in the parent shell, the variable will not take effect after entering the child shell. If you want this variable to take effect in the child shell, you need to use the export instruction, which the author has used before.
Export actually means to declare this variable, so that the child shell of the shell also knows that the value of the variable abc is 123. If export is followed by no variable names, it declares all variables.
At the end, along with our custom variables, are declared.
I just talked about how to set a variable. What if I want to cancel a variable? Just enter "unset variable name".
After using unset abc, echo $abc will no longer output anything.
[configuration files for system environment variables and personal environment variables]
There are a lot of system variables mentioned above, so where are these variables stored in the linux system, and why do users have these variables automatically as soon as they log in to shell?
/ etc/profile: this file presets several important variables, such as PATH, USER, LOGNAME, MAIL, INPUTRC, HOSTNAME, HISTSIZE, umas, and so on.
/ etc/bashrc: this file mainly presets umask and PS1. This PS1 is the string in front of us when we type the command, for example, the author's linux system PS1 is [root@localhost ~] #, you might as well take a look at the value of PS1.
\ u is the user,\ h hostname,\ W is the current directory,\ $is the'#', and if it is an ordinary user, it is displayed as'$'
In addition to two system-level configuration files, there are several such hidden files in each user's home directory:
.bash _ profile: the file name that defines the user's personalization path and environment variables. Each user can use this file to enter shell information dedicated to their own use, and the file is executed only once when the user logs in.
.bashrc: this file contains bash information specific to your shell, which is read when you log in and each time you open a new shell. For example, you can write user-defined alias or custom variables to this file.
.bash _ history: used to record the history of commands.
.bash _ logout: this file is executed when you exit shell. You can put some clean-up work in this file.
[special symbols in linux shell]
In the process of learning linux, you may have come into contact with a special symbol, such as "*", which is a wildcard symbol that represents zero or more characters or numbers. Below, the author will talk about the special characters commonly used.
1. *: represents zero or more characters or numbers.
Test can be followed by no characters, or there can be multiple characters, either with or without a match.
2.?: only represents an arbitrary character
Whether it is a number or a letter, as long as it is a match.
3. #: this symbol indicates the meaning of the comment in linux, that is, the content after "#" is ignored by linux.
Insert "#" at the beginning or middle of the command and linux will ignore it. This symbol is used a lot in shell scripts.
4.\: an escape character that restores the following special symbols (such as "*") to ordinary characters.
5. |: pipe character, as mentioned many times before, its function is to throw the result of the command before the symbol to the command after the symbol. The following commands mentioned here are not available for all commands, and commands for document operations are commonly used, such as cat, less, head, tail, grep, cut, sort, wc, uniq, tee, tr, split, sed, awk and so on, among which grep, sed, awk are the tools that regular expressions must master, which are described in detail in the following content.
6. $: in addition to the identifier used in front of the variable, there is another wonderful use, that is, and'!' Use it together.
'! $' indicates that the last variable was hit in the previous command (maybe it is not appropriate to call it a variable, in short, it is the last thing in the previous command). For example, the last command above is test.txt, then enter! $under the current command represents test.txt.
1) grep: filter one or more characters, and their usage will be described in more detail in the following content.
2) cut: intercept a field
Syntax: cut-d "delimiter" [- cf] n where n is a number
-d: followed by a delimiter, which is enclosed in double quotation marks
-c: which character is followed by
-f: which block is followed by
-d is followed by a delimited character, where a colon is used to split the character,-f 1 is to intercept the first paragraph, and the space between-f and 1 is optional.
-c can be followed by a number n, an interval n1-n2, or multiple digits N1, N2, n3.
3) sort: used for sorting
Syntax: sort [- t delimiter] [- kn1,n2] [- nru] N1 here
< n2 -t 分隔符 :作用跟cut的-d一个意思 -n :使用纯数字排序 -r :反向排序 -u :去重复 -kn1,n2 :由n1区间排序到n2区间,可以只写-kn1,即对n1字段排序 4) wc :统计文档的行数、字符数、词数,常用的选项为: -l :统计行数 -m :统计字符数 -w :统计词数 5) uniq :去重复的行,笔者常用的选项只有一个: -c :统计重复的行数,并把行数写在前面 有一点需要注意,在进行uniq之前,需要先用sort排序然后才能uniq,否则你将得不到你想要的,笔者上面的试验当中已经是排序过所以省略掉那步了。 6)tee :后跟文件名,类似与重定向">", but the specific gravity orientation has one more function, which not only writes the file to the following file, but also displays it on the screen.
7) tr: replacement characters, often used to deal with special symbols that appear in documents, such as the ^ M symbol that appears in DOS documents. There are two common options:
-d: delete a character, followed by the character to be deleted
-s: remove repetitive characters
The most commonly used is to change lowercase to uppercase: tr'[a murz]'[Amurz]'
Of course, it is also possible to replace a character.
However, replacement, deletion and de-repetition are all for one character and have some limitations. If it is for a string, it will no longer work, so I suggest just a simple understanding of the tr, you will learn more tools for string manipulation in the future.
8) split: cut documents, common options:
-b: split the document according to its size, in byte
The format is as shown in the above example. The passwd is the prefix of the split file name, and the split file name is passwdaa, passwdab, passwdac.
-l: split the document according to the number of lines
6.: semicolon. We usually type one command on one line, and then enter to run it, so how about running two or more commands in one line? You need to add a ";" between the commands.
7. ~: user's home directory, / root if root, and / home/username for ordinary users
8. &: you need to add this symbol if you want to put a command in the background. It is usually used in situations where commands take a very long time to run.
Use jobs to view the tasks performed in the background in the current shell. It can be transferred to the front desk with fg. The sleep command here means hibernation, followed by a number, in seconds, in a cyclic shell script.
At this point, you press CTRL + z to pause it, and then type bg to enter the background again.
In the case of multitasking, if you want to transfer the task to the foreground, fg is followed by a task number, which can be obtained using the jobs command.
9. >, > >, 2 >, 2 > >: the overredirect symbol > and > > denote substitution and appending, respectively, and then there are two symbols here: 2 > and 2 > > denote error redirection and error pursuit and aggravation direction, respectively. When we run a command to report an error, the error message will be output to the current screen. If you want to redirect to a text, use 2 > or 2 > >.
10. []: parentheses, with a combination of characters in the middle, representing any of the intermediate characters
11. & & and | |
I just mentioned the semicolon, which is used as a separator between multiple commands. There are also two special symbols that can be used in the middle of multiple commands, namely "&" and "| |". The author lists all these situations as follows:
1) command1; command2
2) command1 & & command2
3) command1 | | command2
When using ";", command2; will be executed regardless of whether command1 executes successfully or not. When "&" is used, command2 will not execute until command1 executes successfully, otherwise command2 will not execute; when "| |" is used, command2 will not execute after command1 executes successfully, otherwise command2 will be executed. In short, command1 and command2 will always execute a command.
The above is all the content of this article "what is the use of shell script?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.