Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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"

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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'.

share|improve this answer
    
[[ $3 < 50 ]] compares $3 with 50 lexicographically, so if $3 is 6 for instance, the command returns false, since 6 comes after 5. use (( )) for arithmetic comparison. –  geirha Jun 3 '12 at 14:47
    
@geirha Oh, yes, of course. Fixed. Thanks. –  Gilles Jun 3 '12 at 14:52
    
Ok, thank you very much, sorry for these dumb errors, it's one of my first scripts. Obviously, it's more different then coding then I thought. So fi is a command, the spaces are important, and [] is used for char/strings. And yes it's what I want the script to do, not to get the present time + 10sc. Thank you again. –  Sam Jun 3 '12 at 18:28
    
Oh, I just noticed an error. I meant : elif (( $2 < 59 )); then s=$3-50 m=$2+1 h=$1 else s=$3-50 m=0 h=$1+1 fi –  Sam Jun 3 '12 at 18:40

You forget to a ; after the nested fi statement

#!/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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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