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
Browse code

script.sh - v4.2 - Error func improved and template updated

Fred authored on11/03/2015 22:56:50
Showing1 changed files
... ...
@@ -1,8 +1,8 @@
1 1
 #!/bin/bash
2 2
 # Title:
3 3
 # Version: 
4
-# Author: Frédéric CHEVALIER <fcheval@txbiomedgenetics.org>
5
-# Created in: 2014-1
4
+# Author: Frédéric CHEVALIER <fcheval@txbiomed.org>
5
+# Created in: 2015-
6 6
 # Modified in:
7 7
 # Licence : GPL v3
8 8
 
... ...
@@ -12,7 +12,7 @@
12 12
 # Aims #
13 13
 #======#
14 14
 
15
-# 
15
+aim=""
16 16
 
17 17
 
18 18
 
... ...
@@ -20,7 +20,7 @@
20 20
 # Versions #
21 21
 #==========#
22 22
 
23
-# v0.0 - 2014-1: creation
23
+# v0.0 - 2015-xx-xx: creation
24 24
 
25 25
 
26 26
 
... ...
@@ -28,21 +28,13 @@
28 28
 # Functions #
29 29
 #===========#
30 30
 
31
-# Dependency test
32
-function test_dep {
33
-    which $1 &> /dev/null
34
-    if [[ $? != 0 ]]
35
-    then
36
-        error "Package $1 is needed. Exiting..." 1
37
-    fi
38
-}
39
-
40
-
41 31
 # Usage message
