The application of environment variables in UNIX

Source: Internet
Author: User
Tags command line mail

The position of environment variables in UNIX operating systems Needless to say, we all know. But in the UNIX operating system, there are several environmental variables that are often overlooked. I sometimes communicate with some of the more experienced Systems Engineering division, they do not know that there is this environment variable. I would like to introduce to you today the neglected environment variables and their applications. help you sweep the blind spot of understanding.

First, prevent the file from being accidentally overwritten.

In the UNIX operating system, there is a very special symbol called a redirection character. With this symbol, you can redirect the results of some screens or other content to a file. Obviously this redirection symbol is very popular with system engineers. Using this symbol, you can easily save all the file names in the current directory to a file. However, this symbol also has a flaw. If a file already exists, the system engineer can now use the redirection symbol to create a file with the same name, and the original file will be overwritten without any hint. Obviously, if the operation is careless, it will bring irreparable loss. Sometimes, in order to avoid this situation, our system engineers, when using the redirection symbol to create a file, have to look at whether the file exists in the current directory.

For this reason, sometimes we want to create a file by using the redirection symbol, by default, the file is not overwritten. If the target file already exists, the system prompts for an error message. Unless the user uses parameters to specify that the system overwrites the existing file in the redirection symbol command. Can this demand be realized? The answer is certainly yes. This requires an environment variable Noclobber parameter that is ignored by everyone.

If we need to implement this requirement, we can set the following. Set–o Noclobber. The meaning of this order is to take a protective measure of the document. When a redirection needs to be made to a target file that already exists, the system prompts the error message (the file already exists) and refuses to establish the file. In this case, the target file will not be accidentally overwritten. However, sometimes the system engineer may know the object file exists. When you view a process, the same command is executed two times before and after. At this point the system engineer would like to overwrite the original file with the redirected symbol. At this point, the system engineer does not need to cancel this protection measure. Instead, just add a | parameter to the redirection symbol. such as LS *.jpg >| Image.txt can be. The meaning of this command is to display all the JPG files in the current directory and save their file names to the Image.txt file. If the target file already exists, overwrite it.

Obviously this noclobber environment variable parameter provides a mechanism for the accidental protection of a file. If the UNIX operating system is used for server or development purposes, the author recommends enabling this environment variable parameter. Because in these applications, redirect symbols are used in particular. Sometimes it may be that the target file already exists, but it may inadvertently cause the file to be accidentally overwritten, and then the system engineer will regret it. So, when you enable this parameter, you can minimize the resulting loss. To this end, I suggest that in these occasions, it is best to enable the forgotten in the corner of the environment variables to improve the security of the file.

The substitution effect of the ~.

~ is called a character in the Unix operating system. This is also an environment variable symbol that is neglected in the Unxi operating system but is very useful. In simple terms, this generation character can be used to represent the home directory. If you are creating a new user Jussin on a UNIX operating system, and have a mail client installed for it. By default, this message is saved in the home directory of its users (if the installation path is not changed during installation). At this point under the privileged user, the system engineer needs to switch immediately to the user's home directory for mail maintenance. At this point, without the help of this ~ character, the system engineer had to confirm the user's home directory before using the CD command layer. Obviously this operation will be more troublesome. In fact, in the UNIX operating system, there is a more convenient way. If you only need to enter on the command line, the CD ~jussin, the system will automatically log in to the user's home directory. So, we use this generation character to represent the user's home directory. The traditional operation just introduced is much simpler.

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.