Monday, May 20, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 479  / 2 Years ago, sun, april 3, 2022, 3:38:52

I was looking around on the internet to find if there was already a way to time a certain part of an executable/script to see how long that part takes, but all that I find is more for outside the file. So I want to input a line that starts a timer and at the end of the part stops the timer and shows me the elapsed time, not the whole thing but just a part.

example:



  #!/bin/sh
IPTABLES="/sbin/iptables"
BLOCKEDIPS_XS=/root/iptables/iptables/blockxs.zone
$IPTABLES -F
$IPTABLES -F INPUT
$IPTABLES -F OUTPUT
$IPTABLES --delete-chain
$IPTABLES -F FORWARD
$IPTABLES -F -t mangle
$IPTABLES -F -t nat
$IPTABLES -X
timer start
/bin/egrep -v "^#|^$" $BLOCKEDIPS_XS | while IFS= read -r ip
do
# Append everything to droplist
$IPTABLES -A droplist -i eth0 -s $ip -j LOG --log-prefix " Drop IP List blockxs "
$IPTABLES -A droplist -i eth0 -s $ip -j DROP
done <"$BLOCKEDIPS_XS"
timer stops, shows elapsed time

More From » command-line

 Answers
3

A time that operates on command groups and the SECONDS variable are both bash features, not in sh. With sh, you can get the timestamps at the two points using the date command, and then get the difference. date +%s will give the time in seconds since the epoch.



# timer start
start=$(date +%s)
/bin/egrep -v "^#|^$" $BLOCKEDIPS_XS | while IFS= read -r ip
do
# Append everything to droplist
$IPTABLES -A droplist -i eth0 -s $ip -j LOG --log-prefix " Drop IP List blockxs "
$IPTABLES -A droplist -i eth0 -s $ip -j DROP
done <"$BLOCKEDIPS_XS"
# timer stops, shows elapsed time
echo $(( $(date +%s) - start ))

[#8203] Monday, April 4, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
irtuallyefu

Total Points: 429
Total Questions: 97
Total Answers: 119

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
;