~dricottone/my-utils

ref: b29d2b4a9bcb2e3b35c41e5dbb43d6e031999a06 my-utils/safe-rm.bash -rwxr-xr-x 317 bytes
b29d2b4aDominic Ricottone Restarting with fresh commit history 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

# save_rm.bash
# ============
# Usage: safe_rm.bash TARGET PATTERN
#
# Checks if a target matches a pattern before removing it

err_msg() {
  (>&2 echo "$1")
  exit 1
}

if [[ $# -lt 2 ]]; then
  err_msg "Usage: safe_rm.bash TARGET PATTERN"
fi

PATTERN="^${2}$"
if [[ $1 =~ $PATTERN ]]; then
  rm $1
fi