In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Don't say much nonsense, it's all in the picture!
First, the happy beginning of the introduction to shell
First, the pleasant beginning of the introduction of shell Shell itself is a program written in C language, it is a bridge for users to use Unix/Linux, most of the user's work is done through Shell. Shell is both a command language and a programming language. As a command language, it interactively interprets and executes commands entered by users; as a programming language, it defines a variety of variables and parameters, and provides many control structures that are unique in high-level languages, including loops and branches.
Although it is not a part of the kernel of the Unix/Linux system, it invokes most of the functions of the system core to execute programs, create files and coordinate the running of each program in a parallel way. Therefore, for users, shell is the most important utility, in-depth understanding and proficiency in the characteristics of shell and how to use it is the key to make good use of Unix/Linux system.
Second, the common shell before the life and this life
The common Shell script interpreters on Unix/Linux are bash, sh, csh, ksh, and so on, which are traditionally called a Shell. We often talk about how many Shell there are, but we are actually talking about the Shell script interpreter.
Bash is the default shell,bash of the Linux standard, which is done jointly by Brian Fox and Chet Ramey. It is an abbreviation for Bourne Again Shell. There are a total of 40 internal commands.
Linux uses it as the default shell because it has features such as:
You can use functions similar to doskey under DOS to look up and quickly enter and modify commands with arrow keys.
Automatically gives a command that starts with a string by looking for a match.
Contains its own help function, you only need to type help at the prompt to get related help.
Sh
Sh is developed by Steve Bourne and stands for Bourne Shell, and sh is the default shell of the Unix standard.
Ash
Ash shell is written by Kenneth Almquist, and Linux is a small shell that takes up the least system resources. It contains only 24 internal commands, so it is very inconvenient to use.
Csh
Csh is the larger kernel of Linux. It is compiled by 47 authors represented by William 喜悦 and has 52 internal commands. The shell actually points to a shell like / bin/tcsh, that is, csh is actually tcsh.
Ksh
Ksh is an acronym for Korn shell and is written by Eric Gisin with 42 internal commands. The biggest advantage of this shell is that it is almost fully compatible with the commercial version of ksh, so you can try the performance of the commercial version without paying for the commercial version.
Note: bash is the abbreviation for Bourne Again Shell and the default shell of the linux standard. It is based on Bourne shell and absorbs some of the features of C shell and Korn shell. Bash is fully compatible with sh, that is, scripts written in sh can be executed in bash without modification.
III. The compiled language and interpretive language of the episode
Compiling language
Many traditional programming languages, such as C, C++ and Java, are compiled. Such languages need to convert our written source code (source code) into object code (object code) in advance, a process known as "compilation".
When running the program, read the object code (object code) directly. Because the compiled object code (object code) is very close to the bottom of the computer, so the execution efficiency is very high, which is the advantage of compiled language.
However, because compiled languages mostly operate at the bottom, dealing with bytes, integers, floating-point numbers, or other machine-level objects, it often requires a lot of complex code to implement a simple function. In C++, for example, it is difficult to copy all the files in one directory to another.
Interpretive language
Interpretive languages are also known as "scripting languages". When executing such programs, the interpreter (interpreter) needs to read the source code (source code) we have written, convert it into object code (object code), and then run it by the computer. Because there is more compilation process each time the program is executed, the efficiency is reduced.
The advantage of using scripting languages is that they mostly run at a higher level than compiled languages and can easily handle objects such as files and directories; the disadvantage is that they are generally not as efficient as compiled languages. On balance, however, scripting is usually worth it: a simple script that takes an hour to write, the same function implemented in C or C++, can take two days, and in general, the script executes fast enough to ignore its performance problems. Examples of scripting languages are awk, Perl, Python, Ruby, and Shell.
Fourth, my first intimate contact with shell script
How to write shell scripts:
The first line of the script text, the top box: gives shebang, the path to the interpreter program file, indicating the interpreter that interprets the running code of the current script
#! / bin/bash
#! / bin/tcsh
#! / usr/bin/python3
#! / usr/bin/perl
You can use a text editor: vi, vim, nano
What is a shell script?
Accumulation of commands
However, many non-commands are not idempotent, so it is necessary to use program logic to judge whether the running conditions are met or not, so as to avoid errors in operation.
Run the script method:
(1) grant execution permission and run the program file directly
Chmod + x / PATH/TO/SCRIPT_FILE
/ PATH/TO/SCRIPT_FILE
(2) run the interpreter directly, taking the script file as a parameter
Bash / PATH/TO/SCRIPT_FILE
The code in the script:
(1) lines starting with # will be ignored by the interpreter
(2) the blank line will be ignored by the interpreter
The first shell script, which outputs hello,world
#! / bin/bash#this is my first shell scriptecho "hello,world!"
Fifth, the magic of shell variable
Shell supports custom variables.
Define variable
When you define a variable, the variable name is not marked with a dollar sign ($), such as:
VariableName= "value"
Note that there can be no spaces between the variable name and the equal sign, which may be different from all the programming languages you are familiar with. At the same time, the naming of variable names must follow the following rules:
The first character must be a letter (a murz.Amurz).
There can be no spaces in the middle, you can use an underscore (_).
You cannot use punctuation.
You cannot use keywords in bash (you can view reserved keywords with the help command).
An example of a variable definition:
MyUrl= "http://zhaoyongtao.blog.51cto.com/"myNum=100 uses variables
To use a defined variable, simply precede the variable name with a dollar sign ($), such as:
Your_name= "kim" echo $your_name echo ${your_name}
The curly braces outside the variable name are optional, whether to add them or not, and the curly braces are added to help the interpreter identify the boundaries of the variable, as in the following case:
Fruit= "apple" echo "I like ${fruit} s"
If you don't add curly braces to the fruit variable and write it as echo "I like $fruit", the interpreter will treat $fruits as a variable (its value is empty), and the code execution result will not be what we expected.
It is recommended to add curly braces to all variables, which is a good programming habit.
Redefine variables
Defined variables can be redefined, such as:
MyUrl= "http://zhaoyongtao.blog.51cto.com"echo ${myUrl} myUrl=" https://blog.51cto.com"echo ${myUrl}
It's legal to write this way, but note that you can't write $myUrl= "https://blog.51cto.com/" on the second assignment, but only add the dollar sign ($) when using variables.
Read-only variable
Using the readonly command, you can define a variable as a read-only variable, and the value of a read-only variable cannot be changed.
The following example attempts to change a read-only variable and reports an error:
#! / bin/bashmyUrl= "http://zhaoyongtao.blog.51cto.com/"readonly myUrlmyUrl=" https://blog.51cto.com/"
Run the script and the result is as follows:
/ bin/sh: NAME: This variable is read only. Delete a variable
Use the unset command to delete a variable. Syntax:
Unset variable_name
Variables cannot be used again after they have been deleted; the unset command cannot delete read-only variables.
For example:
#! / bin/shmyUrl= "http://zhaoyongtao.blog.51cto.com/"unset myUrlecho $myUrl
The above script does not have any output.
Variable type
When running shell, three variables exist at the same time:
1) Local variable
Local variables are defined in scripts or commands and are valid only in the current shell instance. Other shell-started programs cannot access local variables.
2) Environmental variables
All programs, including those started by shell, can access environment variables, and some programs need environment variables to ensure their normal operation. Shell scripts can also define environment variables if necessary.
3) shell variable
The shell variable is a special variable set by the shell program. Some of the shell variables are environment variables and some are local variables. These variables ensure the normal operation of shell.
6. Cool techs of shell special variable
As mentioned earlier, variable names can only contain numbers, letters, and underscores, because some variables that contain other characters have a special meaning, and such variables are called special variables.
For example, $represents the ID of the current Shell process, that is, pid. Look at the following code:
#! / bin/bash#echo $$
Running result
12431
List of special variables
Variable
Meaning
, 0
The file name of the current script $n
Parameters passed to a script or function. N is a number that indicates the number of parameters. For example, the first parameter is $1 and the second parameter is $2 $#
The number of arguments passed to the script or function.
$*
All parameters passed to the script or function. $@
All parameters passed to the script or function. When enclosed in double quotation marks (""), it is slightly different from $*, which will be discussed below. $?
The exit status of the previous command, or the return value of the function.
$
The current Shell process ID. For Shell scripts, it is the process ID where these scripts are located.
Command line argument
The parameters passed to the script when you run the script are called command line parameters. Command-line parameters are represented by $n, for example, $1 for the first parameter, $2 for the second parameter, and so on.
Look at the following script:
#! / bin/bashecho "File Name: $0" echo "First Parameter: $1" echo "Second Parameter: $2" echo "Quoted Values: $@" echo "Quoted Values: $*" echo "Total Number of Parameters: $#"
Running result:
[root@MyServer test] # bash. / test.sh abc 123File Name:. / test.shFirst Parameter: abcSecond Parameter: 123Quoted Values: abc 123Quoted Values: abc 123Total Number of Parameters: 2 $* and $@
Both $* and $@ indicate that all arguments passed to a function or script, when not contained in double quotes (""), are marked with "$1"$2". Outputs all parameters in the form "$n".
But when they are enclosed in double quotes (""), "$*" takes all the parameters as a whole and outputs all parameters in the form of "$1 $2 … $n"; "$@" separates the parameters as "$1"$2"... Outputs all parameters in the form "$n".
The following example clearly shows the difference between $* and $@:
#! / bin/bashecho "\ $* =" $* echo "\"\ $*\ "=" $* "echo"\ $@ = "$@ echo"\ "\ $@\"$@" echo "print each param from\ $*" for var in $* doecho "$var" doneecho "print each param from\ $@" for var in $@ doecho "$var" doneecho "print each param from\"\ $*\ "for var in" $* "doecho" $var "doneecho" Print each param from\ "\ $@\"for var in" $@ "doecho" $var "done
Execute. / test.sh "a"b"c"d" and see the following result:
[root@MyServer test] # bash. / test.sh "a"b"c"d" $* = abcd "$*" = abcd $@ = abcd "$@" = abcdprint each param from $* abcdprint each param from $@ abcdprint each param from "$*" abcdprint each param from "$@" abcd exit status
$? You can get the exit status of the previous command. The so-called exit status is the result returned after the last command was executed.
The exit status is a number, and in general, most commands return 0 for success and 1 for failure.
However, there are some commands that return other values, indicating different types of errors.
In the following example, the command executes successfully:
[root@MyServer test] # bash. / test.sh abc 123File Name:. / test.shFirst Parameter: abcSecond Parameter: 123Quoted Values: abc 123Quoted Values: abc 123Total Number of Parameters: 2 [root@MyServer test] # echo $? 0
Variable substitution, command substitution, escape character escape character of shell substitution
If the expression contains special characters, Shell will replace it. For example, using a variable in double quotation marks is a substitution, and an escape character is also a substitution.
For example:
#! / bin/bash
Axi10
Echo-e "Value of an is $a\ n"
Running result:
Value of an is 10
Here-e means to replace the escape character. If you do not use the-e option, it will be output as is:
Value of an is 10\ n
The following escape characters can be used in echo:
Escape character meaning\\ backslash\ an alarm, bell\ b backspace (delete key)\ f page feed (FF), move the current position to the beginning of the next page\ nnewline\ r enter\ t horizontal tab (tab key)\ v vertical tab
You can use the-E option of the echo command to disable escape, which is also not escaped by default, and the-n option to disable line breaks.
Command replacement
Command replacement means that Shell can execute the command first, save the output temporarily, and output it in the appropriate place.
Syntax for command substitution:
`command`
Note that it is a backquote, not a single quotation mark, and this key is below the Esc key.
In the following example, the result of command execution is saved in a variable:
#! / bin/bash
DATE= `date`
Echo "Date is $DATE"
USERS= `who | wc-l`
Echo "Logged in user are $USERS"
UP= `date; uptime`
Echo "Uptime is $UP"
Running result:
Date is Thu Jul 2 03:59:57 MST 2009Logged in user are 1Uptime is Thu Jul 2 03:59:57 MST 200903 up 59 up 20 days, 14:03, 1 user, load avg: 0.13,0.07,0.15 variable substitution
Variable substitution can change the value of a variable according to its state (empty, defined, etc.)
The form of variable substitution that can be used:
The original value of the ${var} variable ${var:-word} returns word if the variable var is empty or has been deleted (unset), but does not change the value of var. ${var:=word} if the variable var is empty or has been deleted (unset), then return word and set the value of var to word. ${var:?message} if the variable var is empty or has been deleted (unset), the message message is sent to the standard error output, which can be used to detect whether the variable var can be assigned normally.
If this replacement appears in the Shell script, the script will stop running. ${var:+word} if the variable var is defined, it returns word, but does not change the value of var.
Take a look at the following example:
#! / bin/bashecho ${var:- Variable is not set} echo "1-Value of var is ${var}" echo ${var:= "Variable is not set"} echo "2-Value of var is ${var}" unset varecho ${var:+ "This is default value"} echo "3-Value of var is $var" var= "Prefix" echo ${var:+ "This is default value"} echo "4-Value of var is $var" echo ${var:? "Print this message"} Echo "5-Value of var is ${var}"
Running result:
Variable is not set
1-Value of var is
Variable is not set
2-Value of var is Variable is not set
3-Value of var is
This is default value
4-Value of var is Prefix
Prefix
5-Value of var is Prefix
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.