This question already has answers here:
Value too great for base (error token is "09")
(7 answers)
Value too great for base (error token is "08") [duplicate]
(1 answer)
Closed 5 months ago.
I have a script that looks at the current time, does some tasks and notifies me 1 minute before the timer ends. For some reason there is a problem and the script does not pass the condition "IF"
I read that it seems like bash interprets numbers not as a decimal number. I tried to solve the problem by removing the first zero from the string. Bash will understand this as a decimal number and perform a subtraction operation.
For example. Turn off the computer after 10 minutes. Now is 01:00:00. Shutdown at 01:10:00
The script makes a timecode file with name "T01H10M.shutdown" and takes all the necessary numbers of hour and minute from it.
When the time comes to 01:09:00, the script notifies me about the imminent completion of the work.
#Search shutdown time from file name
SearchFilenameDateWhenPCgotoShutdown="$(find /root/ -name '*.shutdown')"
SearchTimeHour=$(echo $SearchFilenameDateWhenPCgotoShutdown | cut -f2 -d "T"| cut -f1 -d "H")
SearchTimeMinute=$(echo $SearchFilenameDateWhenPCgotoShutdown | cut -f2 -d "H" | cut -f1 -d "M")
#Calculation of the penultimate minute and сorrection of the subtraction error.
SearchTimeMinuteMinus1=$(($SearchTimeMinute - 1 ))
if [ $SearchTimeMinuteMinus1 -eq -2 ]; then
SearchTimeMinuteMinus1=58;
fi
if [ $SearchTimeMinuteMinus1 -eq -1 ]; then
SearchTimeMinuteMinus1=59
fi
#Determining the current hour and minute
RealTimeHour=$(date "+%H")
RealTimeMinute=$(date "+%M")
if [ $RealTimeMinute = 00 ];
then
RealTimeMinute=0;
else
RealTimeMinute=`echo $RealTimeMinute |sed 's/^0*//'`
fi
#The condition matches the current hour
if [ $SearchTimeHour = $RealTimeHour ]; then
if [ $SearchTimeMinuteMinus1 = $RealTimeMinute ]; then
curl -s "https://api.telegram.org/bla bla bla bla &> /dev/null
fi
And here there is an error that when the current time is in minutes from 00-07 minutes, the condition IF = 00 then = 0 else remove "0" work fine and corrects the number to decimal with a condition. Output 1, 2, 3...7 And when the minute is already 08, the script stops at the stage of checking minutes.
if [ $SearchTimeMinuteMinus1 = $RealTimeMinute ]; then
And I don't understand why!
I tried to do it in a separate script.
B=08
if [ $B = 00 ];
B=0;
else
B=`echo $B |sed 's/^0*//'`
fi
echo $B
He works. Output 8. Why I can't do it in the main script, I don't understand.
Screenshot with each 2 min script run
Help please.
--------------------UPD 4.10.22--------------------
OK! Thank you all for your advice. I fixed the code and it works. But! Another problem :D When the time comes at 59 minutes. An error occurs in stage.
if [ $SearchTimeHour = $RealTimeHour ]; then
Problem
This is very strange! After all, all variables are defined correctly. Any ideas?
Full code:
#!/bin/bash
if ping -c 2 100.100.100.100 | grep "ttl"; then
if ping -c 2 192.168.10.1 | grep "ttl"; then
if ping -c 2 192.168.88.102 | grep "ttl"; then
#OMV name
NameOMV="Standart OMV"
#The option determines how many minutes to turn off the system
ShutDownParameterMinutes=2
#Defining the startmark file in the script
startmark=/root/startmark
#Checking the existence of the startmark file
if [ -f $startmark ]
then
#Recording when to switch off to a variable
SearchFilenameDateWhenPCgotoShutdown="$(find /root/ -name '*.shutdown')"
#Determination of the hour and minute of shutdown
SearchTimeHour=$(echo "$SearchFilenameDateWhenPCgotoShutdown" | cut -f2 -d "T"| cut -f1 -d "H")
SearchTimeMinute=$(echo "$SearchFilenameDateWhenPCgotoShutdown" | cut -f2 -d "H" | cut -f1 -d "M")
#Determination of the hour and minute of shutdown minus 1 minute for notification in telegrams
#Correction of the subtraction error of 2 minutes - 2 minutes becomes 58 minutes, and -1 = 59 minutes
SearchTimeMinuteMinus1=$((SearchTimeMinute - 1 ))
if [ "$SearchTimeMinuteMinus1" -eq -2 ]; then
SearchTimeMinuteMinus1=58
fi
if [ "$SearchTimeMinuteMinus1" -eq -1 ]; then
SearchTimeMinuteMinus1=59
fi
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 SearchTimeMinuteMinus1=${SearchTimeMinuteMinus1}" &> /dev/null
#Determining the current hour and minute
RealTimeHour=$(date "+%H")
RealTimeMinute=$(date "+%M")
RealTimeMinute=$((10#$RealTimeMinute))
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 RealTimeHour=${RealTimeHour}" &> /dev/null
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 RealTimeMinute=${RealTimeMinute}" &> /dev/null
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 TICK $(date "+%d.%m.%Y - %H:%M:%S")" &> /dev/null
#The condition matches the current hour
if [ "$SearchTimeHour" -eq "$RealTimeHour" ]; then
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 Hour OK" &> /dev/null
if [ "$SearchTimeMinuteMinus1" -eq "$RealTimeMinute" ]; then
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 Minute OK" &> /dev/null
rm "$SearchFilenameDateWhenPCgotoShutdown"
rm startmark
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 $NameOMV is less 1 minutes left before shutdown" &> /dev/null
fi
fi
else
#Creating a startmark
touch startmark
#Notification in telegram about the start of the shutdown procedure
echo "OrangePi is creating startmark to offline: $(date "+%d.%m.%Y - %H:%M:%S")" >> shutdownlog.txt
#We determine the time when to turn off
DateWhenPCgotoShutdown=$(date +"T""%H""H""%M""M" --date="$ShutDownParameterMinutes minute")
TimeStampStartName="${DateWhenPCgotoShutdown}.shutdown"
touch "$TimeStampStartName"
chmod +x "$TimeStampStartName"
#Notification in telegram when to turn off the computer
TelegramMessage="OrangePi $NameOMV is creating startmark to offline: $(date "+%d.%m.%Y - %H:%M:%S" --date="$ShutDownParameterMinutes minute")"
echo "${TelegramMessage}" >> shutdownlog.txt
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 $TelegramMessage" &> /dev/null
#/sbin/shutdown -h +$ShutDownParameterMinutes
echo "SHUTDOWN"
fi
fi
fi
fi
With respect.
I used this bash script here to download only short parts of long yt videos:
#!/bin/bash
#taken from https://unix.stackexchange.com/a/388148/48971
if [ $# -lt 4 ]; then
echo "Usage: $0 <youtube's URL> <HH:mm:ss.milisecs from time> <HH:mm:ss.milisecs to time> <output_file_name>"
echo "e.g.:"
echo "$0 https://www.youtube.com/watch?v=T1n5gXIPyws 00:00:25 00:00:42 intro.mp4"
exit 1
fi
echo "processing..."
from=$(date "+%s" -d "UTC 01/01/1970 $2")
to=$(date "+%s" -d "UTC 01/01/1970 $3")
from_pre=$(($from - 30))
if [ $from_pre -lt 0 ]
then
from_pre=0
fi
from_pre_command_print=$(date -u "+%T" -d #$from_pre)
from_command_print=$(date -u "+%T" -d #$(($from - $from_pre)))$(grep -o "\..*" <<< $2)
to_command_print=$(date -u "+%T" -d #$(($to - $from_pre)))$(grep -o "\..*" <<< $3)
command="ffmpeg "
for uri in $(youtube-dl -g $1)
do
command+="-ss $from_pre_command_print -i $uri "
done
command+="-ss $from_command_print -to $to_command_print $4"
echo "downloading with the following command:"
echo "$command"
$command
but the problem is its only precise to the full second.
I want to use it to download a bunch of very short clips (only one word long mostly) those are often below 1 second long.
I tryed to fix this by using milliseconds in the date.
But then i found out bash can only subtract with integers.
Here is what i tryed:
from=$(date "+%s.%3N" -d "UTC 01/01/1970 $2")
to=$(date "+%s.%3N" -d "UTC 01/01/1970 $3")
function diff {
diff="$(echo $from - 5 | bc)"
echo $diff
}
from_pre=$diff
echo $diff
but the workaround using bc didnt work because later in the script its again throwing errors because bash dosnt know what to do with non integers.
a sample command would look like this:
sh download_youtube.sh https://www.youtube.com/watch?v=dH3auOKyxio 00:06:28.230 00:06:28.740 clip004.mp4
and this works...if the the timeframe is more than 1 second.
Sadly i am out of knowlage here how to make it more precise.
This all is part of a project to make automated compilations of specific word from a youtube channel more on that here:
https://github.com/moeC137/video-recutter
I am very thankfull for every help
edit:
after a bit try an error i found out that this seams to work for me:
#!/bin/bash
#taken from https://unix.stackexchange.com/a/388148/48971
if [ $# -lt 4 ]; then
echo "Usage: $0 <youtube's URL> <HH:mm:ss.milisecs from time> <HH:mm:ss.milisecs to time> <output_file_name>"
echo "e.g.:"
echo "$0 https://www.youtube.com/watch?v=T1n5gXIPyws 00:00:25 00:00:42 intro.mp4"
exit 1
fi
echo "processing..."
from=$(date "+%s" -d "UTC 01/01/1970 $2")
to=$(date "+%s" -d "UTC 01/01/1970 $3")
from_pre=$(($from - 20))
#to_post=$(($to + 20))
if [ $from_pre -lt 0 ]
then
from_pre=0
fi
from_pre_command_print=$(date -u "+%T" -d #$from_pre)
from_command_print=$(date -u "+%T" -d #$(($from - $from_pre)))$(grep -o "\..*" <<< $2)
to_command_print=$(date -u "+%T" -d #$(($to - $from_pre)))$(grep -o "\..*" <<< $3)
#to_post_command_print=$(date -u "+%T.%3N" -d #$to_post)
command="ffmpeg "
for uri in $(youtube-dl -g $1)
do
command+="-ss $from_pre_command_print -i $uri "
done
command+="-ss $from_command_print -to $to_command_print $4"
echo "downloading with the following command:"
echo "$command"
$command
But i starting to think the orginal script actually works with milleseconds.
Because this:
sh download_youtube.sh https://www.youtube.com/watch?v=dH3auOKyxio 00:06:28.230 00:06:28.740 clip004.mp4
works, but when try to combine it with the script from here How to repeat a command for every line in a textfile with given arguments from the textfile (Bash?) it dosent use the milliseconds anymore.
Solved
The orginal Script worked. It just didnt like the "," in the timestamps, i replaced them with "." and now its working.
solved by replacing "," in timestamps with "."
Could anyone help me with a UNIX script that extracts the date from the last line of a file, compares it to current date, and if date from file is with 1 hour delay between current date, echo YES?
File.txt
18:48:43 iLIKEtoMOVEitMoveIT
18:58:43 iLIKEtoMOVEitMoveIT
19:22:43 iLIKEtoMOVEitMoveIT
clear line
So far I figured out how to get the last line which has the time:
tail -n 2 File.txt | head c-8
Output = 19:22:43
And how to store the current date as only time in a variable:
TheCurrentDate="date +"%T""
How to compare those 2 HH:MM:SS and calculate if one hour has passed between them, then echo"YES". All put in script.sh
DATE=$(tail -n 2 File.txt | cut -c 1-8 | head -n 1)
FROM_FILE=$(date -d "$DATE" +%s)
NOW=$(date +%s)
DIFFERENCE=$((NOW - FROM_FILE))
if [ $DIFFERENCE -le 3600 ]; then
echo YES
fi
The idea is to convert the timestamp to seconds since epoch (+%s) using date. Then you just compare numbers.
EDIT
Your File.txt should does seem like a log file, where the logging program doesn't bother to prepend the date. As #Jonathan Leffler pointed out, NOW could be 00:15 and FROM_FILE could be 23:45.
In this case, date would interpret FROM_FILE as being the end of today, rather than the end of yesterday. This can be fixed ad-hoc:
DATE=$(tail -n 2 File.txt | cut -c 1-8 | head -n 1)
FROM_FILE=$(date -d "$DATE" +%s)
NOW=$(date +%s)
if [ $FROM_FILE -gt $NOW ]; then
# it's not really in the future, it's from yesterday
FROM_FILE=$((FROM_FILE - 24 * 3600))
fi
DIFFERENCE=$((NOW - FROM_FILE))
if [ $DIFFERENCE -le 3600 ]; then
echo YES
fi
I want to read all dates between two range of dates and this ranges includes both start date and end date
input_start_date="2013-09-05"
input_end_date="2013-09-10"
START_DATE=$(date -I -d "$input_start_date") || exit -1
END_DATE=$(date -I -d "$input_end_date") || exit -1
d="$START_DATE"
while [ "$d" <= "$END_DATE" ]; do
echo $d
d=$(date -I -d "$d + 1 day")
done
but when I ran the above code I get below error
bash: = 2013-09-10: No such file or directory
Could someone help me to fix this issue
Expected output
2013-09-05
2013-09-06
2013-09-07
2013-09-08
2013-09-09
2013-09-10
start=2013-09-05
end=2013-09-10
while [[ $start < $end ]]
do
printf "$start\n"; start=$(date -d "$start + 1 day" +"%Y-%m-%d")
done
or you can try this one
END=$(date -d "2013-09-10" +%s);
DATE=$(date -d "2013-09-05" +%s);
while [[ "$DATE" -le "$END" ]]; do date -d "#$DATE" +%F; let DATE+=86400; done
The idea is right, but you just got the operator wrong, <= does not work with date strings in bash, you needed a inequality operator != in the condition.
while [ "$d" != "$enddate" ]; do
The <= operator works when used in arithmetic context in bash with the ((..)) operator.
Something little in awk (changed the range a bit since there was no test data, just the expected output):
$ awk '$0>="2013-09-06" && $0<="2013-09-09"' file
2013-09-06
2013-09-07
2013-09-08
2013-09-09
You kind of need a do-while loop here, which bash does not provide. How about
date="$start_date"
while true; do
echo "$date"
[[ $date = "$end_date" ]] && break
date=$(date -d "$date + 1 day" "+%F")
done
Don't use ALL_CAPS_VAR_NAMES -- too easy to mistakenly overwrite shell/system vars.
I am writing a script in bash to calculate the time elapsed for the execution of my commands, consider:
STARTTIME=$(date +%s)
#command block that takes time to complete...
#........
ENDTIME=$(date +%s)
echo "It takes $($ENDTIME - $STARTTIME) seconds to complete this task..."
I guess my logic is correct however I end up with the following print out:
"It takes seconds to complete this task..."
Anything wrong with my string evaluation?
I believe bash variables are untyped, I would love if there is a "string to integer" method in bash nevertheless.
I find it very clean to use the internal variable "$SECONDS"
SECONDS=0 ; sleep 10 ; echo $SECONDS
Either $(()) or $[] will work for computing the result of an arithmetic operation. You're using $() which is simply taking the string and evaluating it as a command. It's a bit of a subtle distinction.
As tink pointed out in the comments on this answer, $[] is deprecated, and $(()) should be favored.
You are trying to execute the number in the ENDTIME as a command. You should also see an error like 1370306857: command not found. Instead use the arithmetic expansion:
echo "It takes $((ENDTIME - STARTTIME)) seconds to complete this task..."
You could also save the commands in a separate script, commands.sh, and use time command:
time commands.sh
You can use Bash's time keyword here with an appropriate format string
TIMEFORMAT='It takes %R seconds to complete this task...'
time {
#command block that takes time to complete...
#........
}
Here's what the reference says about TIMEFORMAT:
The value of this parameter is used as a format string specifying how the timing information for pipelines prefixed with the time
reserved word should be displayed. The ‘%’ character introduces an
escape sequence that is expanded to a time value or other information.
The escape sequences and their meanings are as follows; the braces
denote optional portions.
%%
A literal ‘%’.
%[p][l]R
The elapsed time in seconds.
%[p][l]U
The number of CPU seconds spent in user mode.
%[p][l]S
The number of CPU seconds spent in system mode.
%P
The CPU percentage, computed as (%U + %S) / %R.
The optional p is a digit specifying the precision, the number of fractional digits after a decimal point. A value of 0 causes no
decimal point or fraction to be output. At most three places after the
decimal point may be specified; values of p greater than 3 are changed
to 3. If p is not specified, the value 3 is used.
The optional l specifies a longer format, including minutes, of the form MMmSS.FFs. The value of p determines whether or not the
fraction is included.
If this variable is not set, Bash acts as if it had the value
$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
If the value is null, no timing information is displayed. A trailing newline is added when the format string is displayed.
For larger numbers we may want to print in a more readable format. The example below does same as other but also prints in "human" format:
secs_to_human() {
if [[ -z ${1} || ${1} -lt 60 ]] ;then
min=0 ; secs="${1}"
else
time_mins=$(echo "scale=2; ${1}/60" | bc)
min=$(echo ${time_mins} | cut -d'.' -f1)
secs="0.$(echo ${time_mins} | cut -d'.' -f2)"
secs=$(echo ${secs}*60|bc|awk '{print int($1+0.5)}')
fi
echo "Time Elapsed : ${min} minutes and ${secs} seconds."
}
Simple testing:
secs_to_human "300"
secs_to_human "305"
secs_to_human "59"
secs_to_human "60"
secs_to_human "660"
secs_to_human "3000"
Output:
Time Elapsed : 5 minutes and 0 seconds.
Time Elapsed : 5 minutes and 5 seconds.
Time Elapsed : 0 minutes and 59 seconds.
Time Elapsed : 1 minutes and 0 seconds.
Time Elapsed : 11 minutes and 0 seconds.
Time Elapsed : 50 minutes and 0 seconds.
To use in a script as described in other posts (capture start point then call the function with the finish time:
start=$(date +%s)
# << performs some task here >>
secs_to_human "$(($(date +%s) - ${start}))"
Try the following code:
start=$(date +'%s') && sleep 5 && echo "It took $(($(date +'%s') - $start)) seconds"
This is a one-liner alternative to Mike Q's function:
secs_to_human() {
echo "$(( ${1} / 3600 ))h $(( (${1} / 60) % 60 ))m $(( ${1} % 60 ))s"
}
try using time with the elapsed seconds option:
/usr/bin/time -f%e sleep 1 under bash.
or \time -f%e sleep 1 in interactive bash.
see the time man page:
Users of the bash shell need to use an explicit path in order to run
the external time command and not the shell builtin variant. On system
where time is installed in /usr/bin, the first example would become
/usr/bin/time wc /etc/hosts
and
FORMATTING THE OUTPUT
...
% A literal '%'.
e Elapsed real (wall clock) time used by the process, in
seconds.
Combining internal variable "$SECONDS" in Lon Kaut's answer with ssc's one-liner under Internal Server Error's answer:
SECONDS=0
sleep 2s
echo "Elapsed time: $((SECONDS/3600))h $(((SECONDS/60)%60))m $((SECONDS%60))s"
($/${} is unnecessary on arithmetic variables)
This is an old post and it seems that everybody like writing too much code here. But actually, you only need 3 lines of simple bash code in order to you can show properly the elapsed time in a well-formatted way.
START_TIME=$(date +%s)
# put your code here
ELAPSED=$(($(date +%s) - START_TIME))
printf "elapsed: %s\n\n" "$(date -d#$ELAPSED -u +%H\ hours\ %M\ min\ %S\ sec)"
This produces the following result:
elapsed: 0 hours 2 min 19 sec
Test the code:
START_TIME=$(date +%s)
ELAPSED=86399
printf "elapsed: %s\n\n" "$(date -d#$ELAPSED -u +%H\ hour\ %M\ min\ %S\ sec)"
elapsed: 23 hour 59 min 59 sec
If you would like to show the days as well, then you need to do a little trick in order for the day will be displayed properly:
ELAPSED=86399
printf "elapsed: %s\n\n" "$(date -d#$ELAPSED -u +%d\ days\ %H\ hour\ %M\ min\ %S\ sec)"
elapsed: 01 days 23 hour 59 min 59 sec
ELAPSED=86400
elapsed: 02 days 00 hour 00 min 00 sec
As you can see the day is calculated on a wrong way, so you need to do a math operation in bash this way:
printf "elapsed: %s day %s\n\n" "$(($(date -d#$ELAPSED -u +%d)-1))" "$(date -d#$ELAPSED -u +%H\ hour\ %M\ min\ %S\ sec)"
elapsed: 1 day 00 hour 00 min 00 sec
The \ character in the date-time pattern escapes the whitespace.
I hope that it helps you.
start=$(date +%Y%m%d%H%M%S);
for x in {1..5};
do echo $x;
sleep 1; done;
end=$(date +%Y%m%d%H%M%S);
elapsed=$(($end-$start));
ftime=$(for((i=1;i<=$((${#end}-${#elapsed}));i++));
do echo -n "-";
done;
echo ${elapsed});
echo -e "Start : ${start}\nStop : ${end}\nElapsed: ${ftime}"
Start : 20171108005304
Stop : 20171108005310
Elapsed: -------------6
#!/bin/bash
time_elapsed(){
appstop=$1; appstart=$2
ss_strt=${appstart:12:2} ;ss_stop=${appstop:12:2}
mm_strt=${appstart:10:2} ;mm_stop=${appstop:10:2}
hh_strt=${appstart:8:2} ; hh_stop=${appstop:8:2}
dd_strt=${appstart:6:2} ; dd_stop=${appstop:6:2}
mh_strt=${appstart:4:2} ; mh_stop=${appstop:4:2}
yy_strt=${appstart:0:4} ; yy_stop=${appstop:0:4}
if [ "${ss_stop}" -lt "${ss_strt}" ]; then ss_stop=$((ss_stop+60)); mm_stop=$((mm_stop-1)); fi
if [ "${mm_stop}" -lt "0" ]; then mm_stop=$((mm_stop+60)); hh_stop=$((hh_stop-1)); fi
if [ "${mm_stop}" -lt "${mm_strt}" ]; then mm_stop=$((mm_stop+60)); hh_stop=$((hh_stop-1)); fi
if [ "${hh_stop}" -lt "0" ]; then hh_stop=$((hh_stop+24)); dd_stop=$((dd_stop-1)); fi
if [ "${hh_stop}" -lt "${hh_strt}" ]; then hh_stop=$((hh_stop+24)); dd_stop=$((dd_stop-1)); fi
if [ "${dd_stop}" -lt "0" ]; then dd_stop=$((dd_stop+$(mh_days $mh_stop $yy_stop))); mh_stop=$((mh_stop-1)); fi
if [ "${dd_stop}" -lt "${dd_strt}" ]; then dd_stop=$((dd_stop+$(mh_days $mh_stop $yy_stop))); mh_stop=$((mh_stop-1)); fi
if [ "${mh_stop}" -lt "0" ]; then mh_stop=$((mh_stop+12)); yy_stop=$((yy_stop-1)); fi
if [ "${mh_stop}" -lt "${mh_strt}" ]; then mh_stop=$((mh_stop+12)); yy_stop=$((yy_stop-1)); fi
ss_espd=$((10#${ss_stop}-10#${ss_strt})); if [ "${#ss_espd}" -le "1" ]; then ss_espd=$(for((i=1;i<=$((${#ss_stop}-${#ss_espd}));i++)); do echo -n "0"; done; echo ${ss_espd}); fi
mm_espd=$((10#${mm_stop}-10#${mm_strt})); if [ "${#mm_espd}" -le "1" ]; then mm_espd=$(for((i=1;i<=$((${#mm_stop}-${#mm_espd}));i++)); do echo -n "0"; done; echo ${mm_espd}); fi
hh_espd=$((10#${hh_stop}-10#${hh_strt})); if [ "${#hh_espd}" -le "1" ]; then hh_espd=$(for((i=1;i<=$((${#hh_stop}-${#hh_espd}));i++)); do echo -n "0"; done; echo ${hh_espd}); fi
dd_espd=$((10#${dd_stop}-10#${dd_strt})); if [ "${#dd_espd}" -le "1" ]; then dd_espd=$(for((i=1;i<=$((${#dd_stop}-${#dd_espd}));i++)); do echo -n "0"; done; echo ${dd_espd}); fi
mh_espd=$((10#${mh_stop}-10#${mh_strt})); if [ "${#mh_espd}" -le "1" ]; then mh_espd=$(for((i=1;i<=$((${#mh_stop}-${#mh_espd}));i++)); do echo -n "0"; done; echo ${mh_espd}); fi
yy_espd=$((10#${yy_stop}-10#${yy_strt})); if [ "${#yy_espd}" -le "1" ]; then yy_espd=$(for((i=1;i<=$((${#yy_stop}-${#yy_espd}));i++)); do echo -n "0"; done; echo ${yy_espd}); fi
echo -e "${yy_espd}-${mh_espd}-${dd_espd} ${hh_espd}:${mm_espd}:${ss_espd}"
#return $(echo -e "${yy_espd}-${mh_espd}-${dd_espd} ${hh_espd}:${mm_espd}:${ss_espd}")
}
mh_days(){
mh_stop=$1; yy_stop=$2; #also checks if it's leap year or not
case $mh_stop in
[1,3,5,7,8,10,12]) mh_stop=31
;;
2) (( !(yy_stop % 4) && (yy_stop % 100 || !(yy_stop % 400) ) )) && mh_stop=29 || mh_stop=28
;;
[4,6,9,11]) mh_stop=30
;;
esac
return ${mh_stop}
}
appstart=$(date +%Y%m%d%H%M%S); read -p "Wait some time, then press nay-key..." key; appstop=$(date +%Y%m%d%H%M%S); elapsed=$(time_elapsed $appstop $appstart); echo -e "Start...: ${appstart:0:4}-${appstart:4:2}-${appstart:6:2} ${appstart:8:2}:${appstart:10:2}:${appstart:12:2}\nStop....: ${appstop:0:4}-${appstop:4:2}-${appstop:6:2} ${appstop:8:2}:${appstop:10:2}:${appstop:12:2}\n$(printf '%0.1s' "="{1..30})\nElapsed.: ${elapsed}"
exit 0
-------------------------------------------- return
Wait some time, then press nay-key...
Start...: 2017-11-09 03:22:17
Stop....: 2017-11-09 03:22:18
==============================
Elapsed.: 0000-00-00 00:00:01