In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article is to share with you what are the knowledge points about shell. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
$? The previous command exits status. 0 indicates success.
$$shell process ID
PPID parent process ID
The default name of the current Locale of LANG, which is overridden by other LC_* variables
LC_ALL current Locale name, which overrides LANG and other LC_*
PS1 (prompt sign) current prompt string
Exit terminates the execution of the entire script
Return exits the current function and returns the number return N or $?
Shift
Shift 3 means that the original $4 is now $1, the original $5 is now $2, and so on, the original $1, $2, $3 are discarded, and $0 is not moved. The shift command with no arguments is equivalent to shift 1
Getopts
Http://blog.sina.com.cn/s/blog_674b5aae0100o2nz.html
The general format of getopts is: getopts option_string variable
Specify the value of the variable, that is, put a colon after the option in option_string, but when the colon is placed in the first option, it exists as an option
The specific meaning is that if an option is set to be passed by value but no value is passed, the information after the option is returned. If this option is not added, the default system will also return an error message, but the error message is not clear.
For example, getopts ahfvc: option indicates that options a, h, f, v can be passed without the actual value, while option c must take a value. When using an option to take a value, you must use the variable OPTARG to save the value.
When you use the getopts command, shell automatically generates two variables, OPTIND and OPTARG.
The initial value of OPTIND is 1, which means the index of the next parameter to be processed. The getopts command returns true as long as it exists, so the general getopts command uses a while loop
OPTARG is the location that is stored when getopts gets its expected parameters.
#! / bin/bash
If [$#-lt 1]
Then
Echo "there is no option"
Else
While getopts ": Iti:s:v" opt
Do
Case $opt in
I) echo "option is I"
T) echo "option is t"
I) ii=$OPTARG;echo "option is iMager the value is $ii"
S) ss=$OPTARG;echo "option is s, the value is $ss"
V) echo "option is v\ n"
:)
Echo "> Error:'- $OPTARG' requires an argument"
?) paralist=-1
Echo "> Error:'- $OPTARG' not supported,please input valid argument [Itisv]"
Esac
Done
Fi
Here are a few examples of execution:
(1) sh datediff.sh-I 1-I-v
Option is i,the value is 1
Option is I
Option is v\ n
(2) sh datediff.sh-s-I 1
Option is s, the value is-I / / take the value after the s option
(3) there is a leading colon in the sh datediff.sh-s / / option
> Error:'- s' requires an argument
Output after the leading colon in the sh datediff.sh-s / / removal option
Datediff.sh: option requires an argument-- s
> Error:'- 'not supported,please input valid argument [Itisv]
Subshell and code blocks
The former is surrounded by (), while the latter is {}
Subshell can be executed anywhere on the line, creating a new process. In addition, calling & submitting a background job / pipe creates a new process.
The code block can only be placed after a newline character, semicolon, or keyword to share the state with the main script
Http://stackoverflow.com/questions/5547787/running-shell-script-in-parallel
Subshell analog parallel operation
#! / bin/bash
For i in $(seq 1 1000)
Do
(Generating random numbers here, sorting and outputting to file$i.txt) &
If (($I% 10 = = 0)); then wait; fi # Limit to 10 concurrent subshells.
Done
Wait
Tee
Http://linux.chinaunix.net/docs/2007-08-07/4538.shtml
The tee command reads data from standard input and outputs its contents to a standard output device, while saving the contents to a file. For example, there is the following script snippet, which is used to obtain the local ip address:
Ipaddr= `/ sbin/ifconfig | grep 'inet addr:' | grep-v' 127.0.0.1'| cut-d:-f3 | awk'{print $1}'`
# Note that the whole sentence after the = sign is enclosed in backquotation marks (the key to the left of the number 1 key).
Echo $ipaddr
When you run this script, the actual output is not the local ip address, but the broadcast address. In this case, we can use the tee command to output some intermediate results and modify the above script fragment to:
Ipaddr= `/ sbin/ifconfig | grep 'inet addr:' | grep-v' 127.0.0.1' | tee temp.txt | cut-d:-f3 | awk'{print $1}'`
Echo $ipaddr
After that, execute the script again, and then look at the contents of the temp.txt file:
$cat temp.txt
Inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
We can find that the second column of the intermediate result (separated by a: sign) contains the IP address, while in the above script, the third column is intercepted using the cut command
So we just need to change cut-d:-f3 in the script to cut-d:-f2 to get the correct result.
Trap
Http://linux.chinaunix.net/docs/2007-08-07/4538.shtml
The trap command is used to capture a specified signal and execute predefined commands.
Its basic syntax is:
Trap 'command' signal
Where signal is the signal to be captured, and command is the command to be executed after the specified signal is captured.
You can use the kill-l command to see all the available signal names in the system, and the command executed after capturing the signal can be any one or more legal shell statements, or a function name.
When the shell script is executed, it produces three so-called "pseudo signals", which are called "pseudo signals" because these three signals are generated by shell, while the other signals are generated by the operating system.
Table 1. Shell pseudo signal
When is the signal name generated?
EXIT exits from a function or the entire script is executed
ERR when a command returns a non-zero state (indicates that the command execution is not successful)
Before each command in the DEBUG script is executed
By capturing the EXIT signal, we can output the values of some variables we want to track when the shell script aborts execution or exits from the function, and then determines the execution status of the script and the cause of the error. The method is:
Trap 'command' EXIT or trap' command' 0
By capturing ERR signals, we can easily track unsuccessful commands or functions and output relevant debugging information.
The following is an example program that captures ERR signals, where $LINENO is a built-in variable of shell that represents the current line number of the shell script.
$cat-n exp1.sh
1 ERRTRAP ()
2 {
3 echo "[LINE:$1] Error: Command or function exited with status $?"
4}
5 foo ()
6 {
7 return 1
8}
9 trap 'ERRTRAP $LINENO' ERR
10 abc
11 foo
The output is as follows:
$sh exp1.sh
Exp1.sh: line 10: abc: command not found
[LINE:10] Error: Command or function exited with status 127
[LINE:11] Error: Command or function exited with status 1
During debugging, in order to track the values of certain variables, we often need to insert the same echo statements in many parts of the shell script to print the values of related variables, which is cumbersome and clumsy.
By capturing the DEBUG signal, we only need a trap statement to complete the whole tracking of the relevant variables.
The following is an example program that tracks variables by capturing DEBUG signals:
$cat-n exp2.sh
1 #! / bin/bash
2 trap 'echo "before execute line:$LINENO, DEBUG.
3 axi1
4 if ["$a"-eq 1]
5 then
6 baked 2
7 else
8 bread1
9 fi
10 cysteine 3
11 echo "end"
The output is as follows:
$sh exp2.sh
Before execute line:3, axiomain, bimonthly, c =
Before execute line:4, axiom 1, baccarat c =
Before execute line:6, axiom 1, baccarat c =
Before execute line:10, axiom1, baccalaure2 c =
Before execute line:11, axiom 1, 2, 2, 3
End
From the running results, you can clearly see the change in the value of the relevant variables after each command is executed.
Eval
Http://www.cnblogs.com/friedwm/archive/2012/04/06/2435171.html
Eval is the equivalent of a parameter replacer. It evaluates and replaces all variables starting with $, and then executes the result as a command.
#! / bin/bash
PARA= "hello world my friend"
Function Process ()
{
Temp=$ (eval echo\ $$1 | cut-d'- f 2 -) # 1
Eval $1 =\ $temp # 2
}
Process PARA
Echo $PARA
[oracle@ ~] $sh tesh.sh
World my friend
Description:
At # 1, eval evaluates all the variables that follow it, $1==PARA, and then executes echo $PARA | cut-d'- f 2mur. the processed values are temporarily stored in temp.
# 2, replace first, the replacement result is: PARA=$temp, and then execute this command, and the result is copied back to the source parameter.
Http://doudouclever.blog.163.com/blog/static/175112310201252111104169/
Set 11 22 33 44
If you want to output the last parameter, 44, you can use the following command
Echo $4
But if we don't know how many parameters there are, and we want to output the last parameter, you might think of using $# to output the last parameter
If you use the command:
Echo "\ $$#"
The result is $4, not the 44 we want. This involves the problem of indirect references to variables. Our original intention is to output $4. By default, indirect references to variables are ignored after the command.
At this point, you can use the eval command.
Eval echo "\ $$#"
The result is 44.
Source, exec and system
Http://www.cnblogs.com/zhaoyl/archive/2012/07/07/2580749.html
Bash shell commands are divided into two categories: external commands and internal commands.
External commands are implemented through system calls or independent programs, such as sed, awk, etc.
Internal commands are implemented by special file formats (.def), such as cd, history, exec, and so on.
There are two ways to execute shell scripts, one is to generate a new shell and then execute the corresponding shell scripts;, the other is to execute under the current shell without enabling other shell.
The way to generate a new shell and then execute scripts is to add the following statement at the beginning of the scripts file
#! / bin/sh
The general script file (.sh) is used in this way. This method first enables the new sub-shell (the new child process) and then executes the command under it.
Another way is to use the source command mentioned above, which no longer generates a new shell, but executes all commands under the current shell.
The source command is the dot (.) Orders.
Type man source under bash, find the source command interpretation, and you can see the explanation "Read and execute commands from filename in the current shell environment and..." .
You can see that the source command executes each command in the parameter file in the current process, not another child process (or sub-shell).
Exec:
Type man exec under bash, find the exec command explanation, and you can see "No new process is created." This explanation means that the exec command does not produce new child processes. So what's the difference between exec and source?
1. The system calls exec to replace the original process with a new process, but the PID of the process remains the same. Therefore, it can be argued that the exec system call does not create a new process, but simply replaces the contents of the original process context.
The code, data, and stack segments of the original process are replaced by the new process.
A process mainly includes the following aspects:
(1) an executable program
(2) all data associated with the process (including variables, memory, buffers)
(3) Program context (program counter PC, where the program execution is saved)
2. Exec is a family of functions, which is composed of six functions, starting with excl and execv.
To perform an exec system call, this is usually the case. Create a new process with the fork () function, and then let the process execute the exec call.
We know that after fork () establishes a new process, the parent and the child share code segments, but the data space is separate, but the parent process will copy the contents of its own data space to the child process, and the context will also copy to the child process.
In order to improve efficiency, a write-time copy strategy is adopted, that is, when a child process is created, it does not copy the address space of the parent process, and the parent and child processes have a common address space.
Only when the child process needs to write data (such as writing data to the buffer), the address space is copied and the buffer is copied to the child process. Thus, the parent and child processes have a separate address space.
For fork () after the implementation of exec, this strategy can improve efficiency, if you start with copy, then after exec, the data of the child process will be abandoned and replaced by the new process.
3. The difference between exec and system
(1) exec directly replaces the original program with a new process, and does not return to the original program after running.
(2) system is called shell to execute your command, system=fork+exec+waitpid, after execution, go back to the original program. Continue with the following section.
In short, if you call with exec, you should first fork a new process, and then exec. And system doesn't need you to fork the new process, it's already packaged.
The exec command closes the current shell process when it is executed, and then changes to the following command to continue execution.
Thank you for reading! This is the end of this article on "what are the knowledge points of shell?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.