Code: Select all
Uptime=$(awk -v fmt="$fmt" '{printf fmt, $1/86400, $1%86400/3600, $1%3600/60, $1%60}' /proc/uptime)
Code: Select all
Uptime=$(awk -v fmt="$fmt" '{printf fmt, $1/86400, $1%86400/3600, $1%3600/60, $1%60}' /proc/uptime)
I don't think so.some1 (bold marks by me) wrote:1) Learn to use printf
2) Dont use shell-variables inside awk-code
3) You need a default-case
4) Anyway - awk is too slow for this job.
Don't worry, awk is very fast - probably faster than anything else.ITSMERSH wrote:Even if I don't know how fast awk is doing this job, it is fast enough for my purposes.
Code: Select all
#!/bin/bash
echo ------1------
time uptime
echo ------2------
time {
UT=($(< /proc/uptime))
date -ud@"$UT" +'Uptime %-H hrs %M min'
}
echo ------3------
time {
set $(< /proc/uptime)
set $(date -ud@"$1" +'%j %-H %M')
echo "Uptime $(($1-1)) days $2 hrs $3 min"
}
echo ------4------
time awk '{printf "Uptime %d days %d hrs %d min\n", $1/86400, $1%86400/3600, $1%3600/60 }' /proc/uptime
Code: Select all
------1------
20:24:22 up 9:18, load average: 0.02, 0.04, 0.04
real 0m0.006s
user 0m0.000s
sys 0m0.003s
------2------
Uptime 9 hrs 18 min
real 0m0.009s
user 0m0.000s
sys 0m0.007s
------3------
Uptime 0 days 9 hrs 18 min
real 0m0.009s
user 0m0.000s
sys 0m0.003s
------4------
Uptime 0 days 9 hrs 18 min
real 0m0.004s
user 0m0.003s
sys 0m0.000s
Hi some1.some1 wrote:1) Learn to use printf
2) Dont use shell-variables inside awk-code
3) You need a default-case
4) Anyway - awk is too slow for this job.Code: Select all
case "${LANG:0:2}" in fr)Session=Session fmt="%d j %d h %d m %d s" ;; de)Session=Sitzung fmt="%d Tg %d Std %d Min %d Sek" ;; *)Session=Session fmt="%d d %d h %d m %d s" ;; esac Uptime="$(awk -v fmt="$fmt" '{printf fmt,int($1/86400),int($1%86400/3600),int(($1%3600)/60),int($1%60);}' /proc/uptime)"
Code: Select all
echo $Uptime
Code: Select all
echo ------4------
time awk '{printf "Uptime %d days %d hrs %d min\n", $1/86400, $1%86400/3600, $1%3600/60 }' /proc/uptime
echo ------5------
time {
set $(< /proc/uptime)
UT=${1%.*}
printf "Uptime %d days %d hrs %d min\n" $((UT/86400)) $((UT%86400/3600)) $((UT%3600/60))
}
echo ------6------
time {
set $(< /proc/uptime)
UT=${1%.*}
echo "Uptime $((UT/86400)) days $((UT%86400/3600)) hrs $((UT%3600/60)) min"
}
Code: Select all
------4------
Uptime 0 days 12 hrs 16 min
real 0m0.004s
user 0m0.000s
sys 0m0.000s
------5------
Uptime 0 days 12 hrs 16 min
real 0m0.002s
user 0m0.000s
sys 0m0.000s
------6------
Uptime 0 days 12 hrs 16 min
real 0m0.001s
user 0m0.000s
sys 0m0.000s
Code: Select all
set $(cat /proc/uptime)
UT=${1%.*}
echo $UT
Code: Select all
[~]>cat /proc/uptime
89377.92 139265.34
[~]>set $(< /proc/uptime);UT=${1%.*};echo $UT
89563
You haven't seen strange code yet. Try this:ITSMERSH wrote:Though, strange code!
Code: Select all
#!/bin/bash
[[ $(< /proc/uptime) =~ [0-9]* ]]
echo "Uptime $((BASH_REMATCH/86400)) days $((BASH_REMATCH%86400/3600)) hrs $((BASH_REMATCH%3600/60)) min"
Source.MochiMoppel wrote:You haven't seen strange code yet. Try this:ITSMERSH wrote:Though, strange code!Code: Select all
#!/bin/bash [[ $(< /proc/uptime) =~ [0-9]* ]] echo "Uptime $((BASH_REMATCH/86400)) days $((BASH_REMATCH%86400/3600)) hrs $((BASH_REMATCH%3600/60)) min"
musher0 wrote:Source.MochiMoppel wrote:You haven't seen strange code yet. Try this:ITSMERSH wrote:Though, strange code!Code: Select all
#!/bin/bash [[ $(< /proc/uptime) =~ [0-9]* ]] echo "Uptime $((BASH_REMATCH/86400)) days $((BASH_REMATCH%86400/3600)) hrs $((BASH_REMATCH%3600/60)) min"
MochiMoppel wrote:Source
musher0 wrote:MochiMoppel wrote:Source