Code: Select all
sh-4.3# cat /proc/uptime
199514.08 312966.41
sh-4.3#
Is there a special way to read/format the results from /proc/uptime?
Code: Select all
sh-4.3# cat /proc/uptime
199514.08 312966.41
sh-4.3#
Well, it tells me that your computer is now up for 2 days 07 hours and 25 minITSMERSH wrote:This result/output is nothing I can use in any manner...Code: Select all
sh-4.3# cat /proc/uptime 199514.08 312966.41 sh-4.3#
Code: Select all
awk '{print "Uptime: "int($1/86400)" day(s), "int($1%86400/3600)" hours, "int(($1%3600)/60)" minutes, and "int($1%60)" seconds"}' /proc/uptime
BFN.Uptime: 0 day(s), 7 hours, 57 minutes, and 0 seconds
Code: Select all
awk '{print "Uptime: "int($1/86400)" day(s), "int($1%86400/3600)" hour(s), "int(($1%3600)/60)" minute(s), and "int($1%60)" second(s)"}' /proc/uptime
I am a linguist, after all!Uptime: 0 day(s), 8 hour(s), 9 minute(s), and 38 second(s)
Code: Select all
UPTIME=`awk '{print ""int($1/86400)" Tg, "int($1%86400/3600)" Std, "int(($1%3600)/60)" Min"}' /proc/uptime`
Code: Select all
#!/bin/bash
# /root/my-applications/bin/uptime.sh
####
case "${LANG:0:2}" in
en)Session=Session
Uptime="`awk '{print int($1/86400)" d, "int($1%86400/3600)" h, "int(($1%3600)/60)" m, "int($1%60)" s"}' /proc/uptime`" ;;
fr)Session=Session
Uptime="`awk '{print int($1/86400)" j, "int($1%86400/3600)" h, "int(($1%3600)/60)" m, "int($1%60)" s"}' /proc/uptime`" ;;
de)Session=Sitzung
Uptime="`awk '{print int($1/86400)" Tg, "int($1%86400/3600)" Std, "int(($1%3600)/60)" Min, "int($1%60)" Sek"}' /proc/uptime`" ;;
esac
clear
echo -e "\n\t\e[35m\e[4m$Session :\e[24m \e[33m$Uptime \e[0m\n"
Code: Select all
#!/bin/bash
# /root/my-applications/bin/uptime.sh
####
case "${LANG:0:2}" in en)Session=Session;Jr=d;Hre=h;Min=m;Sec=s ;;
fr)Session=Session;Jr=j;Hre=h;Min=m;Sec=s ;;
de)Session=Sitzung;Jr=Tg;Hre=Std;Min=Min;Sec=Sek ;;
esac
Uptime="`awk '{print int($1/86400)" '$Jr', "int($1%86400/3600)" '$Hre', "int(($1%3600)/60)" '$Min', "int($1%60)" '$Sec'"}' /proc/uptime`"
clear;echo -e "\n\t\e[35m\e[4m$Session :\e[24m \e[33m$Uptime \e[0m\n"
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
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"