Project

General

Profile

glidein_lib.sh

Marco Mambelli, 01/29/2018 03:24 PM

 
1
#!/bin/bash
2

    
3
function sanitizedCat {
4
    # This function prints the saniteized content of the file passed as argument.
5
    # If the content is a number it just prints it, if it is an alphanumeric string it adds quotes
6
    # and prints it, otherwise it prints "WrongFormat"
7
    VAL=$(cat "$1")
8
    if [[ $VAL =~ ^[0-9]+(.[0-9]+)?$ ]]; then
9
        echo $VAL
10
    elif [[ $VAL =~ ^[^\"\\]+$ ]]; then
11
        echo \"$VAL\"
12
    else
13
        echo '"WrongFormat"'
14
    fi
15
}
16

    
17
function getValueFromFileOrURL {
18
    # The function takes as an argument a filename and a variable name
19
    # The variable contains the url or the directory location of the file,
20
    # so $1 can be shutdowntime_job
21
    # and $2 /path/to/jobfeature/dir
22
    # The function returns the value found in the file (by cat-ing it), or Unknown
23
    # if the file does not exist or $2 is empty
24
    FILENAME="$1"
25
    VARNAME="$2"
26
    if [ -n "$VARNAME" ]; then
27
        if [ -f "$VARNAME/$FILENAME" ]; then
28
            sanitizedCat "$VARNAME/$FILENAME"
29
            return
30
        else
31
            #check if shutdowntime job is a URL and wget it
32
            ADDRESS="$VARNAME/$FILENAME"
33
            echo $ADDRESS | grep -E '^https?' > /dev/null
34
            if [ $? -eq 0 ]; then
35
                #use quiet mode and redirect file to a temporary one
36
                TMPFILE=tmp_$(uuidgen)
37
                wget -qO- $ADDRESS > $TMPFILE
38
                if [ $? -eq 0 ]; then
39
                    sanitizedCat $TMPFILE
40
                    rm $TMPFILE
41
                    return
42
                fi
43
                rm $TMPFILE
44
            fi
45
        fi
46
    fi
47
    echo '"Unknown"'
48
}