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