Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Mon 19 Nov 2018, 09:11
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to get specific results from 'free -m' ?
Post new topic   Reply to topic View previous topic :: View next topic
Page 3 of 3 [45 Posts]   Goto page: Previous 1, 2, 3
Author Message
MochiMoppel


Joined: 26 Jan 2011
Posts: 1679
Location: Japan

PostPosted: Thu 08 Nov 2018, 01:37    Post subject:  

@some1: IMO int becomes unnecessary when you use the conversion specifier %d:
Code:
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?
Back to top
View user's profile Send private message 
ITSMERSH


Joined: 02 May 2018
Posts: 465

PostPosted: Thu 08 Nov 2018, 07:01    Post subject:  

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! Smile

_________________
RSH
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1679
Location: Japan

PostPosted: Thu 08 Nov 2018, 07:43    Post subject:  

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:
#!/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:
------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 Laughing

Note that example 1 will be much slower when the output of the uptime command is parsed to look similar to the other examples
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12986
Location: Gatineau (Qc), Canada

PostPosted: Thu 08 Nov 2018, 09:19    Post subject:  

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:
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! Laughing 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:
echo $Uptime
at the bottom of your script.
_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)

Last edited by musher0 on Thu 08 Nov 2018, 10:02; edited 1 time in total
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1679
Location: Japan

PostPosted: Thu 08 Nov 2018, 09:44    Post subject:  

OK, pure bash is much faster than awk:
Code:
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:
------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? Laughing


.

Last edited by MochiMoppel on Thu 08 Nov 2018, 10:31; edited 1 time in total
Back to top
View user's profile Send private message 
some1

Joined: 17 Jan 2013
Posts: 88

PostPosted: Thu 08 Nov 2018, 10:24    Post subject:  

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.
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12986
Location: Gatineau (Qc), Canada

PostPosted: Thu 08 Nov 2018, 14:31    Post subject:  

@mochiMoppei:

What is this trick?
Code:
set $(< /proc/uptime)
 UT=${1%.*}
Thanks in advance.
_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
ITSMERSH


Joined: 02 May 2018
Posts: 465

PostPosted: Thu 08 Nov 2018, 15:22    Post subject:  

Gives at least equal results:
Code:
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! Laughing Wink

_________________
RSH
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12986
Location: Gatineau (Qc), Canada

PostPosted: Thu 08 Nov 2018, 15:55    Post subject:  

Thanks Rainer for putting me on the track.
Code:
[~]>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
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1679
Location: Japan

PostPosted: Thu 08 Nov 2018, 23:26    Post subject:  

ITSMERSH wrote:
Though, strange code! Laughing Wink

You haven't seen strange code yet. Try this:
Code:
#!/bin/bash
[[ $(< /proc/uptime) =~ [0-9]* ]]
echo "Uptime $((BASH_REMATCH/86400)) days $((BASH_REMATCH%86400/3600)) hrs  $((BASH_REMATCH%3600/60)) min"
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12986
Location: Gatineau (Qc), Canada

PostPosted: Fri 09 Nov 2018, 06:28    Post subject:  

MochiMoppel wrote:
ITSMERSH wrote:
Though, strange code! Laughing Wink

You haven't seen strange code yet. Try this:
Code:
#!/bin/bash
[[ $(< /proc/uptime) =~ [0-9]* ]]
echo "Uptime $((BASH_REMATCH/86400)) days $((BASH_REMATCH%86400/3600)) hrs  $((BASH_REMATCH%3600/60)) min"

Source. Wink

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1679
Location: Japan

PostPosted: Fri 09 Nov 2018, 06:58    Post subject:  

Source Exclamation
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12986
Location: Gatineau (Qc), Canada

PostPosted: Fri 09 Nov 2018, 07:35    Post subject:  

MochiMoppel wrote:
Source Exclamation
Laughing
_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
some1

Joined: 17 Jan 2013
Posts: 88

PostPosted: Sat 10 Nov 2018, 09:36    Post subject:  

musher0 wrote:
MochiMoppel wrote:
ITSMERSH wrote:
Though, strange code! Laughing Wink

You haven't seen strange code yet. Try this:
Code:
#!/bin/bash
[[ $(< /proc/uptime) =~ [0-9]* ]]
echo "Uptime $((BASH_REMATCH/86400)) days $((BASH_REMATCH%86400/3600)) hrs  $((BASH_REMATCH%3600/60)) min"

Source. Wink


MochiMoppel wrote:
Source Exclamation


musher0 wrote:
MochiMoppel wrote:
Source Exclamation
Laughing



musher0:Why Question Question Question Question
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12986
Location: Gatineau (Qc), Canada

PostPosted: Sat 10 Nov 2018, 11:51    Post subject:  

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 Wink, 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
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 3 of 3 [45 Posts]   Goto page: Previous 1, 2, 3
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0776s ][ Queries: 12 (0.0172s) ][ GZIP on ]