Do not leave environment variables in Unix on the bench

Source: Internet
Author: User
Article Title: Do not leave Unix environment variables on the bench. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

The position of environment variables in Unix operating systems does not need to be discussed by the author. However, in Unix operating systems, several environment variables are often ignored. I sometimes don't know the environment variable when I talk to some experienced System Engineers. I will introduce these ignored environment variables and their applications today. Helps you scan blind spots.

1. prevent accidental file overwriting.

In a Unix operating system, a special symbol is called a redirection character. With this symbol, You can redirect some results or other content displayed on the screen to a file. Obviously, this redirection symbol is very popular with System Engineers. By using this symbol, you can easily save all the file names in the current directory to one file. However, this symbol also has a defect. If a file already exists, when the system engineer creates a file with the same name using the redirection symbol, the original file will be overwritten without any prompt. Obviously, if you are not careful during the operation, it will lead to irreparable losses. Sometimes, to avoid this problem, our system engineers have to check whether the file exists in the current directory before using the redirection symbol to create a file.

For this reason, sometimes we want to create a file using the redirection symbol, by default, the file is not overwritten. If the target file already exists, the system will prompt an error message. Unless you specify parameters in the redirection symbolic command to overwrite the original file. Can this requirement be met? Of course, the answer is yes. This requires an environment variable noclobber parameter ignored by everyone.

If we need to implement the above requirements, we can make the following settings. Set? O noclobber. This command is used to protect files. When the target file to be created for redirection already exists, the system will prompt an error message (the file already exists) and refuse to create the file. In this case, the target file will not be accidentally overwritten. However, sometimes System engineers may know the existence of the target file. For example, the same command is executed twice before and after the process is viewed. In this case, the system engineer wants the redirection symbol to overwrite the original file. At this time, the system engineer does not need to cancel this protection. You only need to add a | parameter after the redirection symbol. Such as ls *. jpg> | image.txt. This command indicates all JPG files in the current directory and saves the names to the image.txt file. If the target file already exists, overwrite it.

Obviously, this noclobber environment variable parameter provides an unexpected file protection mechanism. If the Unix operating system is used as a server or for development, we recommend that you enable this environment variable parameter. In these applications, many redirection symbols are used. Sometimes the target file may already exist, but the file may be accidentally overwritten due to negligence. At that time, the system engineer will regret it again. Therefore, if this parameter is enabled at this time, the resulting losses can be minimized. For this reason, I suggest you enable the environment variable that has been forgotten in the corner in these scenarios to improve file security.

 Ii ,~ .

~ In a Unix operating system, it is called a replacement character. This is also an environment variable symbol that is ignored but useful in the Unxi operating system. In short, this character can be used to represent the main directory. For example, a new user Jussin is created on the Unix operating system and a mail client is installed on it. By default, this email is saved in the user's home directory (if the installation path is not changed during installation ). Under a privileged user, the system engineer needs to switch to the user's home directory immediately for mail maintenance. In this case, if you do not have this ~ For the help of character generation, the system engineer had to first confirm the user's home directory, and then use the cd command to go in layer by layer. This operation is obviously troublesome. In fact, there is a simpler method in Unix operating systems. If you only need to input in the command line, cd ~ Jussin, the system will automatically log on to the user's home directory. Therefore, we use this character to represent the user's main directory. The traditional operations just introduced here are much easier.

When ~ If the user name is not added after the symbol, but the/symbol is directly added without any content, it indicates the Home Directory of the current user. For example, System Engineers often need to modify user configuration files for some purposes. In this case, the system engineer needs to use the vi command to edit the configuration file, such as vi $ HOME/. profile. This command is used to open the. profile configuration file under the current user's home directory. When you enter this command, HOME must be capitalized to indicate the environment variable. It is troublesome to enter this command. First, you need to use the special $ symbol, and second, you need to convert the case-sensitive mode. The vi command must be in lower case, while the HOME environment variable must be in upper case. Therefore, it is inconvenient to input data. In fact, System Engineers can also use ~ This character is used to simplify the input. For example, with the help of this generation of characters, the above command can be simplified to vi ~ /. Profile. This saves the trouble of case-insensitive conversion.

In addition, we can also use ~ Quickly switch to the current user's home directory. Such as cd ~ Switch to the current user's home directory. This is very convenient to use. In addition, System Engineers can also use cd ~ -Command to switch to the latest directory. That is to say, the preceding two commands can be used to quickly switch between the recently used directory and the current user's home directory. This is very convenient for daily maintenance work. Sometimes System Engineers often need these two commands when developing embedded programs. Because it can easily switch between the user's home directory and the current directory. So, this ~ Most of its System Engineers ignore the character generation, and its role cannot be underestimated. However, its disadvantage is that its versatility is not very good. As far as I know, not all shells support this character generation. This generation character is not supported in the Bourne shell, for example. For example, use vi ~ /. Profile command. An error message is displayed. This is the biggest defect of this character generation. System Engineers should pay attention to this universal problem during use. If you do not consider this issue, this character generation is really a good environment variable.

3. Size of historical variables.

In shell, we press the direction key to display the commands that have been executed recently. Sometimes, we need to repeat a command for testing. If this command is too long, the historical command will be very useful. System Engineers only need to use the arrow keys to find the commands previously executed, and then press the Enter key. For example, a system engineer may have compiled a script program for file backup. During the compilation process, you need to constantly execute this script program to improve its functions. At this time, the history command function can be used to save the trouble of every command input.

In general, the executed command is stored in the hard disk or memory. As the price of memory and hard disk declines, System Engineers often have large hard disks and memory on Unix servers. For this reason, the system engineer can set the history file to a large value to save more historical commands. For example, a system engineer can change the HISTSIZE environment variable to display the History Length in the memory. You can change the environment variable HISTFILESIZE to the size of history files stored on the hard disk.

In general, I suggest you modify these two environment variables. This not only simplifies the input by System Engineers, but also allows you to view historical records to query previous maintenance operations on the operating system. This is of some significance for operating system troubleshooting. If necessary, the system engineer can also specify the location where historical records are stored as needed. For example, you can set the HISTFILE environment variable to determine the location and file name of the historical file stored on the hard disk. If this environment variable is not set, the history file is saved in the user's home directory by default. That is to say, each user has his/her own history file. Commands executed in each user's shell are recorded in their respective history files, and are not common to each other.

Although these environment variables are only used to implement some specific functions, their functions cannot be ignored. Setting these environment variables together not only improves file security, but also greatly facilitates our operations. For this reason, I suggest that, if you want to let these environment variables sit on the bench again, you should try to make full use of the value of these environment variables. This makes our work easier.

 

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.