In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Martin Streicher, the software developer of Pixel, Byte and and Comma, reveals the secrets of UNIX masters for us in this article. Martin Streicher is a freelance developer for Ruby on Rails and a former editor of Linux Magazine. Martin, who graduated from Purdue University with a degree in computer science, has been programming UNIX-like systems since 1986. He likes to collect works of art and toys.
Save environment variabl
Most UNIX users cram a large number of user settings into shell startup files such as .bashrc (for Bash shell) and .zshrc (for Z shell) to rebuild their beloved shell environment again and again. Startup files can create aliases, set shell options, create functions, and set environment variables. Key environment variables include HOME (pointing to your home directory), PATH (listing the directories from which to search for applications), and MANPATH (listing the directories from which to search for man pages). To see which environment variables are set in your shell, type the printenv command. Check the shell man page for a complete list of available environment variables.
Like shell, many other UNIX applications can be customized through environment variables. For example, the Java subsystem requires that a JAVA_HOME be defined to point to the root of the Java runtime. Similarly, the Amazon Web Services (AWS) utility suite forces the use of AWS_CREDENTIAL_FILE to point to a file that contains valid private key credentials. Separate applications also provide environment variables, and the key is how to find them. Fortunately, this kind of work does not need to be illegal; instead, simply consult the utility man page at hand and look for the chapter entitled "Environment Variables".
For example, the paging utility less defines several useful environment variables:
The ◆ environment variable LESS stores some command-line options to reduce the number of keys each time you call the pager. For example, if you need to read a large number of log files, you can add the following statement to an shell startup file:
Export LESS='--RAW-CONTROL-CHARACTERS-squeeze-lines-ignore-case'
The above option interprets the control characters (usually syntax coloring) separately, compresses multiple blank lines into one line, and ignores case in string matching. If you are using code, try the following options:
Export LESS='--LINE-NUMBERS-quit-if-one-screen-quit-on-intr'
The ◆ environment setting named LESSKEY points to a key binding file. You can use key bindings to customize the behavior of less, such as matching the behavior of another page or editor.
◆, like shell, less preserves the history of multiple calls. Set LESSHISTFILE and LESSHISTSIZE to point to a persistent command file and set the maximum number of commands to record, respectively.
GNU Compiler Collection (GCC) is another typical application example of environment variables. GCC defines various environment variables to customize its operations. LIBRARY_PATH, as its name implies, is a directory list that searches for libraries to link to; COMPILER_PATH works very similar to shell's PATH, but is used internally by GCC to find subroutines used during compilation.
If you write code for a single platform and build binaries, you may never use these environment variables, but if you cross-compile the same code across platforms, these variables are essential for accessing different headers and libraries for each platform. You can set these variables to different sets of values, one for one machine and another for another style of system.
In fact, you can get a hint from GCC that you can maintain multiple sets of environment variables for each application, switching from one set to another based on the task at hand. One way is to save an environment initialization file in each project directory and source it as needed. For example, many Ruby developers use this method to switch between different Ruby versions, changing the environment variables PATH, GEM_HOME, and GEM_PATH as needed to jump from one version to another.
"embellishment" environment
Much like environment variables, many Linux and UNIX applications provide a dot file-a small file whose name starts with a dot-to customize it. Unlike environment variables, environment variables collect a small number of tags and relatively little information, while point files may be more extensive, more complex, and have their own unique syntax rules, or even their own programming language. Dot files are an ideal place to save options and settings, because (according to UNIX tradition) filenames that start with a dot do not appear in the standard directory listing. Use ls-a to view these so-called hidden files. A dot file is a plain text file, but the file name is special.
Point files are usually located in your home directory, but some utilities also look for point files in the current working directory. If an application supports multiple point files, the program usually applies to precedence rules to indicate that one file takes precedence over another. Typically, "local" point files-located in the current working directory-have the highest priority, followed by point files in the home directory, and finally a system-wide configuration file. These files can exist all, one, or none of them, depending on whether the application treats them as mutually exclusive or incremental. In the first case, the priority of the first dot file in the priority chain is indisputable. In the latter case, the configuration can be cascaded or melted into the final result.
Less's key binding file is an example of a simple dot file, located in $HOME/.lesskey. Each line in the file is a pair (a button and a command), as follows:
\ r forw-line
\ nforw-line
E forw-line
J forw-line
^ E forw-line
^ N forw-line
K back-line
Y back-line
^ Y back-line
Fetchmail is a more complex example of a dot file. This utility extracts e-mail messages locally from multiple remote sources and delivers messages. The operation of this utility is controlled only by $HOME/.fetchmailrc. (see the man page for many of its options. Cron, git, vi, and many other commands recognize point files. Again, read the man pages of this application to learn what can be configured in the point file. Some of the files are rich enough to occupy a separate man page, such as crontab.
Shh. Secrets about SSH
Secure Shell (SSH) is a powerful subsystem for securely logging in to remote systems, copying files, and traversing firewalls. Because SSH is a subsystem, it provides a number of options to customize and simplify its operations. In fact, SSH provides an entire "dot directory" called $HOME/.ssh to contain all its data. Your .ssh directory must be in mode 600 to prevent others from accessing it. Non-600 mode will interfere with normal operation. In particular, the file $HOME/.ssh/config can define a number of shortcuts, including aliases for machine names, per-host access control, and so on.
Here is a typical block of code located in $HOME/.ssh/config to customize the SSH for a particular host:
Host worker
HostName worker.example.com
IdentityFile / .ssh/id_rsa_worker
User joeuser
Each block in the ssh/config configures one or more hosts. Different blocks are separated by a blank line. This block uses four options: Host, HostName, IdentityFile, and User. Host creates a nickname for the machine specified by HostName. Nicknames allow you to type ssh worker instead of ssh worker.example.com. In addition, the IdentityFile and User options specify how to log in to worker. The former points to a private key used by this host, while the latter provides login ID. In this way, this code block is equivalent to the following command:
Ssh-I / .ssh/id_rsa_worker
ControlMaster is a little-known and powerful option. If set, multiple SSH sessions of the same host will share a single connection. Once the first connection is established, subsequent connections no longer require credentials, eliminating the hassle of typing a password each time you connect to the same machine. ControlMaster is very convenient, and you might want to enable it for each machine. Enabling is as simple as using the host wildcard *:
Host *
ControlMaster auto
ControlPath ~ / .ssh / master-%r@%h:%p
As you might expect, blocks marked Host * will be applied to every host, even those that are not explicitly specified in the configuration file. ControlMaster auto attempts to use an existing connection and creates a new connection if no shared connection is found. ControlPath points to a file to persist a control socket for sharing. % r is replaced with the Telnet user name, h with the target hostname, and p instead of the port used by the connection. (you can also use% l, which is replaced with a local hostname. The above specification creates a control socket using a file name similar to the following
: 22
When all connections to the remote host are cut off, each control socket is removed. If you want to know which hosts you are connected to at any time, just type ls ~ /. Ssh and look at the hostname section (% h) of the control socket.
The SSH configuration file is very large and has its own man pages. Type man ssh_config to view all possible options. Here's a clever SSH trick: you can access a remote system from a local system through SSH. The command line to be used is as follows:
$ssh example.com-L 5000:localhost:3306
This command means to connect through example.com and establish a channel between port 5000 on the local machine and port 3306 (the MySQL server port) on the machine named "localhost". Because localhost is interpreted on example.com (because the channel is established), localhost is example.com. Because the outbound channel-formerly known as local forwarding (local forward)-is established, the local client can connect to port 5000 and communicate with the MySQL server running on the example.com.
The general form of channel creation is as follows:
$ssh proxyhost
Localport:targethost:targetport
Where proxyhost is a machine that can be accessed through SSH and has a network connection to targethost (not through SSH). Localport is an unprivileged port on your local system (any unused port above 1024), and targetport is the port of the service you are connecting to.
The previous command is sent from your machine to the outside world. You can also send it in using SSH, or connect to your local system from the outside world. The general forms of inbound channels are as follows:
$ssh user@proxyhost-R proxyport:targethosttargetport
When you set up an inbound channel-formerly known as remote forwarding-the roles of proxyhost and targethost are reversed: the target is your local machine and the agent is the remote machine. User is your login name on the agent. The following command provides a concrete example:
$ssh-R 8080:localhost:80
This command means that the user Joe connects to example.com and connects the remote port to local port 80. This command provides users on example.com with a channel to connect to Joe's machine. Remote users can connect to 8080 in order to connect to the Web server on the Joe machine.
In addition to-L and-R for local and remote forwarding, SSH provides the-D parameter to create a HTTP agent on the remote machine. See the SSH man page for the correct syntax.
Rewrite with history
If you often spend a lot of time at the shell prompt, saving shell history can save time and typing. But if the history cannot be modified, it can cause some trouble: recording duplicate commands, and multiple shell instances may interfere with their own history. These two problems are easy to solve, just add two lines to your .bashrc:
Export HISTCONTROL=ignoreboth
Shopt-s histappend
The first line removes successive repeating commands from your shell history. If you want to remove all scattered copies, change ignoreboth to erasedups. The second line appends the history of shell to your record file when shell exits. By default, the Bash record file is named ~ /. Bash_history (yes, this is a dot file). You can change its location by setting HISTFILE (yes, this is an environment variable). If you want to save a shell's last 10000 command in a record file containing 100000 entries, add export HISTSIZE=10000 HISTFILESIZE=100000 to your shell startup file. To view the history of a shell, type history at any prompt.
If it cannot be called, the saved command history is of little use. And that's what the shell! (or bang) operator is for:
*! ("bang bang") repeat the last command in its entirety.
*!: 0 is the name of the previous command.
*! ^ is the first argument to the previous command. !: 2, 3... ! $and other orders are the second and third of the previous order. And the last parameter.
*! * are all arguments to the last command, except for the command name.
*! n repeats the command numbered n in history.
*! handle repeats the last command that starts with a character in handle. For example,! ca repeats the last command that starts with the character ca, such as cat README.
*!? handle repeats the last command that contains a string of characters in handle. For example,!? READ also matches cat README.
* ^ ideal ^ replaces the first instance of original with substitution. For example, if the previous command is the cat README, command ^ README ^ license.txt, a new command cat license.txt will be generated.
*!: gs/original/substitution will replace all instances of original with substitution (!: gs means "global replace [global substitution]").
*!-2 is the penultimate command,!-3 is the penultimate command, and so on.
You can even merge historical expressions to generate a "magic soup" like 2:0-R! ^!-3:2, which extends to the name of the penultimate command, plus-R, plus the first argument of the previous command, and the second argument of the penultimate command. To make such mysterious commands more readable, you can extend the historical reference as you type. Type the command bind Space:magic-space at any prompt, or add it to a startup file to bind the spacebar to the function magic-space, which extends the inline history replacement.
Automatic decompression independent of extension
With so much code on Internet, you may download dozens of files every day. It is possible that all those files are packaged in different ways-some ZIP files, some RAR files, and many tarball files, although each package is compressed using a different utility. Remembering how to unzip and expand each package format will be exhausting. So why not do all those tasks in a single command? The following function is widely available in many sample point files:
Ex () {
If [- f $1]; then
Case $1 in
* .tar.bz2) tar xjf $1
* .tar.gz) tar xzf $1
* .bz2) bunzip2 $1
* .rar) rar x $1
* .gz) gunzip $1
* .tar) tar xf $1
* .tbz2) tar xjf $1
* .tgz) tar xzf $1
* .zip) unzip $1
* .Z) uncompress $1
* .7z) 7z x $1
*) echo "'$1' cannot be extracted via extract ()"
Esac
Else
Echo "'$1' is not a valid file"
Fi
}
This function ex extends 11 file formats; it can also be extended if you want to deal with other package types. Once defined-for example, in a shell startup file-you can simply type ex somefile, where somefile ends with one of the following named extensions:
$ls
Source
$tar czf source.tgz source
$ls-1
Source
Source.tgz
$rm-rf source
$ex source.tgz
$ls-1
Source
Source.tgz
By the way, if you misplaced the file you downloaded today, you can run find to find it:
$find ~-type f-mtime 0
The command-type f looks for plain text files, and-mtime 0 looks for files created since midnight of the day.
More secrets.
There are many expert secrets that need to be uncovered. Search Web for "shell auto-complete" to learn more about the autocomplete feature, which is used to provide context-sensitive extensions when you type a command. In addition, search "shell prompts" to learn how to customize your shell prompt: you can set it to color, you can set your current working directory or Git branches, and you can display the number of history-a convenient reference if you call history frequently. To see a working example, search Github for "dot files". Many experts post their shell configurations on Github.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.