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

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

#41 Post by musher0 »

MochiMoppel wrote:
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"
Source. ;)
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

#42 Post by MochiMoppel »


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

#43 Post by musher0 »

MochiMoppel wrote:Source :!:
:lol:
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

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

#44 Post by some1 »

musher0 wrote:
MochiMoppel wrote:
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"
Source. ;)
MochiMoppel wrote:Source :!:
musher0 wrote:
MochiMoppel wrote:Source :!:
:lol:

musher0:Why :?: :?: :?: :?:

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

#45 Post by musher0 »

Hi, some1.

"Why" what? Please get off your high horse.

Because that is my way of saying to everyone on this forum that this kind
of learning is fun.

Tipped off by a hint from a benevolent fellow "student" (RSH, not to name
him!), I find on the Web a source specifically for MochiMoppei's trick --
because he did not provide one --, and he replies referring me to the
entire "Book of Bash"!

There is humor in this. At least I can see it.

It is well proven that one learns better when one is in a relaxed, even
humorous, mood. If that angers the "teachers" on this forum... well, what
can I say? You guys go follow a course in Learning Modes at your local
Faculty of Education?

Although inactive ATM, I hold a valid Teacher's Certificate, you know, so
I know a thing or two about that. Another thing you have to know about
me: I truly despise "teachers" who base their teaching only on
haughtiness and authority.

(Edit, 10 min. later:)
... but maybe you belong to a culture where it is ok for teachers to base
their teaching on haughtiness and authority. In my culture, it is not, not
anymore.
(end of edit)


Put down a "student" for what he does not know, and you create a rebel,
perhaps an enemy. Prove that your teaching is based on your skills,
provide good sources, explain instead of commanding, display a friendly
attitude towards your student, and this student will become an admirer.

(Edit, 5 min. later:)
-- teach from the known to the unknown. On a forum like this, it is not
obvious, but try to deduce or find out in some way or other at which level
the student is.

If there are too many steps between what the student knows and the
target knowledge, the student won't learn anything. He'll send you to hell
because of the difficulty.

-- teach from context, teach from the problem. Although not ideal
because it creates "spotty knowledge", it is better than nothing, and it is
very useful in problem-solving.

It is also a very popular mode of learning on forums like this one, and
with people who find the "cram-your-brain" approach disgusting.

On the bright side, if this is done enough times, these "spots" of
knowledge will eventually merge into a full knowledge of the subject.

-- foster good Web and library research techniques. If the student cannot
learn from you because you displayed, e.g., the unsavory side of your
personality in your last lesson ;), he can probably learn from his research.
(end of edit)


As a result -- and this is no doubt the most important, incredible and
amazing thing -- : the student will learn!

In conclusion --
Food for thought, I hope. But who am I to "teach a teacher", eh?

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

Post Reply