Shift parameter moves left one
SHIFT + number, the parameter moves to the left n bits at a time
It can be used to determine the number of parameters behind the program.
Returns an error when the shift value is empty
Use read to assign input values to one or more shell variables
Read reads values from standard input, assigns a variable to each word
All remaining words are assigned to the last variable
How bash expands the command line
In the following order of precedence
Dividing the command line into a single command word
Expand aliases
Expanded curly brace declaration ({})
Expand Tilde Declaration (~)
command to replace $ () and '
Divide the command line into command words again
Expand File Wildcard (*,?, [ABC], etc.)
Prepare for i/0 redirection (<, >)
Run command
Escape
A backslash (\) causes subsequent characters to be interpreted as intended
$ echo Your Cost: \$5.00
Your Cost: $5.00
Quotation marks to prevent extension
? Single quotation mark (') prevents all extensions
? Double quotation marks (") also prevent all extensions, but the following exceptions apply:
$ (dollar sign)-variable extension
' (anti-quote)-command substitution
\ (backslash)-suppresses single character expansion
! (exclamation mark)-Historical command replacement
Environment Configuration related
Bash's configuration file
In terms of effective scope, there are two categories:
Global configuration:
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
Personal configuration:
~/.bash_profile
~/.bashrc
The order and scope of the configuration files in effect
Interactive login:
(1) Enter the account password directly through the terminal login
(2) Users who switch with "Su-username"
Order of execution:/etc/profile--/etc/profile.d/*.sh-->~/.bash_profile--~/.BASHRC--and/ETC/BASHRC
? Non-interactive logon:
(1) Su UserName
(2) The terminal opened under the graphical interface
(3) Execute script
(4) Any other bash instance
Order of execution: ~/.BASHRC--/ETC/BASHRC-->/etc/profile.d/*.sh
Profile class
? By function, there are two categories:
Profile class and BASHRC class
? Profile class: Provides configuration for the interactive logon shell
Global:/etc/profile,/etc/profile.d/*.sh
Personal: ~/.bash_profile
Function:
(1) for defining environment variables
(2) run a command or script
BASHRC class
? BASHRC class: Provides configuration for non-interactive and interactive logon shells
Global:/ETC/BASHRC
Personal: ~/.BASHRC
Function:
(1) Defining command aliases and functions
(2) Defining local variables
Editing the configuration file takes effect
? After you modify the profile and BASHRC file, it takes effect
Two methods:
1 restarting the shell process
2. or source
Cases:
. ~/.bashrc
Bash quits task
? Save in ~/.bash_logout file (user)
? Run when you exit the login shell
? For
Create an automatic backup
Clear Temporary files
$-variable
? H:hashall, when this option is turned on, the shell hashes the path where the command is located, avoiding querying every time. The h option is turned off via set +h
? I:interactive-comments, including this option, shows that the current shell is an interactive shell. The so-called interactive shell, in the script, I option is off.
? M:monitor, turn on monitoring mode, you can control
Process stop, continue, background or foreground execution, etc.
? B:braceexpand, curly brace extension
? The H:history,h option opens, and you can expand the commands in the history list to
Through! Exclamation marks to complete, such as "!!" Returns a recent history command on the
"!n" returns the nth historical command
The difference between $* and [email protected]
$*
The following is the script code for a.sh:
Here is the script code for b.sh
See, $* all the parameters as a whole, so the first parameter of b.sh is a B C, the following argument is empty
[Email protected]
As the above demo is only a little bit different, the b.sh call into [email protected]
See, [email protected] All parameters are considered as independent individuals, so the b.sh parameters are a.sh.
Shift Action Demo
The following is the script content:
echo "The name of this script is: ' basename '"
echo "The first parameter is: $ $"
echo "The second parameter is: $"
echo "The third parameter is: $ three"
Echo "demonstrates the next shift parameter adjustment function, here is just one, shift back can be followed by a specific number of"
Shift
echo "The first parameter is the original \$2 parameter: $ $"
echo "The second parameter is the original \$3 parameter: $"
echo "The third parameter is the parameter after the original \$3, because it is not, should be empty: $ $"
Logical operations
True =1 false =0
and arithmetic
1 and 1 are 1
As long as 0 is 0.
or arithmetic
As long as 1 is 1.
0 and 0 are 0
Non -
! 0 is 1
! 1 is 0
Short-circuit operation in shell programming
Short Circuit and
The first one is 0, and the result must be 0.
The first one is 1, the second must be involved in the operation
Like what:
Command 1 && Command 2
When command 1 returns a value of 0, which is true , command 2 must participate in the operation
When command 1 returns a value of 1, which is false , command 2 does not participate in the operation
Short Circuit or
The first one is 1, and the result must be 1.
The first one is 0, the second must be involved in the operation
Like what:
Command 1 | | Command 2
When command 1 returns a value of 1, which is false , command 2 must participate in the operation
When command 1 returns a value of 0, which is true , command 2 does not participate in the operation
XOR: ^
XOR two values, same as false, different for true
Like what:
Command 1 ^ Command 2
When command 1 and command 2 return the same value , the result is a false
When command 1 and command 2 return A different value, the result is a true
Look at the following example, the operator position is different, the result is not the same, why the first one will show 2 messages at the same time?
The first instruction set returns the state so interpreted: The first command does not find the user, the return value is not 0, false, so it will execute | | After this command and the output succeeds, the return value is 0, true, so the command after && is executed, and the output is successful and the return value is 0.
So it shows 2 messages.
The second instruction set returns the state as follows: the first command does not find the user, the return value is not 0, false, and the following is the relationship , whether it is true or false, the final result is false, so will execute | | After the command and the output is successful, the return value is 0
We deliberately put the echo input error into Echoa, so that this command will return to 0, false
the instruction set return status is interpreted as follows: The first command finds the user, the return value is 0, true, and therefore executes the subsequent instruction, but the subsequent instruction is the wrong command, which returns a non-0, false, relationship with the following instruction, it must execute, the output succeeds, and the return value is 0
Pits produced by special operation results
When expr and let commands are calculated, be aware that
The result of expr evaluates to null or 0 when the value returned is 1
Let calculation, the last parameter is 0, the return value is 1
The brackets can be used to determine if the variable is empty, so observe the following changes carefully. The variables inside the brackets remember to use double quotation marks
You can also use a character + variable to determine whether a variable is empty
Some usage specifications in double brackets
[["$var" = = "abc"]] to determine whether a 2-side string is equal, use = =
[["$var" =~ \.sh$]] When you use =~ to extend a regular expression, the expression does not require a quoted number
If the string is compared, = = after the quotation mark is a string, without quotation marks is a wildcard character, therefore, this situation is more prone to self-masking. So the suggestion is this: use double brackets only when it comes to the need to use regular expressions.
BUG
The following is a bug in Bash oh, as long as the backslash appears in the regular expression, it will cause the match to fail, so the solution is to replace the regular expression content with a variable.
Here are 2 different case comparisons
Let's get a simple regular.
When testing conditions
-V Variable name checks whether the variable has been set
Centos 6 is not supported by- v
Num= ""; [-V num] && echo "Settings" | | echo "Not set"
Centos 7
Num= ""; [-V num] && echo "Settings" | | echo "Not set"
Group
The use of shell parentheses and curly braces in Linux is different
Parentheses ()
① Command Group. The commands in parentheses open a new child shell program, the variables in parentheses are local variables and cannot be used in other parts of the script. Multiple commands in parentheses are separated by semicolons.
The ② command is replaced. Command substitution $ (cmd) equals ' cmd '(this is not a single quotation mark, ' is the key below ESC '), the shell executes a $ (CMD) structure and then executes the cmd in $ (cmd) once to get its output, This output is then placed in the original command. For example:
This is an example, if you do not add the parentheses, after cd/app/dir/, then the PWD can see that the directory is actually entered, but when the PWD command in parentheses is finished, the command prompt displays the/app, which is the directory before the CD command. Since the CD command is acting on the child process. In practical scenarios, a child process is temporarily opened to perform some operations without affecting the current environment
The ③ is used to initialize the array. Example: Arr= (M N)
Curly braces {}
① expansion. Extend the file name in the curly braces. In curly braces, white space is not allowed, unless the whitespace is referenced or escaped. Expansion is divided into ordinary comma (,) to expand, such as echo {a,b}.txt The interval of the contents are listed; two dots (.. ) to expand, such as echo {1..5}.txt Auto-complete 1 to 5 intermediate content.
# echo {a,b}.txt
A.txt B.txt
# echo {1..5}.txt
1.txt 2.txt 3.txt 4.txt 5.txt
② internal Group. Unlike the commands in parentheses, the commands inside the curly braces run in the current shell and do not reopen the child shell. The commands within parentheses are separated by semicolons, and the last command must be followed by a semicolon. there must be a space between the first command and the opening parenthesis of the {}.
The parentheses in the shell, the brace structure, and the parenthesized variables are used in the following commands:
1.${var}
2.$ (CMD)
3. () and {}
4.${var:-string},${var:+string},${var:=string},${var:?string}
5.$ ((exp))
6.$ (Var%pattern), $ (Var%%pattern), $ (Var#pattern), $ (var# #pattern)
Now to one by one details:
1) The prototype of the variables in the shell
The most common variant form is $var, which prints var with the command
Echo $var
But here's the problem: when you want to display variable values with arbitrary characters (such as $varaa), an error occurs. The system will think that the whole varaa is a variable, then you can use a brace to define the scope of the variable name, such as ${VAR}AA, so it is good.
Now it's time to use {curly braces}.
2) command replacement $ (cmd)
The command replaces the $ (CMD) and the Sign ' cmd ' (note that this is not a single quotation mark, on an American keyboard, ' is the key under Esc '). The entire replacement process is illustrated with echo$ (LS): The shell scans the command line once, finds the $ (CMD) structure, and then $ (cmd) CMD executes once, obtains its standard output, and then puts this output in the original command echo $ (LS) in the $ (LS) position, that is, replace the $ (LS), and then execute the echo command. As follows:
3) A string of commands to execute () and {}
() and {} are executed on a string of commands, but differ:
A, () just re-open a sub-shell for a sequence of commands to execute
B, {} Executes a string of commands in the current shell
C, () and {} both put a string of commands in parentheses, and the commands are used between them ; Number separated
D, () The last command can be used without a semicolon
E, {} The last command to use a semicolon
F, There must be a space between the first command and the opening parenthesis of {}
G, Each command in () does not have to have spaces in parentheses
H, The redirection of one of the commands inside the brackets () and {} only affects the command, but redirects outside the brackets affect all the commands in the parentheses
Shell Programming Basics (ii)