0

How can I convert this simple python code to Shell script?

 import time
 import sys
 cur_time = int(time.time()*1000)
 print cur_time
 sys.exit(1)
asked May 2, 2016 at 12:52

3 Answers 3

2

It's just multiplying the seconds since epoch by 1000 (with some added nanosecond precision).

You can do:

$(($(date '+%s') * 1000))

With the nanosecond precision, inzsh:

$(($(date '+%s.%N') * 1000))

Precision to 2 decimal points, in zsh:

printf '%.2f\n' $(($(date '+%s.%N') * 1000))

As bash (and other shells) does not support floating point arithmetic, you can use bc instead.

Example:

% echo $(($(date '+%s') * 1000))
1462194433000
% echo $(($(date '+%s.%N') * 1000))
1462194596950.2983
% printf '%.2f\n' $(($(date '+%s.%N') * 1000))
1462194696479.11
answered May 2, 2016 at 13:05
Sign up to request clarification or add additional context in comments.

2 Comments

date +%s delivers: %s
This does not change things. A more portable way to print the current time_t value is to run awk 'BEGIN{srand(); print srand() }'.
0

You can test scrptine.I think it can help you.

answered May 2, 2016 at 13:01

Comments

0

You can change the pemission

 chmod +x file

and add in the first line

#!/usr/bin/env python

If I am correct it's used to call without the preceding language. The script then calls the language's interpreter to run the code inside the script

answered May 2, 2016 at 13:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.