Browse code

git-repo-creator.sh - v0.2 - SSH port option added

Fred authored on02/06/2019 20:34:44
Showing1 changed files
... ...
@@ -1,9 +1,9 @@
1 1
 #!/bin/bash
2 2
 # Title: git-repo-creator.sh
3
-# Version: 0.1
3
+# Version: 0.2
4 4
 # Author: Frédéric CHEVALIER <fcheval@txbiomedgenetics.org>
5 5
 # Created in: 2014-08-31
6
-# Modified in: 2014-09-01
6
+# Modified in: 2019-06-02
7 7
 # Licence : GPL v3
8 8
 
9 9
 
... ...
@@ -20,6 +20,7 @@ aim="Create a git repository locally and remotely on a given server using ssh."
20 20
 # Versions #
21 21
 #==========#
22 22
 
23
+# v0.2 - 2019-06-02: SSH port option added
23 24
 # v0.1 - 2014-09-01: default value for -d using structure of -r added / if remote repository exist, skip instead of interrupt the script / addition of missing part of initialisation step when done for the first time / bug correction in the reinitialisation of remote path
24 25
 # v0.0 - 2014-08-31: creation
25 26
 
... ...
@@ -40,7 +41,7 @@ function test_dep {
40 41
 # Usage message
41 42
 function usage {
42 43
     echo -e "
43
-    \e[32m${0##*/}\e[00m -r|--rep repository_name -d|--drep remote_repository_name -s|--svr server_name -u|--usr user_name -h|--help
44
+    \e[32m${0##*/}\e[00m -r|--rep repository_name -d|--drep remote_repository_name -s|--svr server_name -u|--usr user_name -p|--port ssh_port -h|--help
44 45
 
45 46
 Aim: $aim
46 47
 
... ...
@@ -49,6 +50,7 @@ Options:
49 50
     -d, --drep      name or path of the repository created on the server [default: value of -r]
50 51
     -s, --svr       name of the server
51 52
     -u, --usr       user name for server connection
53
+    -p, --port      SSH port for server connection [default: 22]
52 54
     -h, --help      this message
53 55
 "
54 56
 }
... ...
@@ -88,6 +90,7 @@ do
88 90
         -d|--drep   ) drep="$2" ; shift 2 ;;
89 91
         -s|--svr    ) svr="$2" ; shift 2 ;;
90 92
         -u|--usr    ) usr="$2" ; shift 2 ;;
93
+        -p|--port   ) port="$2" ; shift 2 ;;
91 94
         -h|--help   ) usage ; exit 0 ;;
92 95
         *           ) error "Invalid option: $1\n$(usage)\n" 1 ;;
93 96
     esac
... ...
@@ -119,6 +122,15 @@ then
119 122
     drep="$rep"
120 123
 fi
121 124
 
125
+# Set default value for SSH port
126
+if [[ -n "$port" && $(echo "$port" | egrep -x -v "[[:digit:]]+") ]]
127
+then
128
+    error "The option -p must be numeric only. Exiting...\n$(usage)\n" 1
129
+elif [[ -z $port ]]
130
+then
131
+    port=22
132
+fi
133
+
122 134
 
123 135
 
124 136
 #============#
... ...
@@ -131,14 +143,14 @@ if [[ -n "$drep" ]]
131 143
 then
132 144
 
133 145
     # Test of the ssh connection
134
-    ssh -q "$usr"@"$svr" "echo 2>&1" &> /dev/null
146
+    ssh -p $port -q "$usr"@"$svr" "echo 2>&1" &> /dev/null
135 147
     if [[ $? != 0 ]]
136 148
     then
137 149
         error "Unable to connect to the server $svr. Aborting..." 1
138 150
     fi
139 151
 
140 152
     # Test of the existance of the distant directory
141
-    ssh -q "$usr"@"$svr" "ls $drep" &> /dev/null
153
+    ssh -p $port -q "$usr"@"$svr" "ls $drep" &> /dev/null
142 154
     if [[ $? == 0 ]]
143 155
     then
144 156
         warning "Remote directory exists already on the server."
... ...
@@ -158,7 +170,7 @@ then
158 170
         # Upload of the structure on the server
159 171
         old_pwd="$PWD"
160 172
         cd /tmp
161
-        scp -p -r "${drep%%/*}" $usr@$svr: &> /dev/null
173
+        scp -P $port -p -r "${drep%%/*}" $usr@$svr: &> /dev/null
162 174
         cd "$old_pwd"
163 175
 
164 176
         # Removing of the tmp files
... ...
@@ -204,11 +216,11 @@ then
204 216
             echo "Skipping..."
205 217
         else
206 218
             git remote remove origin
207
-            git remote origin "$usr@$svr:$drep"
219
+            git remote origin "ssh://$usr@$svr:$port/~/$drep"
208 220
         fi
209 221
 
210 222
    else
211
-       git remote add origin "$usr@$svr:$drep"
223
+       git remote add origin "ssh://$usr@$svr:$port/~/$drep"
212 224
    fi
213 225
 
214 226
 fi