Browse Source

Relocate safety checks, add force option (-F)

master
Peter J. Jones 1 year ago
parent
commit
65a9f30f97
Signed by: Peter Jones <pjones@devalot.com> GPG Key ID: 9DAFAA8D01941E49
1 changed files with 20 additions and 9 deletions
  1. 20
    9
      examples/watchfifo.sh

+ 20
- 9
examples/watchfifo.sh View File

@@ -25,6 +25,7 @@
25 25
 ################################################################################
26 26
 set -e
27 27
 option_fifo_file=""
28
+option_force=0
28 29
 option_group=""
29 30
 
30 31
 ################################################################################
@@ -33,6 +34,7 @@ cat <<EOF
33 34
 Usage: watchfifo.sh [options] -- command [arg1, arg2, ...]
34 35
 
35 36
   -f FILE The FIFO file to create and manage
37
+  -F      Force. If FIFO file exists, remove it
36 38
   -g GRP  Set the FIFO file's group to GRP
37 39
   -h      This message
38 40
 
@@ -40,11 +42,14 @@ EOF
40 42
 }
41 43
 
42 44
 ################################################################################
43
-while getopts "f:g:h" o; do
45
+while getopts "f:Fg:h" o; do
44 46
   case "${o}" in
45 47
     f) option_fifo_file=$OPTARG
46 48
        ;;
47 49
 
50
+    F) option_force=1
51
+       ;;
52
+
48 53
     g) option_group=$OPTARG
49 54
        ;;
50 55
 
@@ -67,14 +72,6 @@ die() {
67 72
 
68 73
 ################################################################################
69 74
 prepare() {
70
-  if [ -z "$option_fifo_file" ]; then
71
-    die "you must use -f to specify a file path"
72
-  fi
73
-
74
-  if [ -r "$option_fifo_file" ]; then
75
-    die "FIFO file exists, remove it first: $option_fifo_file"
76
-  fi
77
-
78 75
   mkfifo -m 0620 "$option_fifo_file"
79 76
 
80 77
   if [ -n "$option_group" ]; then
@@ -98,10 +95,24 @@ subcommand() {
98 95
 }
99 96
 
100 97
 ################################################################################
98
+# Safety checks.
99
+if [ -z "$option_fifo_file" ]; then
100
+  die "you must use -f to specify a file path"
101
+fi
102
+
103
+if [ -r "$option_fifo_file" ]; then
104
+  if [ "$option_force" -eq 1 ]; then
105
+    rm "$option_fifo_file"
106
+  else
107
+    die "FIFO file exists, remove it first: $option_fifo_file"
108
+  fi
109
+fi
110
+
101 111
 if [ $# -le 0 ]; then
102 112
   die "please provide a command to run after -- "
103 113
 fi
104 114
 
115
+################################################################################
105 116
 export IFS=$'\n'
106 117
 trap cleanup EXIT
107 118
 prepare

Loading…
Cancel
Save