Browse code

script.sh - v5 - Progress bar and trap command added

Fred authored on23/10/2016 23:44:01
Showing1 changed files
... ...
@@ -1,8 +1,8 @@
1 1
 #!/bin/bash
2 2
 # Title:
3
-# Version: 
3
+# Version: 0.0 a
4 4
 # Author: Frédéric CHEVALIER <fcheval@txbiomed.org>
5
-# Created in: 2015-
5
+# Created in: 20xx-
6 6
 # Modified in:
7 7
 # Licence : GPL v3
8 8
 
... ...
@@ -20,7 +20,9 @@ aim=""
20 20
 # Versions #
21 21
 #==========#
22 22
 
23
-# v0.0 - 2015-xx-xx: creation
23
+# v0.0 - 20xx-xx-xx: creation
24
+
25
+version=$(grep -i -m 1 "version" "$0" | cut -d ":" -f 2 | sed "s/^ *//g")
24 26
 
25 27
 
26 28
 
... ...
@@ -35,13 +37,15 @@ function usage {
35 37
 
36 38
 Aim: $aim
37 39
 
40
+Version: $version
41
+
38 42
 Options:
39 43
     -i, --in        input file
40 44
     -o, --out       name of output file
41 45
     -v, --val       value
42 46
     -s, --switch    switch
43 47
     -h, --help      this message
44
-"
48
+    "
45 49
 }
46 50
 
47 51
 
... ...
@@ -95,6 +99,38 @@ function test_dep {
95 99
 }
96 100
 
97 101
 
102
+# Progress bar
103
+## Usage: ProgressBar $mystep $myend
104
+function ProgressBar {
105
+    if [[ -t 1 ]]
106
+    then
107
+        # Process data
108
+        let _progress=(${1}*100/${2}*100)/100
109
+        let _done=(${_progress}*4)/10
110
+        let _left=40-$_done
111
+        # Build progressbar string lengths
112
+        _fill=$(printf "%${_done}s")
113
+        _empty=$(printf "%${_left}s")
114
+
115
+        # Build progressbar strings and print the ProgressBar line
116
+        # Output example:
117
+        # Progress : [########################################] 100%
118
+        #printf "\rProgress : [${_fill// /=}${_empty// / }] ${_progress}%%"
119
+        printf "\r\e[32mProgress:\e[00m [${_fill// /=}${_empty// / }] ${_progress}%%"
120
+        
121
+        [[ ${_progress} == 100 ]] && echo ""
122
+    fi
123
+}
124
+
125
+
126
+# Clean up function for trap command
127
+## Usage: clean_up file1 file2 ...
128
+function clean_up {
129
+    rm -rf $@
130
+    exit 1
131
+}
132
+
133
+
98 134
 
99 135
 #==============#
100 136
 # Dependencies #
... ...
@@ -149,4 +185,24 @@ fi
149 185
 #-------------#
150 186
 
151 187
 
188
+# Progress bar
189
+for i in myvar
190
+do
191
+    ProgressBar $mystep $myend
192
+done
193
+
194
+# Trap function
195
+##
196
+for i in {1..3} ; do
197
+    cmd &
198
+    mypid="$mypid $!"
199
+done
200
+# OR
201
+cmd &
202
+mypid=$!
203
+##
204
+trap "kill $mypid ; clean_up $tmp1 $tmp2" SIGINT SIGTERM    # Clean_up function to remove tmp files
205
+wait
206
+
207
+
152 208
 exit 0