As a widely used system language under linux, shell has a simple syntax and is easy to use, but it is not so easy to use it well and make fewer mistakes. Although it is a good medicine for home travel, it is also a killer. weapon~
Today, let's talk about the next common problem in Linux: how to avoid accidentally deleting a directory. The following will describe in detail the accidental deletion of directories in different scenarios, as well as the corresponding solutions.
1. The variable is empty, causing the file to be deleted by mistake
base_path=/usr/sbin
tmp_file=`cmd_invalid`
# rm -rf $base_path/$tmp_file
In this case, if cmd executes incorrectly or returns empty, the consequences will be catastrophic. How to prevent it?
(1) Using the variable expansion function of the shell , if the variable is empty, assign 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) Human flesh judges whether the variable is empty :
[[ ${tmp_file} == "" ]] && echo 1
1
[[ -z ${tmp_file} ]] && echo 1
1
(3) If the variable is not defined, you can also open the set option :
# cat a.sh
# 若有用未设置的变量即让脚本退出执行
# set -o nounset
# 或
set -u
b=
echo $b
echo $a
echo 1
# bash a.sh
a.sh: line 4: a: unbound variable
# 另外,
# 如果命令运行失败让脚本退出执行
set -o errexit
# 或
set -e
2. The path contains spaces, causing the file to be deleted by mistake
The most classic in history is the following bumblebee project. This project was not well-known, but a bug in the program's installation script install.sh made this project the most eye-catching project in the world.
So how can we prevent this problem?
(1) Good programming habits: add quotation marks to variables to prevent expansion
path="/usr/local /sbin"
# rm -rf $path
rm -rf "$path"
(2) Semantic checking of variables
For example, detecting whether there are special characters such as spaces is not common and is not recommended.
3. The directory or file contains special characters, causing the file to be deleted by mistake
ll
总用量 8
drwxrwxr-x 2 work work 4096 11月 24 18:57 '~'
-rw-rw-r-- 1 work work 34 11月 24 19:49 a.sh
# rm -rf ~
So how can we prevent this problem?
(1) Good programming habits: add quotation marks to variables to prevent expansion
rm -rf "~"
(2) If you are not sure, echo or find before deleting to see what the variable is expanded into
echo rm -rf "~"
rm -rf ~
echo rm -rf ~
rm -rf /home/work
4. The cd failed to switch the directory, resulting in the file being deleted by mistake
cd ooxx_path_not_exsit
rm -rf *.exe
Congratulations, in this case, the matching files in your current directory will be deleted by mistake, so how can we prevent this problem?
(1) Use logic short-circuit operation
cd path && rm -rf *.exe
(2) Check whether the path exists
[[ -d ~ ]] && echo 1
1
5. The ultimate solution
Don't use root OS resources , so at least not delete system files.
6. Use a friendly prompt in the login shell
The friendly command prompt can always remind the operator which path is currently under, and avoid operating files under the wrong path.
OK, this is the end of this article. I have listed some common cases and solutions. I hope it can inspire everyone. If you have other cases and suggestions, you are welcome to communicate~
Refer:
[1] Bash script set command tutorial