In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what are the skills for the use of Shell in Linux". In daily operation, I believe that many people have doubts about the skills of using Shell in Linux. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the skills for the use of Shell in Linux?" Next, please follow the editor to study!
It is too troublesome to enter a similar file name
Strings enclosed in curly braces are concatenated with commas and can be extended automatically, which is very useful. Look directly at the example:
$echo {one,two,three} file onefile twofile threefile $echo {one,two,three} {1pm 2pm 3} one1 one2 one3 two1 two2 two3 three1 three2 three3
You see, each character in the curly braces can be combined with the string after (or before). Note that the curly braces and the commas in them cannot be separated by spaces, otherwise they will be treated as ordinary strings.
What is the practical use of this technique? The simplest and most practical thing is to extend parameters to commands such as cp,mv,rm:
$cp / very/long/path/file {, .bak} # make a copy of file called file.bak $rm file {1rec 3je 5} .txt # Delete file1.txt file3.txt file5.txt $mv *. {CML CPP} src/ # move all files with suffixes .c and .cpp into the src folder
It is too troublesome to enter a path name
Use cd-to return to the directory you were waiting for, and look directly at the example:
$pwd / very/long/path $cd # go back to the home directory to see $pwd / home/labuladong $cd-# and then return to the previous directory $pwd / very/long/path
Special command! $will be replaced with the last path of the last command, just look at the example:
# No executable permission $/ usr/bin/script.sh zsh: permission denied: / usr/bin/script.sh $chmod + x! $chmod + x / usr/bin/script.sh
Special command! * will be replaced with all the file paths entered by the last command, just look at the example:
# create three script files $file script1.sh script2.sh script3.sh # add executable permissions to them all $chmod + x! * chmod + x script1.sh script2.sh script3.sh
You can add your commonly used working directory to the environment variable CDPATH. When the cd command cannot find the file / directory you specify in the current directory, it will automatically look in the directory in CDPATH.
For example, I often go to the home directory and / var/log directory to find logs. You can execute the following command:
The $export CDPATH='~:/var/log' # cd command will expand the search for $pwd / home/labuladong/musics $cd mysql cd / var/log/mysql $pwd / var/log/mysql $cd my_pictures cd / home/labuladong/my_pictures in the ~ directory and / var/log directory
This technique is very useful, so that you don't have to write a complete path name often and save a lot of time.
It should be noted that the above operations are supported by bash. Of course, other major shell interpreters support extending the search directory of the cd command, but they may not modify the variable CDPATH. You can search the specific setting method.
It is too troublesome to enter repeated commands
Use a special command!, which can be automatically replaced with the last command:
$apt install net-tools E: Could not open lock file-open (13: Permission denied) $sudo! Sudo apt install net-tools [sudo] password for labuladong:
Some commands are so long that I can't remember the specific parameters for a while.
For bash terminals, you can use the Ctrl+R shortcut to reverse the search history command, which is called a reverse search, which is to search for the last command entered.
For example, after pressing Ctrl+R, typing sudo,bash will search for the last command that contains sudo. After you enter, you can run the command:
(reverse-i-search) `sudo': sudo apt install git
But this approach has its drawbacks: first, this function seems to be supported only by bash, but I can't use zsh as a shell terminal; second, I can only find one (recent) command, and if I want to find a previous command, I can't help it.
In this case, the most common method is to use the history command with the pipe character and the grep command to find a history command:
# filter out all historical commands that contain config fields: $history | grep 'config' 7352. / configure 7434 git config-- global-- unset https.proxy 9609 ifconfig 9985 clip-o | sed-z's /\ nclip,\ nclip g' | clip 10433 cd ~ /. Config
All shell commands you use will be recorded, and the previous number indicates which command you want to reuse. After finding the command you want to reuse, you do not need to copy and paste the command, just use the command number you want to reuse to run the command.
Taking the above example, I want to rerun the git config command, and I can do this:
$! 7434 git config-- global-- unset https.proxy # run completed
I think history plus pipe plus grep is still too much typing. You can write a function like this in your shell configuration file (.bashrc, .zshrc, etc.):
His () {history | grep "$@"}
In this way, you don't need to write so much, just his' some_keyword' to search for history commands.
I don't usually use bash as a terminal. I recommend a very useful shell terminal called zsh, which is also my own shell. This terminal can also expand a variety of plug-ins, very easy to use, the specific configuration method can be searched on its own.
Other tips
1. The yes command automatically enters the character y to confirm:
When we install some software, we may have interactive questions:
$sudo apt install XXX... XXX will use 996 MB disk space, continue? [y/n]
Usually we go all the way to the end, but it's annoying if we want to automate the installation of some software. when we encounter this kind of interactive question, we get stuck and have to deal with it manually.
The yes command can help us:
$yes | your_cmd
In this way, it will go down automatically all the way, and it won't stop for us to type.
If you have read the underlying principles of Linux processes, threads, and file descriptors, you know that the principles are simple:
You run the yes command alone and find that it prints out a lot of characters y and pipes the output to the standard input of your_cmd. If your_cmd asks boring questions again, it will read the data from the standard input and will read a y and newline character, which is the same effect as you manually enter y to confirm.
2. Special variable $? Record the return value of the last command.
In Linux shell, following the custom of C language, if the return value is 0, the program exits normally, and if it is not 0, it exits abnormally. Reading the return value of the last command feels useless on the command line, but if you want to write some shell scripts, it's useful to know the return value.
To take a practical example, such as my Github warehouse fucking-algorithm, I need to add three footer links to the bottom of all the markdown files. Some articles already have footers, but most of them don't.
To prevent repeated additions, I must know if a md file has been added at the end, so I can use $? Variables cooperate with the grep command to:
#! / bin/bash filename=$1 # check whether the end of the file contains the keyword tail | grep 'next' $filename # grep returns 0 if it finds a match, and returns a non-zero value [$?-ne 0] & & {add footer;}
3. The special variable $$records the PID of the current process.
This feature may not be used very much in normal use, but it is also very useful when writing shell scripts, for example, if you want to create a temporary file in / tmp, the name of the file has always been very troublesome, at this time you can use the $$variable to expand the PID of the current process as a temporary file name, PID is unique in the computer, so it will never be repeated, and you do not need to remember the name of the temporary file.
At this point, the study of "what are the skills for using Shell in Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.