0

Here is what my current code is and haven't been able to figure out a solution. So here is the code:

#!/bin/bash

DATE_TIME=`date +%s`

FILE_OUTPUT=`ls -l --time-style=long-iso /data | grep drwxrwxrwx | awk '{print $6,$7","$8","}'`

rm -rf /tmp/tmp_direc_last_update.log
echo "$FILE_OUTPUT" > /tmp/tmp_direc_last_update.log

ADD_EPOCH=`sed -e "s/$/"$DATE_TIME",/" -i /tmp/tmp_direc_last_update.log`
FINAL_FILE=`cat /tmp/tmp_direc_last_update.log` 
echo "$FINAL_FILE" > /data/stats/direc_last_update.log

What I need to do is to check if the /tmp/tmp_direc_last_update.log is empty and if it is, don't do any of the last three lines of code.

I've tried putting a if statement with [ -s /tmp/tmp_direc_last_update.log] but I always get an error saying that I'm missing a ']'.

My linux bash scripting skills are not that great, so any help anyone could assist with would be greatly appreciated.

Thanks!

  • You need a space between `log` and `]` – Charles Duffy Aug 30 '21 at 15:36
  • 1
    As a general rule, run your code through http://shellcheck.net/ before asking questions here. – Charles Duffy Aug 30 '21 at 15:36
  • 1
    Also, don't use backticks -- they've been obsolete since the 1992 publication of POSIX sh, which uses `variable=$(somecommand)` instead. – Charles Duffy Aug 30 '21 at 15:37
  • Also, all-caps variable names are reserved -- your own variables should use lowercase names to avoid conflicts (lots of folks run `for PATH in /dir/*/; do` and then wonder why their shell can't run any other commands; `for path in` doesn't have that problem). – Charles Duffy Aug 30 '21 at 15:37
  • ...to follow up re: "reserved" -- see the fourth paragraph of https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html. While that specification only strictly applies to regular environment variables, setting a shell variable overwrites any like-named environment variable, so one can't follow the convention for one without the other. – Charles Duffy Aug 30 '21 at 16:42
  • Thanks @CharlesDuffy for your assistance, I've made the modifications and will be resubmitting the question. I didn't know about shellcheck, but will be using that in the future. Backticks and all-caps variables were taught to me by my supervisor, so I didn't know it had been deprecated and will go back and modify my scripts to use the new formatting. This has been very informative and educational. Thanks again! – testymctesterson Aug 31 '21 at 15:07

0 Answers0