0

I call the following script from another bash script to check for file changes. If the SHA changes from one execution to the next, the google doc has been updated.

The script (tries to) accept a google drive doc ID as a parameter, then tries a few times to get the info (because gdrive fails randomly). The results are several lines long,so the script does a SHA on the results to get a unique short result.

It was working (when gdrive would return results), so I added the loop and failure message to make it a little more robust, but...

I must be doing something wrong with the if and possibly the while statements in the following script because the script cycles through only once even when the gdrive info results fail. Also when the string to test the length of is set to something deliberately short.

If I had hair, I'd be pulling it out.

#!/bin/bash
maxAttempts=10
minResult=100  # gdrive errors are about 80 characters

# If there was no parameter, give an error  
[[ -z $1 ]] && errorMsg="Error: no google docs ID provided as a parameter" && echo $errorMsg && exit 0

# With an ID, get the file info, which includes a timestamp and return the SHA
attemptCount=0
strLength=1 
while [[ "$strLength" < "$minResult" && "$attemptCount" < "$maxAttempts" ]];
do
    ((attemptCount++))
    fileInfo="$(gdrive info $1)"
    #fileInfo="TESTXXX" # use for testing different message lengths
    strLength=${#fileInfo}  # if under 100, the grive attempt failed
    timeStamp="$(echo -n $fileInfo | sha256sum )"
    echo $fileInfo
    if [[ $strLength < $minResult ]]; then
        sleep 10
        timeStamp="Failed to get timestamp after $attemptCount tries, or wrong ID provided"
    fi
done

#return the timestamp
echo $timeStamp

With the if statement at the end, I've tried single and double square brackets, double quotes around the variables, -gt and < and even putting in the numerical values 7 and 100 to try and force that section to execute and it is failing. I have if statements in other functioning scripts that look exactly the same. I'm going crazy. What am I not seeing wrong? Help please.

1
  • See this comment. Also I recommend you to use set -x for debugging Commented Mar 25, 2019 at 10:05

1 Answer 1

1

Use round parenthesis for numerical operations:

if (( strLength < minResult )); then

But make sure that the variables contain numerical values. This can be done with declare:

declare -i strLength
declare -i minResult
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the speedy response. I also just found that these worked: while [[ "$var1" -lt "$var2 ]]; if [[ "$var1" -lt "$var2 ]];then I must have tried every possible combination but that, or messed up the -lt -gt earlier today.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.