Ubuntu – “syntax error: unexpected end of file” simple script

bashscripts

i need some help for a very siple script, i don't get were the fault is.
The script :

#!/bin/bash

declare -i s
declare -i m
declare -i h

if [ "$3" < 50 ]; then s=$3+10  m=$2  h=$1
        else if ["$2" < 50 ];
                then s=$3-50  m=$2+1  h=$1
                    else s=$1-50  m=$2-50  h=$1+1 fi 
fi 

echo "$h:$m:$s"

The script add 10sc to the time we've entered.

I get this error message : "sub_change_dirrect: line 14: syntax error: unexpected end of file"

Best Answer

There are three obvious errors:

  • On the line else s=$1-50 m=$2-50 h=$1+1 fi, the word fi is not treated as a keyword, because it is not the first word in the command. To the shell, this looks like three assignments that apply to the command fi. If you ever got to execute this line, you'd see an error bash: fi: command not found. Put fi on a line of its own (or put a ; before it).
  • [ "$3" < 50 ] is the same as [ "$3" ] < 50 — it's the command [ … ] (which can also be written test) with the sole argument "$3", and with an input redirection from the file 50. Either use the numeric comparison operator -lt, or use an arithmetic instruction (( … )). The single bracket construct is an ordinary built-in command, so special characters such as < retain their normal meaning. The double parenthesis construct is special syntax, and you can use < as a numeric comparison operator inside.
  • ["$2" < 50 ] is missing a space after the opening bracket.

Also the usual convention in shell scripts is to put a newline after then and else. Furthermore, instead of an else block that consists entirely of an if statement, you should use elif. And please indent consistently.

#!/bin/bash
declare -i s
declare -i m
declare -i h

if (( $3 < 50 )); then
  s=$3+10  m=$2  h=$1
elif (( $2 < 50 )); then
  s=$3-50  m=$2+1  h=$1
else
  s=$1-50  m=$2-50  h=$1+1
fi 

echo "$h:$m:$s"

P.S. I haven't reviewed your logic. You seem to be looking for date +%T -d 'now + 10 seconds'.

Related Question