Browse code

git-synch.sh - v1.0 - Several bug corrections and functions added

For more details, see versions section of the script.

Fred authored on14/09/2014 20:22:39
Showing1 changed files
... ...
@@ -1,10 +1,10 @@
1 1
 #!/bin/bash
2 2
 # Title: git-sync.sh
3
-# Version: 0.1
3
+# Version: 1.0
4 4
 # Author: Frédéric CHEVALIER <fcheval@txbiomedgenetics.org>
5 5
 # Created in: 2014-09-03
6
-# Modified in: 2014-09-13
7
-# Licence : GPL v3
6
+# Modified in: 2014-09-14
7
+# Licence: GPL v3
8 8
 
9 9
 
10 10
 
... ...
@@ -20,6 +20,7 @@ aim="Synchronize files from git server (master branch) on this terminal."
20 20
 # Versions #
21 21
 #==========#
22 22
 
23
+# v1.0 - 2014-09-14: git dependency check replaced by wget check (bug) / test_dep function improved / remove comments in list file / check for http path / remove spaces in path / checksum loop corrected
23 24
 # v0.1 - 2014-09-23: dry-run option added / no check certifcate option added for wget
24 25
 # v0.0 - 2014-09-03: creation
25 26
 
... ...
@@ -31,7 +32,7 @@ aim="Synchronize files from git server (master branch) on this terminal."
31 32
 
32 33
 # Dependency test
33 34
 function test_dep {
34
-    if [[ ! $(which $1) ]]
35
+    if [[ ! $(which $1 &> /dev/null) ]]
35 36
     then
36 37
         error "Package $1 is needed. Exiting..." 1
37 38
     fi
... ...
@@ -47,7 +48,8 @@ Aim: $aim
47 48
 Options:
48 49
     -l, --list      File containing remote path and local path of a file to synchronize.
49 50
                     The list file must have a unique path pair per line and each path must
50
-                    be semi-colon separated. The remote path must be http path.
51
+                    be semi-colon separated (path1 ; path2). The remote path must be http path.
52
+                    Comments beginning by # are allowed anywhere.
51 53
 
52 54
     -d, --dry-run   Run the synchronization but print a message instead of updating the files. 
53 55
 
... ...
@@ -74,7 +76,7 @@ function warning {
74 76
 # Dependencies #
75 77
 #==============#
76 78
 
77
-test_dep git
79
+test_dep wget
78 80
 
79 81
 
80 82
 
... ...
@@ -100,6 +102,10 @@ then
100 102
     error "The option -l is required. Exiting...\n$(usage)\n" 1
101 103
 fi
102 104
 
105
+# Temporary list
106
+mydate=$(date +%N)
107
+tmp_list="/tmp/$list-$mydate"
108
+
103 109
 
104 110
 
105 111
 #============#
... ...
@@ -113,18 +119,29 @@ then
113 119
 fi
114 120
 
115 121
 
122
+# Remove comments from list file
123
+sed "/^\s*#/d;s/\s*#.*$//" "$list" > "$tmp_list"
124
+
125
+
116 126
 # Synchronization
117 127
 while read line
118 128
 do
119 129
 
120 130
     # Get paths
121
-    mysrc=$(echo "$line" | cut -d ";" -f 1)
122
-    mydst=$(echo "$line" | cut -d ";" -f 2)
131
+    mysrc=$(echo "$line" | cut -d ";" -f 1 | sed -e "s/^ *//" -e "s/ *$//")
132
+    mydst=$(echo "$line" | cut -d ";" -f 2 | sed -e "s/^ *//" -e "s/ *$//")
123 133
 
124 134
     mydst_tmp="/tmp/${mydst##*/}"
125 135
 
136
+    # Check if remote path is http
137
+    if [[ ! $(echo "$mysrc" | grep -i "^http") ]]
138
+    then
139
+        warning "${mydst##*/}: Remote path is not http address. Skipping..."
140
+        continue
141
+    fi
142
+
126 143
     # Download file
127
-    wget --no-check-certificate -O "$mydst_tmp" "$mysrc"
144
+    wget --no-check-certificate -O "$mydst_tmp" "$mysrc" &> /dev/null
128 145
 
129 146
     # Check for errors
130 147
     if [[ $? != 0 ]]
... ...
@@ -133,7 +150,9 @@ do
133 150
         continue
134 151
     else
135 152
         # Compare downloaded file and destination file (update file if need)
136
-        if [[ $(md5sum "$mydst_tmp") -ne $(md5sum "$mydst") ]]
153
+        mydst_tmp_md=$(md5sum "$mydst_tmp" | cut -d " " -f 1)
154
+        mydst_md=$(md5sum "$mydst" | cut -d " " -f 1)
155
+        if [[ "$mydst_tmp_md" != "$mydst_md" ]]
137 156
         then
138 157
             if [[ $dry == 1 ]]
139 158
             then
... ...
@@ -146,7 +165,11 @@ do
146 165
         fi
147 166
     fi
148 167
 
149
-done < "$list"
168
+done < "$tmp_list"
169
+
170
+
171
+# Remove temporary list
172
+rm "$tmp_list"
150 173
 
151 174
 
152 175
 exit 0