Notes for Unix Bourne shell

Source: Internet
Author: User

recently, I have been engaged in Unix shell and read a lot of books and materials in this process, therefore, we will record some of the details to be noted for the purpose of memo and reminder. The shell here refers to the Bourne shell. I have never used other types of shell, so I don't know if the same problem exists. In addition, the basic information about Unix shell is not described here. In fact, the relevant information on the internet is scattered. In addition, shell itself involves a lot of things, including UNIX system knowledge, it is not easy to clarify the syntax and functions, regular expressions, and pipelines of shell of different versions at a time. Here, we recommend a book "Unix shell example explanation", which helps a lot in the process of learning shell. OK. start to forget ~

1. Environment Variables
The scope of environment variables is from top to bottom, and the environment variables changed in the child process are not passed back to the parent process. Therefore, the global environment variables should be defined in the parent process. The environment variables defined by the child process or other processes cannot be shared. This feature of environment variables for ShellProgramThe structure has a great impact. If you want to use environment variables as global variables, you must maintain a downward call relationship between shell scripts.
Shell does not have the concept of global variables, but only environment variables and local variables. There are several solutions for global variables:
A. Maintain a downward call relationship between shell scripts and define environment variables in the parent process;
B. Use read-only local variables and ensure that all scripts are executed in the same process;
C. Save the variable in a config file and load it by executing the script;
Or the global variables mentioned here are a bit constant. After all, global variables can share information between processes, but none of the methods mentioned above can. It is estimated that only parameters and return values are used for inter-process communication. In the real sense, how to implement global variables is not discussed at the moment (because I do not have this requirement yet, hee)
In fact, the main process that loads environment variables to Unix can also directly use them as global variables. This method is a special use of the first solution.

2. It is best to use the "." Call script after receiving parameters, otherwise it may cause changes to the values of the "$ #" Special variables.

3. When "[]" is used in the IF statement to replace test, note that "[]" uses spaces. For example, if ["$ "! = ""] Then # spaces cannot be omitted

4. During CP-R, note that the copied directory name is not required in the target path. For example: CP-R/A/B/will copy directory A to directory B, and the copied directory is still called a (this is different from xcopy)
In addition, CP does not automatically create the target directory.

5. When using while to read files, pay attention
When using
While read line
Do
#####
Done <File
When reading a file, it is found that the last row of the file will not be read within the while. I don't know if this problem is above.Code....

6. Special Functions "()" "{}"

Note the following methods to obtain the path of the script: currdir = $ (CD "$ (dirname" $0 ")"; PWD)

This statement is used to return the path of the currently executed script, rather than the execution path. "()" Means executing dirname, CD, PWD, and other commands as sub-processes, but does not change the context of the process. The preceding example shows that the script runs $ (dirname "$0") first because there are two nested parentheses "). $0 indicates the name of the currently executed script, and dirname $0 indicates the path to obtain the script (absolute or relative path, depending on how you call the execution script ). $ (CD "**"; PWD): The nested foreign language sentence is used to enter a path and then return the absolute path of the path. As mentioned above, "()" is used to partition context. Therefore, the CD command in $ (CD "**"; PWD) does not change the current path of the script. The principle of the entire statement is probably the same thing.

"()" Is used to group statements into statement blocks, and then the subprocess runs the statement blocks. The context changed by the subprocess is not passed back to the current process.

The role of "{}" is similar to that of "()", but it transmits context changes to the current process. More directly, "{}" uses the same process environment as the current process.

7. Pay attention to the differences between windows and UNIX file formats

In Windows, the line Identifier of the file is "\ r \ n" and "\ n" in UNIX ". If the shell script is used, it cannot be run in Windows format. If the script uses a configuration file, the format problem also affects the script to identify the configuration file string.

8. grep, awk, and sed are powerful commands. They can be combined with other commands through pipelines to complete many complex tasks.

 

9. Shell related documents and manuals:

R2's shell tutorials

Http://www.injunea.demon.co.uk/index.htm

UNIX Power Tools

"Href =" http://docstore.mik.ua/orelly/unix/upt/index.htm "> http://docstore.mik.ua/orelly/unix/upt/index.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.