众所周知rm命令的杀伤力,所以执行rm -rf要格外小心。为了避免意外删除文件,最佳实践是在“.bashrc”文件中创建’rm’命令别名为’rm -i’,它会要求确认每次删除。
这里就不列举出可能的误删除命令,这个脚本主要是用于避免删除根目录,导致系统直接异常。
vim ~/.bashrc
safe_rm() {
option=""
for file in "$@"; do
if [[ $file =~ ^- ]]; then
option="$option $file"
continue
fi
p=$(realpath "$file")
if [[ $p =~ ^/[^/]*$ ]]; then
echo "Try to remove $p. Dangerous!!!"
else
#echo "hahhaaa" # 在测试的时候,可以将这一行改为echo
command rm -i $option "$file"
fi
done
}
alias rm='safe_rm'
source ~/.bashrc
ansible配置
- name: Update .bashrc and source it
hosts: parserver_test
become: yes
tasks:
- name: Add content to .bashrc
lineinfile:
path: /etc/bashrc
line: |
safe_rm() {
option=""
for file in "$@"; do
if [[ $file =~ ^- ]]; then
option="$option $file"
continue
fi
p=$(realpath "$file")
if [[ $p =~ ^/[^/]*$ ]]; then
echo "Try to remove $p. Dangerous!!!"
else
#echo "hahhaaa" # 在测试的时候,可以将这一行改为echo
command rm -i $option "$file"
fi
done
}
alias rm='safe_rm'
insertafter: EOF
- name: Source .bashrc and run a command
#command: "source ~/.bashrc && your_command_here"
shell: source /etc/bashrc
其中 source /etc/bashrc 和 path: /etc/bashrc
可以按照实际情况改为source ~/.bashrc