1. debugging
1. Set options
(1) bash-N scriptname does not actually run the script, but only checks the syntax error of the specimen,
You can also add set-N or set-O noexec to the script.
(2) bash-V scriptname prints the content of this command or script before executing a command or script,
You can also add set-V or set-O verbose to the script.
(3) bash-x scriptname prints the execution result of each command, set-X or set-O xtrace
2. $ lineno and caller
(1) $ lineno is equivalent to _ line _ in VC __
(2) caller
#! /Bin/bash
Func ()
{
Caller 0
}
Func
Exit 0
Put caller in the function to output the row in which the function is called and in which function the function is called.
And the command used to call the script.
Eg: 10 main./test. Sh
3. Write an Assert function in C Language
#! /Bin/bash
Assert ()
{
E_args = 98
E_assert_fsiled = 99
If [-z "$2"]; then
Return $ e_args
Fi
Lineno = $2
If [! $1]; then
Echo "assertion failed: file \" $0 \ ", line $ lineno, \" $1 \""
Exit $ e_assert_failed
Fi
}
A = 5
B = 4
Assert "$ A-lt $ B" $ lineno
# Other command
Echo "Hello shell"
Exit 0
4. Trap capture Signal
Trap 'command' SIG
Trap 'echo Ctrl-C' 2
Trap 2 # restore
Trap 'COMMAND 'debug # Execute Command once every line of script executed by debug
2. Bash options
Mechanisms used to change shell or script Behavior
Bash [-optiion abbreviation] Or bash-O [Option name]
Abbreviations |
Name |
Function |
-C |
Noclobber |
Prevent overwriting files during redirection (this function will be> |Overwrite) |
-D |
(None) |
List strings with a $ prefix caused by double quotation marks, but do not execute the commands in the script |
- |
Allexport |
Export all Defined variables to Environment Variables |
-B |
Notify |
A notification is given when the background task is terminated (not much is used in the script) |
-C... |
(None) |
Slave...READ command |
-E |
Errexit |
Stop the script when the first error occurs. That is, exit the script when a command returns a non-zero value (except until or while loops, if-tests, list constructs) |
-F |
Noglob |
Disable file name replacement (a symbol like * cannot be replaced with a file name) |
-I |
Interactive |
Make the script run in interactive mode |
-N |
Noexec |
Read commands from scripts but do not execute them (syntax check) |
-O option-name |
(None) |
CallOption-nameOption |
-O POSIX |
POSIX |
Change bash or script behavior to conform to POSIX standards. |
-P |
Privileged |
Script"SUID"ProgramTo run(Be careful !) |
-R |
Restricted |
The script runs in restricted mode (See Chapter 21st ). |
-S |
Stdin |
Read commands from standard input |
-T |
(None) |
Exit after the first command. |
-U |
Nounset |
An error message is generated when an undefined variable is used and the script is forcibly exited. |
-V |
Verbose |
Print the command to standard output before executing the command |
-X |
Xtrace |
And-V SimilarityBut print the complete command |
- |
(None) |
Mark of the end of the Option List. The following parameter is the positional parameters parameter ). |
-- |
(None) |
Release location parameter. If the parameter list is specified (-- Arg1 arg2), The location parameters are set to values in the parameter list in sequence. SA |
Tips
1. scripts can be called like common commands embedded in another shell script, such as TCL or wish scripts or even makefile. They can be called as external shell commands using the C system () function.
2. put the commonly used custom variables and functions in the file. When necessary, use the dot in the script. or the source command to contain one or more of these "library files.
# Mybashlib. Sh
Root_id = 0
E_noroot = 101
Check_is_root ()
{
If ["$ uid"-ne "$ root_id"]; then
Echo "must be root to run this script ."
Exit $ e_noroot
Fi
}
# Test. Sh
#! /Bin/bash
./Home/zone/mybashlib. Sh # contains the "Library File". In fact, this sentence is equivalent to executing the script file.
# Therefore, it is best not to have some executable statements in the "library file", but to define some functions and variables.
Check_is_root # Call a function
Exit 0
3. NotesCodeBlock
(1) Here document
: <Commentblock
Echo "this line will not echo"
======================================
Echo "Hello World"
======================================
Commentblock
(2) If-Test Structure
#! /Bin/bash
Comment_block =
If [$ comment1_block]; then
======================
Echo "comment"
======================
Fi
# Control the comment switch by giving the variable comment_block
4. Return the function value to the script. One method is return num, but num must be 0 ~ 255; another method
Is to let the function write value to the standard output, this method can not only return integers, strings, but also can return
Multiple values.
Eg
#! /Bin/bash
Sum_and_product ()
{
Echo $($1 + $2) $($1*$2 ))
}
Sumproduct = $ (sum_and_product 10 100)
Sum = $ (echo "$ sumproduct" | awk '{print $1 }')
Product = $ (echo "$ sumproduct" | awk '{print $2 }')
Exit 0
5. Pass the array parameter to the function and return the array from the function.
Arg = $ (echo "$ {arr [@]}"), and then pass the other Arg to the function.
You can use the same method when returning an array.