3

I have a shell script

 #!/bin/bash START=$(date +%s) echo " Start| $START " # do something # start your script work here # # your logic ends here END=$(date +%s) echo " End| $END " DIFF=$(( $END-$START )) echo " Diff in seconds|" $DIFF 

Still getting this error Start| 1349769151 End| 1349769151 ")49769151rror: invalid arithmetic operator (error token is "

I know it is basic, so I have been searched and debug for two days already. But still gets nothing works on this. Plz help!

2 Answers 2

3

The START and END are not in date format which can't be subtracted. If you just want to measure the execution time then use only %s which will give you difference in seconds.

START=$(date +%s) echo " Start| $START " # do something # start your script work here # # your logic ends here END=$(date +%s) echo " End| $END " DIFF=$(( $END-$START )) echo "Diff in seconds:" $DIFF 
Sign up to request clarification or add additional context in comments.

5 Comments

hi, KingsIndian, I changed my code like you show. But still getting some error. Help!
What error you get? It works ok for me. Can you check if you have the same code?
Still getting this error Start| 1349769151 End| 1349769151 ")49769151rror: invalid arithmetic operator (error token is "
There's no error in this part, you have some error where you have do something. Check the code you have in between calculating the time.
if you don't have any other code then it should work without error. To see some difference use sleep 5.
2

You are trying to perform arithmetic operations with strings: it will not work.

I suggest you to print the date as a timestamp with date "+%s" and use those numbers for your math.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.