0

I have this bash script [it is more complex but putting only relevant bits] and I want to skip running it the hours is between 9pm and 7pm. Here it is:

#!/bin/sh

currenttime=$(date +%H:%M)
# echo ${currenttime}

if [[ "$currenttime" > "21:00" ]] || [[ "$currenttime" < "07:00" ]]; then
    echo 'Gitlab might be down at these hours, skipping translation sync script' 1>&2
    exit ${EXIT_CODE}
fi

It works like expected but for the love of life I cannot figure out why it is creating an empty file called 21:00 in the folder of the .sh script:

enter image description here

Can you help me out and make the code not generate that empty file? What's the deal, why only for 21:00 it creates that? I am not the best at bash scripts.

Filip
  • 346
  • 1
  • 4
  • 15
  • oh so i should use -gt? – Filip Sep 04 '21 at 15:27
  • 2
    are you sure your script uses double brackets (`[[...]]`), or does your script use single brackets (`[...]`)? the latter will treat `"$currenttime"` as a command with output written to a file named `21:00` (`> "21:00"`) – markp-fuso Sep 04 '21 at 15:28
  • Split hours and minutes to different variables. That makes it easier. – Cyrus Sep 04 '21 at 15:30
  • https://i.imgur.com/JoErmsd.png code screenshot, exact same code as pasted above – Filip Sep 04 '21 at 15:30
  • @MarcoLucidi: If the shell does not support `[[ ... ]]`, then he should get an error message. – Cyrus Sep 04 '21 at 15:32
  • it supports [[ .. ]] cause it works ok, just spits out that file but behaviour works ok – Filip Sep 04 '21 at 15:33
  • curious ... what OS is this running on? what's the output from `/bin/sh --version`? – markp-fuso Sep 04 '21 at 15:35
  • 3
    `#!/bin/sh` is POSIX shell, NOT bash. It doesn't support `[[ .. ]]` Did you mean `#!/bin/bash`? – David C. Rankin Sep 04 '21 at 15:37
  • It is a docker image ```/usr/src/app # /bin/sh --version /usr/src/app # /bin/sh /usr/src/app # /bin/sh --help BusyBox v1.31.1 () multi-call binary.``` – Filip Sep 04 '21 at 15:37
  • 1
    `busybox sh` isn't exactly `sh` nor is it `bash`; playing with this [online busybox](https://busybox.net/live_bbox/live_bbox.html) it looks like `[[` is a sibling function to `[` (whereas in `bash` the `[[` is a keyword); also, it appears the `[[` behaves like `[`, ie, `"$currenttime"` is being treated as a command and thus `> "21:00"` is a redirection of output to a file named `21:00`; I can eliminate this behavior by escaping the `>`, eg: `if [[ "$currenttime" \> "21:00" ]] ...`; yeah, a bit of a kludge but this may just be my lack of knowledge re: `busybox`'s (different) shell behavior – markp-fuso Sep 04 '21 at 16:02

0 Answers0