user452187
user452187

Reputation: 123

Linux shell script Date arithmetic

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!

Upvotes: 3

Views: 3518

Answers (2)

Marco Leogrande
Marco Leogrande

Reputation: 8458

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.

Upvotes: 2

P.P
P.P

Reputation: 121347

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

Upvotes: 3

Related Questions