1

I can't understand ,why i get this Error .

can anyone help me?

Errors:

Error : line 2: syntax error near unexpected token `)'
line 2: `time (){'

#! /bin/bash
time (){
    local hour="$1"
    local minute="$2"
    hour=${hour#0}
    minute=${minute#0}
    echo  $((hour * 60 + minute))   
}

start (){
    H_now=`date +%H:%M`
    H_now=${H_now/:/ }
    sum_now=$(time $H_now)
    read -p "Enter hour and minute the time, you want to start download:(hint: hour:minute)" hour
    $hour=${hour/:/ }
    sum_hm=$(time $hour)
    while [ "$sum_now" != "$sum_hm" ];do
        sleep 2
        if [ "$sum_now" == "$sum_hm" ]
        then

            echo "********************"
            echo "Download Started"
            echo "********************"
            wget   "https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz"      
                        
            
            
        fi
        H_now=`date +%H:M`
        H_now=${H_now/:/ }
        sum_now=$(time $H_now)

        
        
    done
    
    echo "********************"
        echo "Download Started"
    echo "********************"                                         
        wget   "https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz"   
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    `time` is a built-in, you can't use it as the name of a function. – Barmar Jul 29 '23 at 18:42
  • Are you reinventing `at` command? – Diego Torres Milano Jul 29 '23 at 19:20
  • in this scenario `time` is expecting to see a command (eg, `time wget ...`, `time sleep 30`) and instead what it sees is `()`; while the left paren is ok if you're planning to run a command in a subshell (eg, `time (wget ...)`, `time (sleep 30)`), what bash sees is an invalid subshell/command invocation thus the error message (ie, after the `(` instead of finding a command bash finds the closing `)` => invalid syntax) – markp-fuso Jul 29 '23 at 19:32
  • 1
    Its immensely frustrating that people aren't **required** to do this before posting a question, it'd save everyone so much time, but anyway .... as the [tag:bash] tag you used instructs "For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting them here." – Ed Morton Jul 29 '23 at 23:08
  • @EdMorton Stackpoverflow or other sites like this ,are place for learning and asking question. how can i learn with out having problem problem or be perfect. ihave to learn and here is right place to do mistake and learn about it . if you want save your time then why even you type such thing? – Sadegh_Ghaffarian Jul 30 '23 at 07:52
  • @DiegoTorresMilano I do! ;-) Have a look at [Sleep until a specific time/date](https://stackoverflow.com/a/19067658/1765658) – F. Hauri - Give Up GitHub Jul 30 '23 at 08:11
  • Instead of forking to `date` you could use `$EPOCH` variable (with time zone offset: `printf -v tzoff '%(%z)T\n' -1; tzoff=$((${tzoff::1}(${tzoff:1:2}*60+${tzoff:3:2})))`), then: `sum=$(( ( EPOCHSECONDS / 60 + tzoff ) % 1440 ))` and you could avoid *fork* to `date` and string manipulation... – F. Hauri - Give Up GitHub Jul 30 '23 at 09:01
  • @Sadegh_Ghaffarian no-ones suggesting questions have to be perfect, just that people asking questions should read the tags they use and do what those tags instruct or, far better, that the site require people asking questions to do what the tags say, e.g. if the site provided a template with code, input, and output boxes that runs shellcheck on shell code, that would be a huge time-saver and if it also ran the code on the input so we could see the actual output and error messages many questions would practically answer themselves. – Ed Morton Jul 30 '23 at 10:40
  • 1
    @EDMorton i totally understand what you mean . it was my first question and i try be better every time. i will learn from my mistake's . thanks for explanation. it means Alot for me – Sadegh_Ghaffarian Jul 30 '23 at 14:17

1 Answers1

1

time function was built-in function so i think that was problem. so i think instead of time i have named the function something else .

time_Test (){
local hour="$1"
local minute="$2"
hour=${hour#0}
minute=${minute#0}
echo  $((hour * 60 + minute))   
}
Cyrus
  • 84,225
  • 14
  • 89
  • 153