In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "Linux Shell mistakenly delete the root directory how to solve", in the daily operation, I believe that many people in the Linux Shell mistakenly delete the root directory how to solve the problem, the editor consulted all kinds of information, sorted out a simple and easy-to-use method of operation, hope to answer the "Linux Shell mistakenly delete the root directory how to solve" the doubt is helpful! Next, please follow the editor to study!
1. If the variable is empty, the file will be deleted by mistake.
Base_path=/usr/sbin tmp_file= `cmd _ roomid` # rm-rf $base_path/$tmp_file
In this case, if the cmd execution goes wrong or returns null, the consequences will be catastrophic, so how to prevent it?
(1) make use of the variable extension function of shell, if the variable is empty, assign it to the default value or throw an exception to exit the script:
Echo ${base_path:?var is empty} / ${tmp_file:?var is empty}-bash: tmp_file: var is empty
(2) whether the human flesh judgment variable is empty:
[[${tmp_file} = ""]] & & echo 1 1 [[- z ${tmp_file}]] & & echo 1 1
(3) if the variable is not defined, you can enable the set option:
# cat a.sh set-u b = echo $b echo $an echo 1 # bash a.sh a.sh: line 4: a: unbound variable
2. The path contains spaces, which leads to accidental deletion of files.
The most classic project in history is the following bumblebee project, which was not well-known, but a bug in its installation script install.sh made it the most high-profile project in the world.
So how can we prevent this problem?
(1) good programming habits: variables in quotation marks to prevent expansion
Path= "/ usr/local / sbin" # rm-rf $path rm-rf "$path"
(2) semantic checking of variables.
For example, testing whether it contains special characters such as spaces is not universal and is not recommended.
3. The directory or file contains special characters that cause the file to be deleted by mistake.
Total amount of ll 8 drwxrwxr-x 2 work work 4096 November 24 18:57'- rw-rw-r-- 1 work work 34 November 24 19:49 a.sh # rm-rf ~
So how can we prevent this problem?
(1) good programming habits: variables in quotation marks to prevent expansion
Rm-rf "~"
(2) if you are not sure, click echo or find before deletion to see what the variable has been extended to
Echo rm-rf "~" rm-rf ~ echo rm-rf ~ rm-rf / home/work
4. Cd failed to switch directories, resulting in files being deleted by mistake
Cd ooxx_path_not_exsit rm-rf * .exe
Congratulations, in this case, all the matching files in your current directory will be deleted by mistake, so how can we prevent this problem?
(1) use logical short circuit operation
Cd path & & rm-rf * .exe
(2) check whether path exists.
[[- d]] & & echo 1 1
5. * solution
Do not use root operating system resources so that at least system files are not deleted.
6. Use a friendly prompt under login shell
A friendly command prompt can always remind the operator which path he is currently in and avoid the wrong path to manipulate the file.
At this point, the study on "how to solve the problem of mistakenly deleting the root directory by Linux Shell" 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.