1

Ubuntu 16.04 - trying to fine tune

Here is my code

#!/bin/bash

# if the .md5sum file doesn't exists
# or if the .md5sum file exists && doesn't match, recreate it
# but if the .md5sum file exists && matches then break and log

csvfile=inventory.csv

if [ ! -f .md5sum ] || [ -f .md5sum ] && [ ! md5sum -c .md5sum >/dev/null ]; then
   md5sum "$csvfile" > .md5sum
else
   echo "$csvfile file matched md5sum ..."
   break
fi

I am having issues with building the conditional statement that would result in a new .md5sum. I am trying to find what part of the conditional statement is wrong and this is what shellcheck tells me.

root@me ~ # shellcheck run.sh

In run.sh line 8:
if [ ! -f .md5sum ] || [ -f .md5sum ] && [ ! md5sum -c .md5sum >/dev/null ]; then
^-- SC1009: The mentioned parser error was in this if expression.
                                         ^-- SC1073: Couldn't parse this test expression.
                                                               ^-- SC1072: Expected "]". Fix any mentioned problems and try again.

I can do the entire process with a few comparisons but I think I'll achieve a faster and cleaner script if I joined the comparisons.

I also checked the code by Jesse_P

#!/bin/bash

csvfile=inventory.csv

echo "hello" > inventory.csv
md5sum "$csvfile" > .md5sum
echo "well hello" > inventory.cvs

if [[ ! -f .md5sum ]] || [[ -f .md5sum && ! md5sum -c .md5sum >/dev/null ]]; then
        echo "All conditiions have been met to generate a new md5sum for inventory.csv ..."
        md5sum inventory.csv > .mds5sum
fi
exit 0

Then I used shellcheck

root@0003 ~ # shellcheck run.sh
    In run.sh line 8:
    if [[ ! -f .md5sum ]] || [[ -f .md5sum && ! md5sum -c .md5sum >/dev/null ]]; then
    ^-- SC1009: The mentioned parser error was in this if expression.
                             ^-- SC1073: Couldn't parse this test expression.
                                                                  ^-- SC1072: Expected "]". Fix any mentioned problems and try again.
  • if [ ! -f .md5sum ] || ! md5sum -c .md5sum >/dev/null; then – jordanm Mar 31 '18 at 19:23
  • if [[ ! -f .md5sum ]] || [[ -f .md5sum && ! md5sum -c .md5sum >/dev/null ]]; then – Jesse_b Mar 31 '18 at 19:25
  • @Jesse_b I added your comment to my question with shellcheck results. – needtoknow Mar 31 '18 at 23:47
  • @needtoknow: Shellcheck isn't always right. What I wrote should work but like shellcheck I also wouldn't recommend it. – Jesse_b Apr 1 '18 at 15:33
1

Proper code

#!/bin/sh

if [ ! -f .md5sum ] || [ -f .md5sum ] && ! md5sum -c .md5sum > /dev/null
then
    echo "All conditions have been met to generate a new md5sum for inventory.csv ..."
    md5sum inventory.csv > .mds5sum
fi

Parsing

[ ! -f .md5sum ]
[ -f .md5sum ]

These are ordinary test commands, so POSIX-ly [...], no need for double brackets.

! md5sum -c .md5sum > /dev/null

This is NOT a test command, so NO brackets around it.

Notes

  1. No need for bash, sh will do in this script. I can only recommend to stick with POSIX shell (sh) as long as you can for portability purposes.

  2. md5sum has been superseded, e.g. with sha512sum. No hashing algorithm is perfect, but md5sum is known not to be collision-resistant especially. I can only recommend to use sha512sum as long as you don't need extra short hash.

  3. Read some more about the test command.

  4. Optionally, read some more about POSIX.

  • Your explanation was fantastic. Me hats off to ya mate. – needtoknow Apr 1 '18 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.