0

Hi I am trying to set up a script in Tomato router that when it runs it will echo a value dependent on the hour. Script written as follow:

HOUR=$(date +"%k")

if [[ $HOUR >= 0 ] && [ $HOUR < 12 ]];then
    echo 'Morning'
else
    echo 'Evening'
fi

However when I execute the script, the following returned:

/tmp/.wxNL0Yv3: line 11: can't create =: Read-only file system 
Evening 

How to fix it? I am pretty sure I did not ask for an access to any Read-only files

Ben L
  • 1
  • 1
  • `>=` asks for write access to a file called `=`. It is *not* a comparison operator (unless you're in a math context). – Charles Duffy Mar 21 '14 at 14:04
  • By the way, are you **completely** sure that your shell really is bash, and not ash or a busybox-provided shell? It's very unusual to have actual bash on systems where space is at a premium, and the best answer if you have real bash is not the same as the best answer if you don't. – Charles Duffy Mar 21 '14 at 14:05
  • ...as another aside, all-uppercase variable names are reserved for environment variables and builtins. For a local like this that you aren't exporting, a lower-case variable name would be more in line with best practices (and prevent potential for collisions with the environment). – Charles Duffy Mar 21 '14 at 14:12

2 Answers2

2

If we assume that your shell isn't really bash, we'd change it as so:

hour=$(date +"%k")
if [ "$hour" -ge 0 ] && [ "$hour" -lt 12 ]; then
    echo 'Morning'
else
    echo 'Evening'
fi

If your shell really is bash, chepner's answer is more appropriate.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
1

The condition of your if statement should read

if [[ $HOUR -ge 0 && $HOUR -lt 12 ]]; then

Double brackets are not nested brackets, but the syntax for a conditional command. Also, for numeric comparisons you need to use -ge and -lt rather than the string operators < and (the nonexistent) >=.

>=, by the way, is interpreted as trying to redirect the output of a command to the file =, and you are on a read-only file system so the new file cannot be created. Redirections are processed prior to parameter expansion, so the shell does not yet know that $HOUR doesn't expand to a command name.

For a more natural looking expression, you can use an arithmetic command ((...)):

if (( $HOUR >= 0 && $HOUR < 12 )); then

In an arithmetic command, the dollar sign for variables is optional, but I like to use it, since it lets bash throw an error for undefined variables rather than treating them as being set to 0.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • No need to use the expansion operator in a math context. – Charles Duffy Mar 21 '14 at 14:36
  • Not necessary, but it does help catch a subtle bug if `HOUR` is not defined. `$HOUR` will expand to the empty string and cause a syntax error; `HOUR` will be treated as set and equal to 0. – chepner Mar 21 '14 at 15:06
  • *nod*. All that said, Tomato is targeted at hardware with something on the scale of 8MB of flash, and not much RAM either. I find it very, very hard to believe that they actually use bash as their shell. – Charles Duffy Mar 21 '14 at 15:09
  • (Looked into it; default shell is busybox-provided, but bash is available an optional package, so the claim that it's in use is plausible). – Charles Duffy Mar 21 '14 at 15:15