42 32
 function usage {
43 33
     echo -e "
44 34
     \e[32m ${0##*/} \e[00m -i|--in file -o|--out value -v|--val value -s|--switch -h|--help
45 35
 
36
+Aim: $aim
37
+
46 38
 Options:
47 39
     -i, --in        input file
48 40
     -o, --out       name of output file
... ...
@@ -76,32 +68,47 @@ function warning {
76 68
 
77 69
 
78 70
 # Error message
71
+## usage: error "message" exit_code
72
+## exit code optional (no exit allowing downstream steps)
79 73
 function error {
80 74
     if [[ -t 1 ]]
81 75
     then
82 76
         echo -e "\e[31mError:\e[00m $1"
83
-        exit $2
84 77
     else
85 78
         echo -e "Error: $1"
79
+    fi
80
+
81
+    if [[ -n $2 ]]
82
+    then
86 83
         exit $2
87 84
     fi
88 85
 }
89 86
 
90 87
 
88
+# Dependency test
89
+function test_dep {
90
+    which $1 &> /dev/null
91
+    if [[ $? != 0 ]]
92
+    then
93
+        error "Package $1 is needed. Exiting..." 1
94
+    fi
95
+}
96
+
97
+
91 98
 
92 99
 #==============#
93 100
 # Dependencies #
94 101
 #==============#
95 102
 
96
-# 
103
+test_dep
97 104
 
98 105
 
99 106
 
100
-#==========================#
101
-# Declaration of variables #
102
-#==========================#
107
+#===========#
108
+# Variables #
109
+#===========#
103 110
 
104
-# Load variables
111
+# Options
105 112
 while [[ $# -gt 0 ]]
106 113
 do
107 114
     case $1 in
... ...
@@ -137,4 +144,9 @@ fi
137 144
 # Processing #
138 145
 #============#
139 146
 
147
+#-------------#
148
+# Sub-section #
149
+#-------------#
150
+
151
+
140 152
 exit 0
Browse code

script.sh - v4.1 - Function test_dep added

Fred authored on11/03/2015 22:21:39
Showing1 changed files
... ...
@@ -2,7 +2,7 @@
2 2
 # Title:
3 3
 # Version: 
4 4
 # Author: Frédéric CHEVALIER <fcheval@txbiomedgenetics.org>
5
-# Created in: 2014-0
5
+# Created in: 2014-1
6 6
 # Modified in:
7 7
 # Licence : GPL v3
8 8
 
... ...
@@ -20,15 +20,7 @@
20 20
 # Versions #
21 21
 #==========#
22 22
 
23
-# v0.0: creation
24
-
25
-
26
-
27
-#==============#
28
-# Dependencies #
29
-#==============#
30
-
31
-# 
23
+# v0.0 - 2014-1: creation
32 24
 
33 25
 
34 26
 
... ...
@@ -36,6 +28,16 @@
36 28
 # Functions #
37 29
 #===========#
38 30
 
31
+# Dependency test
32
+function test_dep {
33
+    which $1 &> /dev/null
34
+    if [[ $? != 0 ]]
35
+    then
36
+        error "Package $1 is needed. Exiting..." 1
37
+    fi
38
+}
39
+
40
+
39 41
 # Usage message
40 42
 function usage {
41 43
     echo -e "
... ...
@@ -51,20 +53,48 @@ Options:
51 53
 }
52 54
 
53 55
 
54
-
55
-# Error message
56
-function error {
57
-    echo -e "\e[31mError:\e[00m $1"
58
-    exit $2
56
+# Info message
57
+function info {
58
+    if [[ -t 1 ]]
59
+    then
60
+        echo -e "\e[32mInfo:\e[00m $1"
61
+    else
62
+        echo -e "Info: $1"
63
+    fi
59 64
 }
60 65
 
61 66
 
62 67
 # Warning message
63 68
 function warning {
64
-    echo -e "\e[33mWarning:\e[00m $1"
69
+    if [[ -t 1 ]]
70
+    then
71
+        echo -e "\e[33mWarning:\e[00m $1"
72
+    else
73
+        echo -e "Warning: $1"
74
+    fi
65 75
 }
66 76
 
67 77
 
78
+# Error message
79
+function error {
80
+    if [[ -t 1 ]]
81
+    then
82
+        echo -e "\e[31mError:\e[00m $1"
83
+        exit $2
84
+    else
85
+        echo -e "Error: $1"
86
+        exit $2
87
+    fi
88
+}
89
+
90
+
91
+
92
+#==============#
93
+# Dependencies #
94
+#==============#
95
+
96
+# 
97
+
68 98
 
69 99
 
70 100
 #==========================#
... ...
@@ -85,9 +115,7 @@ do
85 115
         -v|--val    ) v="$2" ; shift 2 ;;
86 116
         -s|--switch ) switch=1 ; shift ;;
87 117
         -h|--help   ) usage ; exit 0 ;;
88
-        *           ) echo "Invalid option: $1"
89
-                       usage
90
-                       exit 1 ;;
118
+        *           ) error "Invalid option: $1\n$(usage)" 1 ;;
91 119
     esac
92 120
 done
93 121
 
... ...
@@ -95,7 +123,7 @@ done
95 123
 # Check the existence of obligatory options
96 124
 if [[ -z "$input" ]]
97 125
 then
98
-    error "The option input is required. Exiting..." 1
126
+    error "The option input is required. Exiting...\n$(usage)" 1
99 127
 fi
100 128
 
101 129
 if [[ -z "$output" ]]
Browse code

script.sh - v4 - Structures filled with common functions and loops

Fred authored on01/03/2015 17:30:55
Showing1 changed files
... ...
@@ -1,10 +1,10 @@
1 1
 #!/bin/bash
2 2
 # Title:
3
-# Version:
4
-# Author: Frédéric CHEVALIER <f15.chevalier@gmail.com>
5
-# Created in:
3
+# Version: 
4
+# Author: Frédéric CHEVALIER <fcheval@txbiomedgenetics.org>
5
+# Created in: 2014-0
6 6
 # Modified in:
7
-# License : GPL v3
7
+# Licence : GPL v3
8 8
 
9 9
 
10 10
 
... ...
@@ -12,7 +12,7 @@
12 12
 # Aims #
13 13
 #======#
14 14
 
15
-#
15
+# 
16 16
 
17 17
 
18 18
 
... ...
@@ -20,7 +20,7 @@
20 20
 # Versions #
21 21
 #==========#
22 22
 
23
-#
23
+# v0.0: creation
24 24
 
25 25
 
26 26
 
... ...
@@ -28,7 +28,42 @@
28 28
 # Dependencies #
29 29
 #==============#
30 30
 
31
-#
31
+# 
32
+
33
+
34
+
35
+#===========#
36
+# Functions #
37
+#===========#
38
+
39
+# Usage message
40
+function usage {
41
+    echo -e "
42
+    \e[32m ${0##*/} \e[00m -i|--in file -o|--out value -v|--val value -s|--switch -h|--help
43
+
44
+Options:
45
+    -i, --in        input file
46
+    -o, --out       name of output file
47
+    -v, --val       value
48
+    -s, --switch    switch
49
+    -h, --help      this message
50
+"
51
+}
52
+
53
+
54
+
55
+# Error message
56
+function error {
57
+    echo -e "\e[31mError:\e[00m $1"
58
+    exit $2
59
+}
60
+
61
+
62
+# Warning message
63
+function warning {
64
+    echo -e "\e[33mWarning:\e[00m $1"
65
+}
66
+
32 67
 
33 68
 
34 69
 
... ...
@@ -36,7 +71,37 @@
36 71
 # Declaration of variables #
37 72
 #==========================#
38 73
 
39
-
74
+# Load variables
75
+while [[ $# -gt 0 ]]
76
+do
77
+    case $1 in
78
+        -i|--in     ) input="$2" ; shift 2 
79
+                        while [[ ! -z "$1" && $(echo "$1"\ | grep -qv "^-" ; echo $?) == 0 ]]
80
+                        do
81
+                            input=$(echo "$input" "$1")
82
+                            shift
83
+                        done ;; 
84
+        -o|--out    ) output="$2" ; shift 2 ;;
85
+        -v|--val    ) v="$2" ; shift 2 ;;
86
+        -s|--switch ) switch=1 ; shift ;;
87
+        -h|--help   ) usage ; exit 0 ;;
88
+        *           ) echo "Invalid option: $1"
89
+                       usage
90
+                       exit 1 ;;
91
+    esac
92
+done
93
+
94
+
95
+# Check the existence of obligatory options
96
+if [[ -z "$input" ]]
97
+then
98
+    error "The option input is required. Exiting..." 1
99
+fi
100
+
101
+if [[ -z "$output" ]]
102
+then
103
+    output=
104
+fi
40 105
 
