Differences in how scripts are executed
Use direct execution or bash to execute script
When executing a script using direct commands or using bash(sh), the script uses a new sub-bash in-program.
source executes the script
Using source to execute scripts is executed in the parent program.
test command to test the function
The test command is used to detect the relevant properties of some files on the system, such as detecting whether the file exists, what the permissions are, and so on. Use the format:
test [options] filename
The following option attributes are introduced, you can use the man test command to check in the help document
- -e whether the document exists
- -f whether the document exists and is "file "
- -d whether the document exists and is a directory (directory ) )
- -r Does the document exist and has read permission
- -w Does the document exist and have write permission
- -x Does the document exist and have read permission
- ! indicates the reverse state.
- -a(and) both conditions are met at the same time, return true
- -o(or) either of the two conditions is met, return true
- -z(zero)Determine whether the string is 0? If the string is not empty, return true
- -n (nonzero) to judge whether the string is non-zero, if the string is empty, return false, which can be omitted
shell script的debug
Before the script is loaded, I am most afraid of syntax errors. Debug can be judged by using the relevant parameters of bash.
bash [-nvx] hello.sh
Option parameter meaning description:
- -n: do not execute the script, only query the problem of syntax
- -v: output the script content to the screen before executing the script
- -x will use moral script content to display to the screen -