In this thread I will share over time some of my coding goodies, nuggets, aliases,functions and scripts.

Lets start with the 3 ones that I use daily, the ones I use at times every hour or such, especially when RAM and CPU gets used up with my palemoon and 3 windows & 30 tabs minimum each, x versions of an image viewer, x open files in at least 2 mousepads, at times an gmplayer or mtpaint, or even gimp.
Oh, and for email alpine, and for coding either mcedit or geany. So you see, even a system with 3 1/2 GB RAM and Dual Core CPU gets into stress.

fx is my "free eXtended, aka in MB. With dividing ruler. And date/time stamp. For later / former comparisons.

sx is the same for swap, my "swap eXtended". (also with dividing ruler. And date/time stamp. For later / former comparisons.)
And since /dev/swaps or swapon -s not have an "MB option", I had to code it myself. I asked for some help on that coding over at http://www.linuxforums.org/ (I would have preferred stackoverflow, but it fails in registering when using palemoon, and since it has a large part of its site especially for coding.... still sadly they are not willing to fix that issue, they just tell me (use firefox or chrome for setting up the account"
Yeah thanks, real coder brilliance! (Or lazyness)

Did you guess it?
dx is my dfree eXtended, (and again, also with dividing ruler. And date/time stamp. For later / former comparisons.)
I know, once again a lame coded defaulting to 80 $COLUMNS so far, I was too lazy to code for a wider display than 80, are a less wide or "more narrow" display would screw up the info for sf, fx or dx almost always, and it not bothers me when the divider ruler is only 80 chars wide since none of the printed info usually is any wider.

Okay, dfree's output could be longer, depending on the mounted path length, but usually that's in the form of /mnt/sdXn and not any longer.

But maybe you realize that I not just run dfree -mT, but my dx also omits two lines I usually am never interested in.
Lets ask dfree -mT for comparison, shall we?

See what I mean?
I am not interested in devtmpfs or /mnt/live/run, usually these FS never get any more full than 1% regardless for full my local disk, or my RAM and swap is. And that's, as far as I understand it, the normal way since that disk space is used differently and not for use for standard files and such, like storing some more ISOs or MPEGs [1]. (Look it up what they do please if you want, its part of either how /dev works, or how a live Linux works (/mnt/live/run)

So, you maybe want to know how I coded that, aka dx omitting the unneeded 2 lines? [If your system should misbehave, please also do look at standard dfree -mT and see if, maybe, these two show strange full stats or such... just in case. ]

Do you, or don't you? Please do tell me if anyone is interested in my mini coding stuff at all.

Now I used the grep tweak -v "-v, --invert-match select non-matching lines"
Or in other words, omit all matching lines.

Just what we want here, looking if a process runs - by name - since no one is interested that the grep that searches for the running program will also listed, especially since it will always be listed.

Long time having posted in this thread; maybe I should just stop doing so, no replies from no one tells me no one is interested in my coding stuff anyway.
Still today I share something else.

Ever had to pause a video in the middle, cause RL appointments got in the way?

First I made me a hidden text file storing the info about e.g.

title.avi 0:40:35

telling me, I watched title.avi up to approx 0:40:xx and that I want to continue watching at 0:40:35.

So, I thought of a solution having the file name and the time to start watching coded into one script, and then just start the script. It asks via a GUI if the user really wants to watch "title.avi" from "time"?

In this example, the movie file is "/mnt/sda2/video/Filme/Sintel.2010.720p.mkv"
The time to start watching is set to "0:10:21"
The user is told that pressing "Yes" is Abspielen, German for: Play (the video file), while No is Abbrechen = Aborting (the playing of the file), the program then plays a sound file to give an additional info that the video was not played (file /usr/share/sounds/uget/notification.wav)

If course, for all to work as it should, the video must exist as coded into the script, and the notification.wav must be found at the given location. And you need to have mpv installed on your Porteus system.

(weiter is German for "continuation", "carrying on" or such.)

Finally this is how it looks like:
Clicking "Yes" opens the mpv window, playing the video at the told position:
(click for full view)

Aaand another one. Since I am too lazy to break down my scripts to post these here when no one seemingly appreciates these anyway, I will not change my code to make it more easy to adapt to the user's own needs.

So, in the above case, I use some centralized code for my script colour handling that is Linux variant independent. First, when I started with Linux it was via Suse Linux many years ago (Suse 5.1 when I recall right). I coded my stuff so that local files are all in /usr/local/bin because, the /usr/local/bin part says so. But then I encountered for some months only a live OS that had the whole hierarchy of /usr/local in its read-only live system, so I had to think of a solution which is independent of the actual files to load into the scripts and came up with having the file name in a system wide variable. Usually the file is still in /usr/local/bin/, but in case I work with the above mentioned live thingy again or come across yet another one that blocks the write access to the hierarchy of /usr/local, I can alter the path to the needed dependency files accordingly.

Why did I code suspend-s3b2 and what does it stand for?
s3 stand for 3 seconds pause, the grace time I give the user (aka usually me, myself an I ) time to abort the suspend if the need be, and b2 stands for beep2, my somehow volume reduced variant of my beep command.

And what does it do? It shows free -m with an added time and date ruler line prior suspend and after suspend, it calculates the time while the system was in suspend into HH:MM:SS and it prints the start and end of suspend into /var/log/messages as well. What you see above in not all that the script does, most of what gets written into /var/log/messages is missing.

And why did I code it? It was during the rc phases orf Porteus 4.0 and some of my machines had severe issues with suspend, taking u8p to 45or 50 seconds to go into suspend, so I wanted a script that logs the suspend in a way that it can be easily found browsing /var/log/messages; you might have guessed it, its what the "●●●●●●" are for.

