add note about license
[small-misc-bash] / rmln
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 rmln () {
17 local help="Usage: rmln [RM_OPTION]... FILE...
18 wrap rm, expanding symlink arguments
19
20 Symlink arguments are recursively expanded so the links and the targets
21 are removed. All arguments to rm work.
22
23 rm's --help:
24 "
25 local file targets
26
27 # simple but correct counting past gnu rm options
28 local -i index=1
29 local options=true
30 local helped=false # only print our help once
31 while $options; do
32 if [[ "${@:index:1}" == -* ]]; then
33 if [[ "${@:index:1}" == --help ]] && ! $helped; then
34 echo "$help"
35 helped=true
36 fi
37 if [[ "${@:index:1}" == -- ]]; then
38 options=false
39 fi
40 index+=1
41 else
42 options=false
43 fi
44 done
45
46 for file in "${@:index}"; do
47 [[ -L "$file" ]] && _rmln_recurse "$file"
48 done
49 rm "$@" "${targets[@]}"
50 }
51
52 _rmln_recurse() {
53 local x="$( readlink "$1" )"
54 targets+=( "$x" )
55 [[ -L $x ]] && _rmln_recurse "$x"
56 }
57 rmln "$@"