-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.sh
68 lines (47 loc) · 2.94 KB
/
process.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
################################################################################
# #
# Name: process.sh #
# Description: Moves incoming files to Azure Blob storage #
# Author: Sajal Sachdev <[email protected]> #
# #
# Exit codes: #
# 0: All is well on some front somewhere #
# 1: Parameter(s) missing #
# 2: Required something something missing (e.g. permissions or executable) #
# 3: Something went wrong with the copy to Azure #
# #
################################################################################
# Names are cool
FILE="$1"
# Minimize using relative paths, for example:
# if we don't check whether "processed" is a directory, all your files could be overwritten by the next one and called "processed"
# (luckily it's already save in the cloud :D)
PROCESSED_DIR="$PWD/processed"
# It can be wise to use full paths in scripts
# (e.g. sometimes cron lacks a $PATH)
AZCOPY="/path/to/azcopy"
# You could make URI + Token parameters?
CONTAINER_URL="<add container URL here + SAS Token>"
# (I prefer this style but yours is faster and arguably more readable)
# Note the use of double brackets for testing (man test)
#
# BTW, did you know '[' is the same as the 'test' command? ==> ls -l /bin[
# FILE passed? (Rubin insists that [[ ! -n "$FILE" ]] is better (I'm just stubborn))
[[ -z "$FILE" ]] && echo "Usage $0: file" && exit 1
# FILE writeable?
[[ ! -w "$FILE" ]] && echo "Incoming file ($FILE) is not writable, exiting..." && exit 2
# AZCOPY executable?
[[ ! -x "$AZCOPY" ]] && echo "Unable to execute azcopy ($AZCOPY), exiting..." && exit 2
# PROCESSED_DIR directory and writable?
[[ ! -d "$PROCESSED_DIR" ]] || [[ ! -w "$PROCESSED_DIR" ]] && echo "Something is wrong with the processed directory ($PROCESSED_DIR), exiting..." && exit 2
# Unsure whether azcopy does but most apps return their exit code in '$?'
# If you don't need the output, you don't need a sub-shell, if you do, use $() instead of backticks, they are so nineties ;)
$AZCOPY copy "$FILE" "$CONTAINER_URL"
[[ $? -ne 0 ]] && echo "DANGER Will Robinson, azcopy of file ($FILE) failed, exiting..." && exit 3
# Apparently we didn't exit prior to this point so it should be safe to move the file
# Don't forget to QUOTE your variables, it will mess you up (e.g. spaces in filenames, empty variables)
# Take care when mv-ing files, in some cases it's better to cp and then remove (here it's fine)
mv "$FILE" "$PROCESSED_DIR"
# Ah well let's return something
exit 0