So, all in all, no coding goodies this time, just an explanation what the next one will be about, the suspend-s3b2 command, and why I have to first give you the code to the colour handling and sound handling for my scripts.

Since I don't want my shy reading but never commenting fans not going dry till I start with the pre-codes for the suspend-s3b2 script. lets have an one liner, shall we.

The question being "Hey, Rava! I know of a list of programs that use either most of my CPU or Memory after my system runs for a while. I would like to save usage info of these programs into a log so that I can review it later"

Now, lets put the log and regular checking stuff aside for now, lets just concentrate on the listing of %MEM and %CPU part of a list of preselected "known to be potential critical" programs.

Lets presume the programs are the following, separated by "|" divider.

'palemoon|Xorg|pulseaudio|mpv|spacefm|viewnior|interlink'

So, we are running X, using Xorg, most probably we are running Openbox cause that uses SpaceFM as file manager, and we use mpv to watch videos or listen to music, and the audio is maintained by pulseaudio. The browser is palemoon (a fork of Firefox) and the email client is interlink, the slim and efficient fork of Mozilla Thunderbird. And viewnior is used for watching and browsing images.

Now we would need a header line giving us date and time, or else a log would make not much sense, as it would make a listing like above, say, every 10 minutes, but not tell us when palemoon used 300% CPU or any such high %CPU or %MEM usage.

We also might want to add the x most CPU using programs (aside from the ones we watch above since there could be another unsuspecting culprit hogging most of the %CPU or %MEM.), and also the memory and swap usage, like so (listing the 4 most CPU using programs, hence its name):

CAVE!
The parts with the true escape sequences as mentioned in the comments after the $bld entry could NOT be copied into a [code] area of this forum since you cannot copy the escape sequence that contains the DEC 0027 (HEX value 0x01B) string.
If someone wants to have these, please reply and I can see what can be done to give you the wanted / needed info.

An escape sequence is a combination of characters that has a meaning other than the literal characters contained therein, and is marked by one or more preceding (and possibly terminating) characters.

When directed this series of characters is used to change the state of computers and their attached peripheral devices, rather than to be displayed or printed as regular data bytes would be, these are also known as control sequences, reflecting their use in device control, beginning with the Control Sequence Initiator - originally the "escape character" ASCII code - character 27 (decimal) - often written "Esc" on keycaps.

With the introduction of ANSI terminals most escape sequences began with the two characters "ESC" then "[" or a specially-allocated CSI character with a code 155 (decimal).

Please know that using escape sequences in an erroneous way could make your terminal unusable [or worse]. You have been warned!

I already used some of them, but I think, nowadays learning linux script language is absolut out.......

Why do you think that?

With some scripts you can do what a PC or whatever computer is meant to be: you give him enough work that he does stuff that would take you several minutes just to give all into some GUI.

Sure, first you have to learn basics, and then probably some hours on one single complex script, but after that you run the script and the PC works several tasks all by itself, the way a PC is meant to be.

Aaaand finally a new entry, since I already posted that is another thread.

I use an alias to look for programs that use most RAM or use most CPU. If you want to remove some of the ones I look into - or add some - you have to alter the entries. My current standard is this for Openbox or XFCE and palemoon browser and interlink email client, and some other programs known for using either much RAM or CPU, like mpv, viewnior and gimp. Xorg is always good in using usually some RAM. Here is the one-liner:

and the t script relies on the systemwide exported variable <"ECHO_COLORS" that directs to the file that defines the colour and highlighting short names I use for colour highlighting.
But that would be too much to explain how to set up. It works just fine without, you just get no colours at all instead.

Oh and I was too lazy to update the t script. It might complain that it cannot read $COLUMNS.

I could use tput or tset or whatever it was instead, sorry I forgot when I saw the solution to that issue and not updated all my scripts back then that rely on knowing $COLUMNS

So there, flawed code presented by yours truly. Feel free to look up the solution and post the info about e.g. reading COLUMNS in a script without the need to export the variable.

Another way in looking what hogs the CPU is just asking top and only look for the 4 most CPU using processes, or for the 9 most CPU using processes.
Why 4 and 9 you might ask. Cause with the header info line that makes 5 or 10 lines of output.

Here is the code, by now you should be able to put the info together and create the bash functions by yourself, and by now you also should know where to put them to have them on a permanent basis. Tip here: it is the same file that also stores shell aliases.

Please be aware that unlike my "t" script above, the top4x and top9x is too stupid to care about the width of the terminal or virtual console, it just assumes it is 80. It would look ugly when it is less, and not perfect when it is more.

____________
P.S. Nope, it won't work as an alias. For reasons I not really get, the date and time info displayed is not updated when I first created top4x and top9x as aliases, it only works as it should when its a function.
Maybe one of you know why that is. (Or maybe "was", could be due to a bug in bash that by now got fixed.)

Nope, a comment can be used. The issue was: I edited and saved the script while it was already test-running. Now, bash has the unfortunate feature to usually read the script source from disk just when it is needed, by standard never in advance. And therefore, when you edit a running script, and save it, the characters and lines tend to shift. And the results can be unpredictable.

In this case, I deleted a character in the Xdialog area, and saved the script.
Then I pressed one of the --yesno buttons of the running script dialogue to exit it, and the above error appeared.
Why did it appear? By deleting a mere single character in the area already executed and read by bash, the whole data to be read afterwards shifted one character to the left.
And

, and the script was thinking "f" must be an executable command (which it is not) or an already defined function or alias (which it is also not), and "then" being one of the many restricted words you cannot use but in a sh or bash way (either in a script on in the CLI itself) was creating the above error.