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" ]]