Script comments:
in a shell script, a comment followed by the (#) number, used to annotate the script, the comment part is not executed, the comment can be self-contained, or it can be followed by the same line as the command after the script command. When developing scripts, without comments, it's hard for others to understand what the script is doing and forget about it for a long time. Therefore, we have to make as much as possible to develop for the work (script, etc.) to write the annotation habit, not only for the convenience of others, but also convenient for themselves. Otherwise, after writing a script may not remember the purpose of the script a few days later, in re-reading will also waste a lot of valuable time.
Execution of the script
Shell The execution of a script can typically take the following three ways:
1) Bash script-name or SH script-name
2) path/script-name or./script-name (Execute script under current path)
3) Source script-name or. script-name
Executive notes:
The first approach is that when the script file itself does not have the means to use it frequently, it is recommended to do so in bash.
The second method requires that the permissions of the script file be changed to executable by: chmod u+x script-name or chmod 755script-name. Scripts can then be executed directly through the script path.
in the production environment, many readers after writing the shell script, because forget to give the script to execute permissions, the direct application, the result of the script does not follow their own will be executed manually or timed, it is important to note that the avoidance method is to replace the second method with the first method.
The third method is by using source or "." Read in or load the specified shell script file, and then execute all the statements in the specified shell script file, in turn. Use source or "." Can pass the return value of a variable in its own script, such as a value or function, to the current parent script. This is the biggest difference from the previous two methods.
Below we illustrate:
The first method:
[Email protected]]# cat >test.sh
Echo ' I amsky9899 '
[Email protected]]# cat test.sh
Echo ' I amsky9899 '
[Email protected]]# sh test.sh
I amsky9899
[Email protected]]# bash test.sh
I amsky9899
The second method:
[Email protected]]# ls-l test.sh
-rw-r--r--. 1 root root 20 February 103:23 test.sh
[Email protected]]#./test.sh
BASH:./TEST.SH: Insufficient Authority
[Email protected]]# chmod u+x test.sh
[Email protected]]# ls-l test.sh
-rwxr--r--. 1 root root 20 February 103:23 test.sh
[Email protected]]#./test.sh
I amsky9899
The third method:
[Email protected]]# Source test.sh
I amsky9899
[Email protected]]#. test.sh
I amsky9899
Echo ' I am sky9899 '
[[email protected] sky]# echo ' userdir= ' pwd ' > testsource.sh
[email protected] sky]# cat testsource.sh
Userdir= ' pwd '
[[Email protected] sky] #sh testsource.sh
[[Email protected] sky] #echo $userdir
#此处为空, and the current path does not appear.
According to the above example, we can find that the script executed by SH or bash command, after the end of the script in the current shell window to see the value of the Userdir variable, found that the value is empty. Now use the same steps instead of source execution, and then look at the value of the Userdir variable.
[Email protected] sky]# source testsource.sh
[Email protected] sky]# echo $userdir
/home/sky
Note: through source or "." The point number loads the executed script, and the value of the variable in the script is still present in the current shell after the script finishes, while SH and bash do not. Therefore, when doing shell script development, it is best to use "." If there is a requirement in the script to refer to other script content or configuration files. The dot or source loads the script or configuration file at the beginning of the script, and then the contents of the script and the variables and functions in the file can be loaded by invoking source.
Linux Operations position actual interview written questions (3)
[email protected] sky]# cat test.sh
User= ' WhoAmI '
[Email protected] sky]# sh test.sh
[Email protected] sky]# echo $user
Q: What is the result of executing the echo $user command?
Answer:
1) Current User
2) Sky
3) empty (no content)
Basic specifications and habits of Shell script development
1) Start specifying the script interpreter
#! /bin.sh or #!/bin/bash
p) information such as the beginning plus version copyright
#Date:
#Author:
#Mail:
#Function:
#Version:
3) There is no Chinese comment in the script:
As far as possible in English comments, to prevent the computer or switching system environment after the confusion of Chinese characters.
4) The script has an. sh extension:
script-name.sh
5) code writing good habits:
{}, [], ', ', ', '
This article is from the "Linux~dba~mba" blog, make sure to keep this source http://sky9896.blog.51cto.com/2330653/1610345
Shell script Programming One