41 106
 
42 107
 
... ...
@@ -44,5 +109,4 @@
44 109
 # Processing #
45 110
 #============#
46 111
 
47
-
48 112
 exit 0
Browse code

script.sh - v3 - Structure added

Fred authored on01/03/2015 17:01:11
Showing1 changed files
... ...
@@ -1,6 +1,48 @@
1 1
 #!/bin/bash
2
-#title:
3
-#version: 
4
-#author: Fédéric CHEVALIER
5
-#created in:
6
-#modified in:
2
+# Title:
3
+# Version:
4
+# Author: Frédéric CHEVALIER <f15.chevalier@gmail.com>
5
+# Created in:
6
+# Modified in:
7
+# License : GPL v3
8
+
9
+
10
+
11
+#======#
12
+# Aims #
13
+#======#
14
+
15
+#
16
+
17
+
18
+
19
+#==========#
20
+# Versions #
21
+#==========#
22
+
23
+#
24
+
25
+
26
+
27
+#==============#
28
+# Dependencies #
29
+#==============#
30
+
31
+#
32
+
33
+
34
+
35
+#==========================#
36
+# Declaration of variables #
37
+#==========================#
38
+
39
+
40
+
41
+
42
+
43
+#============#
44
+# Processing #
45
+#============#
46
+
47
+
48
+exit 0
Browse code

script.sh - v2

Fred authored on01/03/2015 16:56:45
Showing1 changed files
... ...
@@ -1,4 +1,4 @@
1
-#!/bin/sh
1
+#!/bin/bash
2 2
 #title:
3 3
 #version: 
4 4
 #author: Fédéric CHEVALIER
Browse code

script.sh - v1

Fred authored on01/03/2015 15:57:40
Showing1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,6 @@
1
+#!/bin/sh
2
+#title:
3
+#version: 
4
+#author: Fédéric CHEVALIER
5
+#created in:
6
+#modified in: