How to get specific results from 'free -m' ?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
ITSMERSH

#21 Post by ITSMERSH »

Code: Select all

sh-4.3# cat /proc/uptime
199514.08 312966.41
sh-4.3#
This result/output is nothing I can use in any manner... :wink:

Is there a special way to read/format the results from /proc/uptime?

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#22 Post by MochiMoppel »

ITSMERSH wrote:

Code: Select all

sh-4.3# cat /proc/uptime
199514.08 312966.41
sh-4.3#
This result/output is nothing I can use in any manner... :wink:
Well, it tells me that your computer is now up for 2 days 07 hours and 25 min :wink:

First you should clarify how your output should look like. Total of hours?

ITSMERSH

#23 Post by ITSMERSH »

Ok, so I assume this output is something like seconds/milliseconds, no?

Days, Hours and Minutes would be cool, though I take what I can get! :wink: :lol:

Minutes only would also be interesting...

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#24 Post by musher0 »

Hi guys.

(Hehe) :D

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
I deserve only limited credit for this! It's a variant of this.

It should give you something like:
Uptime: 0 day(s), 7 hours, 57 minutes, and 0 seconds
BFN.
Last edited by musher0 on Wed 07 Nov 2018, 12:43, edited 1 time in total.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#25 Post by musher0 »

Hello again, people!

Here is a more grammatically correct version, to anticipate 0 or 1 for all details:

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
The result will resemble this line:
Uptime: 0 day(s), 8 hour(s), 9 minute(s), and 38 second(s)
I am a linguist, after all! ;)

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

ITSMERSH

#26 Post by ITSMERSH »

Modified this a little for my output needs.

Code: Select all

UPTIME=`awk '{print ""int($1/86400)" Tg, "int($1%86400/3600)" Std, "int(($1%3600)/60)" Min"}' /proc/uptime`
I need to make a reboot, to compare the output from the beginning of a machine running just a few minutes until it runs for days.

Though, I'm busy. So rebooting could take some days.

Will return the results here...

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#27 Post by musher0 »

Trilingual (EN, FR, DE) "beautification":

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"
BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

ITSMERSH

#28 Post by ITSMERSH »

Nice.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#29 Post by musher0 »

The same, in 442 bytes. (hehe)

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"
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

some1
Posts: 117
Joined: Thu 17 Jan 2013, 11:07

#30 Post by some1 »

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)"

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#31 Post by MochiMoppel »

@some1: IMO int becomes unnecessary when you use the conversion specifier %d:

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 see what's wrong with awk. Seems to be the right tool for this job. Pulls out the first field, can format and can calculate. What would be faster?

ITSMERSH

#32 Post by ITSMERSH »

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.
I don't think so.

Even if I don't know how fast awk is doing this job, it is fast enough for my purposes.

The results of uptime are displayed in a GUI of a program of mine. It is examined just once, so it is NOT regularly/sequentially updated during the use of the program.

This program takes about 14 seconds to appear on the screen, which should be enough for awk to do the job. :wink:

@all

Thanks for all the examples! :)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#33 Post by MochiMoppel »

ITSMERSH wrote:Even if I don't know how fast awk is doing this job, it is fast enough for my purposes.
Don't worry, awk is very fast - probably faster than anything else.
Here is a script with some solutions and their processing time:
1) update command
2) date command with limited functionality (only correct if hrs < 24)
3) date command with days display
4) awk

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
A typical output ot this script:

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
And the winner is?
Don't know yet. Now it's some1's turn :lol:

Note that example 1 will be much slower when the output of the uptime command is parsed to look similar to the other examples

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#34 Post by musher0 »

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)"
Hi some1.

I knew you would criticize me!!! But what the h...! Passing bash variables
to awk the way I do is documented. But for you, it's heresy!

What? You're putting English as the universal default language, under *) ?
No-no-no-no. The nerve you have! The universal language is Quechua,
you know that! No ifs or buts about it! :lol: The Inca Emperor will hear
of this...

Joke aside, enough colonization, the en) stays. FYI, the case...esac
form does not need a catch-all to work. Only if there is no other way
around it, or if useful or absolutely needed.

As to the speed of the awk language, it's considered the fastest. The
mawk variant in particular is even the fastest of the awks.

Yes, I will learn to use the printf command when I have a little time.
Thank you for reminding me.

BFN.
~~~~~~~~~
P.S. You forgot

Code: Select all

echo $Uptime
at the bottom of your script.
Last edited by musher0 on Thu 08 Nov 2018, 14:02, edited 1 time in total.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#35 Post by MochiMoppel »

OK, pure bash is much faster than awk:

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"
}
Sample output:

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
And the winner is .... echo? :lol:


.
Last edited by MochiMoppel on Thu 08 Nov 2018, 14:31, edited 1 time in total.

some1
Posts: 117
Joined: Thu 17 Jan 2013, 11:07

#36 Post by some1 »

So the wolfpack are snapping at innocent lurkers.

MochiMoppel:I am busy - but will be back.
Not for a pissing contest - but for kicks and code.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#37 Post by musher0 »

@mochiMoppei:

What is this trick?

Code: Select all

set $(< /proc/uptime) 
 UT=${1%.*}
Thanks in advance.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

ITSMERSH

#38 Post by ITSMERSH »

Gives at least equal results:

Code: Select all

set $(cat /proc/uptime)
UT=${1%.*}
echo $UT
Not that I'm an expert, though to me it seems

set $(< /proc/uptime)

is setting the variable $1 (which is usually an empty string, if no file was submitted to a script or function) for a use and puts the output of

< /proc/uptime

or

cat /proc/uptime

into this variable.

At least that how I understand it after reading and testing by script.

Though, strange code! :lol: :wink:

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#39 Post by musher0 »

Thanks Rainer for putting me on the track.

Code: Select all

[~]>cat /proc/uptime
89377.92 139265.34

[~]>set $(< /proc/uptime);UT=${1%.*};echo $UT
89563
set $(< /proc/uptime) -- runs it "in the abstract", so to speak;
$1 -- is the usual 1st position of any output;
${1%.*} -- is whatever comes before the dot in this output.

And then you do math on ${1%.*}, now known as $UT.

Clever. Also, that brings out bash's power in handling variables... Wow.

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#40 Post by MochiMoppel »

ITSMERSH wrote:Though, strange code! :lol: :wink:
You haven't seen strange code yet. Try this:

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"

Post Reply