Ravas coding goodies

For discussions about programming and projects not necessarily associated with Porteus.
Post Reply
User avatar
Rava
Contributor
Contributor
Posts: 2225
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Ravas coding goodies

Post#1 by Rava » 16 Apr 2016, 16:40

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.


So, here my top 3: aliases/functions:

top4;fx;sx

Code: Select all

rava@porteus:/mnt/DL$ top4;fx;sx
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
 6728 rava      20   0 19748 2476 2032 R    4  0.1   0:00.02 top                
13074 root      20   0  596m 171m  20m S    2  5.1   1284:17 X                  
29350 rava      20   0 4209m 2.1g  10m S    2 64.3   1056:19 palemoon           
    1 root      20   0   240   16    0 S    0  0.0   0:18.38 init               
16.04.2016 17:15:42 ____________________________________________________________
             total       used       free     shared    buffers     cached
Mem:          3393       3237        155          0         19        338
-/+ buffers/cache:       2879        513
Swap:         6326       2063       4263
16.04.2016 17:15:43 ____________________________________________________________
Filename				Type		Size	Used	Priority
/dev/zram0                              partition       678     0       1000    
/dev/sda1                               partition       2047    0       100     
/mnt/sda3/pagefile.sys                  file            3599    2063    -1   
What do they do?

top4 gives me the topmost 4 CPU using processed (utilizing, of course, top)

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)

Code: Select all

rava@porteus:/mnt/DL$ type top4 fx sx
top4 is aliased to `top -bn 1|head -n 11|tail -n 5'
fx is aliased to `echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;free -m'
sx is a function
sx () 
{ 
    echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;
    { 
        read firstLine;
        echo "$firstLine";
        while read f t s u p; do
            let "s2 = $s / 1024";
            let "u2 = $u / 1024";
            printf '%-40s%-16s%-8s%-8s%-8s\n' $f $t $s2 $u2 $p;
        done
    } < /proc/swaps
}
And when we are at sx and fx, why not give you dx as well?


Maybe someone guesses what dx could be, seen what sx and fx do?

Code: Select all

# dx
16.04.2016 17:34:42 ____________________________________________________________
Filesystem     Type     1M-blocks  Used Available Use% Mounted on
aufs           aufs          1528   906       622  60% /
/dev/sda2      ext3          3904  3858        45  99% /mnt/sda2
/dev/sda3      ntfs         32022 31483       539  99% /mnt/sda3
/dev/loop18    ext2            25    22         3  89% /Lsfind
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?

Code: Select all

$ df -mT
Filesystem     Type     1M-blocks  Used Available Use% Mounted on
aufs           aufs          1528   908       620  60% /
devtmpfs       devtmpfs      1696     1      1696   1% /dev
/dev/sda2      ext3          3904  3858        45  99% /mnt/sda2
/dev/sda3      ntfs         32022 31483       539  99% /mnt/sda3
/mnt/live/run  none          1697     3      1695   1% /run
/dev/loop18    ext2            25    22         3  89% /Lsfind
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.

Thanks for your time!Image
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 2225
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Re: Ravas coding goodies

Post#2 by Rava » 20 Apr 2016, 06:31

It's neat how easy it is to look up if a certain program or script is currently running:

Code: Select all

# ps ax|grep swapx
30161 pts/0    S+     0:00 /bin/sh /usr/local/bin/swapx
30166 tty1     S+     0:00 grep --color=auto swapx
What annoys me is that the grep that looks for the name is always listed as well. Is there are way to undo that? Sure, we are at a *IX here, and a GNU *IX to boot...

Killed the swapx script, but that calls come programs successively, the first one being mcedit.
So, mcedit still runs:

Code: Select all

ps ax|grep mcedit|grep -v grep
30162 pts/0    S      0:00 mcedit /etc/fstab
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.

Code: Select all

# ps ax|grep mcedit|grep -v grep
#
Seems I killed the mcedit now as well...


Using the same trick with the already killed swapx script, we get a much neater result: when the program or script is not currently running, we get no results.

Now sure we can put that into a script itself, calling it e.g. grepps or psgrep (or even greps) but I leave that to you.

Cave!
Searching for "grep $psname" can get you into trouble, when $psname being the program or script we look up.

Some users, so me, use an alias (or even a function) to expand grep's usual behaviour.
E.g. on my Porteus "grep whatever" gets expanded to "grep --color=auto whatever"

So, omitting it via

Code: Select all

ps |grep whatever |grep whatever
would still
print "grep --color=auto whatever" since that was what was executed here.
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 2225
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Resuming watching a movie…

Post#3 by Rava » 06 Jan 2019, 01:31

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. :D

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

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

Code: Select all

#!/bin/sh
# TODO
# read a .weiter kind of setupfile that defines the $spiele and $to instead of hardcoding this info in each and every .mein-weiter-abspielen--FILMTITEL.sh
spiele="/mnt/sda2/video/Filme/Sintel.2010.720p.mkv"
to="0:03:00"
Xdialog --title ".weiter mpv"  --fixed-font --under-mouse --yesno "Spiele:\n${spiele}\nYes:Abspielen, No:Abbrechen\n\n( Abspielen ab $to )" 0 0
if [ $? -eq 0 ]; then mpv --player-operation-mode=pseudo-gui --video-sync=display-resample --no-osd-bar --audio-file-auto=fuzzy --start $to "$spiele" ; else
	wavfile=/usr/share/sounds/uget/notification.wav
	aplay "$wavfile" 2>/dev/null
fi
exit
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:
Image
Clicking "Yes" opens the mpv window, playing the video at the told position:
Image
(click for full view)
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 2225
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Re: Ravas coding goodies

Post#4 by Rava » 09 Jan 2019, 08:29

Code: Select all

# suspend-s3b2
suspend-s3b2 V0.1
(sleep 3s; beep2)
09.01.2019 07:27:34 ____________________________________________________________
              total        used        free      shared  buff/cache   available
Mem:           3880        2402         490         217         986         859
Swap:          3018         385        2632
●●●●● pm-suspend took 01:37:30 … ●●●●●●●●●●●●●●●
09.01.2019 09:05:05 ____________________________________________________________
              total        used        free      shared  buff/cache   available
Mem:           3880        2449         406         217        1023         812
Swap:          3018         383        2634
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 :D ) 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. :)
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 2225
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Re: Ravas coding goodies

Post#5 by Rava » 10 Jan 2019, 13:36

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.

Here is the one liner that uses top and grep to get the info we need:

Code: Select all

$ echo '  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND';/usr/bin/top -bn 1 | grep -E 'palemoon|Xorg|pulseaudio|mpv|spacefm|viewnior|interlink'
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 1459 root      20   0  508748 186104  32580 S   6.2   4.7  73:11.82 Xorg
 9542 guest     20   0 3181392 873072 101128 S   6.2  22.0 176:59.98 palemoon
 1498 guest     20   0 1396352  98532  24588 S   0.0   2.5   3:42.15 spacefm
 1574 guest     20   0 1190044   8628   6132 S   0.0   0.2  12:29.78 pulseaudio
 4039 guest     20   0 2069084  69096  19624 S   0.0   1.7  10:50.67 mpv
15948 guest     20   0  307896  77496  34096 S   0.0   2.0   0:11.98 viewnior
18213 guest     20   0 2152328 336100  30188 S   0.0   8.5  38:28.19 mpv
32075 guest     20   0 2059764 242792  47452 S   0.0   6.1   8:23.75 interlink
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):

Code: Select all

$ top4x;fx;sx
10.01.2019 13:51:47 ____________________________________________________________
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 1459 root      20   0  509644 185056  31532 S  12.5   4.7  73:24.64 Xorg
 9542 guest     20   0 3178320 864592 101096 R   6.2  21.8 179:21.08 palemoon
25132 guest     20   0    6560   2992   2416 R   6.2   0.1   0:00.02 top
    1 root      20   0     240      0      0 S   0.0   0.0   0:02.40 init
10.01.2019 13:51:47 ____________________________________________________________
              total        used        free      shared  buff/cache   available
Mem:           3880        2319         870          57         690        1101
Swap:          3018         665        2352
10.01.2019 13:51:47 ____________________________________________________________
Filename				Type		Size	Used	Priority
/dev/zram0                              partition       970     665     100     
/mnt/sda6/linuxswap                     file            2047    0       -2    

As you can see, the functions or alias of top4x, fx and sx already have the date and time stamp divider line we would need for our above log suspicious programs thingy.

But since one of these is part of suspend-s3b2, we will get into top4x, fx and sx later. (And also add top9x and dx sometimes.)
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 2225
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Ravas coding goodies

Post#6 by Rava » 01 Apr 2019, 08:46

So, as I mentioned the script here Porteus linux on the net or on the waves of the web (Post by Rava #70563) I give you the script as well.

Please be aware that I hardcoded the quality 80% and the resize factor or 33% and also the part of the new filenames that gives the 33% resize info "033" into the script.

So, when someone of the shy readers want to alter the script, e.g. change the resize factor to 20% or 40%, please remember to also change the part of the new filename to "020" or "040".

Requirement:
You need to have graphicsmagick

I converted graphicsmagick-1.3.17-x86_64-1cf into a module and are quite happy with that version.

Cave! My Script is too lazy to check if you have "gm" installed, it would result in errors when you run it without graphicsmagick binary and its needed dependencies in your system.

The scripts needs to be called like so:
[script name] name.jpg name2.jpg name3.jpg
or
[script name] *.JPG

Code: Select all

#!/bin/sh
bld="\033[1m"   # bold  <------>^[[0;1m <- as true  Escape-Sequence works even without "echo -e"
yel="\033[33m"  # yellow<------>^[[0;33m
off="\033[m"    # off ^[[0;m
# ${string:position:length}
# Extracts $length characters of substring from $string at $position.
# gm convert -quality 93 -geometry 33% input.jpg output_033.jpg
while [ $# -gt 0 ]; do
{
    echo -e $bld${yel}gm convert -verbose -quality 80 -geometry 33% "$1" "${1:0:8}_033.jpg"$off
    gm convert -verbose -quality 80 -geometry 33% "$1" "${1:0:8}_033.jpg"
    shift
}
done
exit 0
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.

For more info on that see:
Escape sequence: Use in Linux and Unix displays
ANSI escape code: Unix-like systems
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!
Cheers!
Yours Rava

kultex
White ninja
White ninja
Posts: 26
Joined: 30 Dec 2015, 12:11
Distribution: Porteus 4.0 XFCE
Location: Austria

Ravas coding goodies

Post#7 by kultex » 15 May 2019, 12:04

here is somebody, who is interested in your scripts.....

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

keep going....

User avatar
Rava
Contributor
Contributor
Posts: 2225
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Ravas coding goodies

Post#8 by Rava » 19 May 2019, 17:51

kultex wrote:
15 May 2019, 12:04
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. :)
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 2225
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Ravas coding goodies

Post#9 by Rava » 21 May 2019, 20:50

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:

Code: Select all

top -bn 1 |grep -E 'palemoon|Xorg|pulseaudio|mpv|spacefm|viewnior|interlink|gimp|Thunar'
You have to alter the entries to make it work best for you. :)

When you want to have it as a script, use this (I named it topgrep :) )

Code: Select all

#!/bin/sh
#topgrep V0.3
topgrep='palemoon|Xorg|pulseaudio|mpv|spacefm|viewnior|interlink|gimp|Thunar'
echo 'top -bn 1 |grep -E '\'$topgrep\'
t
echo '  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND';/usr/bin/top -bn 1 | grep --color=auto -E $topgrep
The line "echo 'top -bn 1 |grep -E '\'$topgrep\'" is not really needed but I like the idea seeing which programs are looked into.


CAVE!
this script uses another script of mine named "t" , short for Trenner or Trennlinie, that is German for "separation line".
Here is the code:

Code: Select all

#!/bin/bash
VERSION="0.8"
MYNAME="t" # aka "trennlinie.sh" 

################################################################################
## functions
################################################################################

################################################################################
## Global Variables
################################################################################

################################################################################
## main
################################################################################
test ! "$ECHO_COLORS"x = "x" && test -f $ECHO_COLORS && . $ECHO_COLORS

# capture width since it could change while proggi is running and we don't want to confuse it.
# most probably you must tell your bash in /etc/profile and ~/bashrc or other ~/ to
#export COLUMNS
width=$COLUMNS
if [ -z $width ]; then
    echo "$MYNAME V$VERSION : Error: Could not read \$COLUMNS. You must export this variable.
Abort!"
    exit 1
fi

declare -i mywidth=0
char="_"
mychar=""
mycolour=$yel$bld	# uses definition found in $ECHO_COLORS file

# some very common width we declare by hand to speed things up a bit
# should be more than 80 & 128 ^-^

# 141 and 142: possible full screen width on W1942T / Terminal 0.2.8 / Monospace 12 pt
# 140 : also W1942T / Terminal 0.2.8 / Monospace 12 pt, but "F7"
# 155: notebook with std monospace 12pt 1152px 
case $width in
    80) mychar="________________________________________________________________________________" ;;
    101) mychar="_____________________________________________________________________________________________________" ;;
    124) mychar="____________________________________________________________________________________________________________________________" ;;
    128) mychar="________________________________________________________________________________________________________________________________" ;;
    140) mychar="____________________________________________________________________________________________________________________________________________" ;;
    141) mychar="_____________________________________________________________________________________________________________________________________________" ;;
    142) mychar="______________________________________________________________________________________________________________________________________________" ;;
    155) mychar="___________________________________________________________________________________________________________________________________________________________" ;;
    *) { 
	if test $width -gt 80 ; then
	    mychar="________________________________________________________________________________"
	    mywidth=80
	    while [ "$width"x != "$mywidth"x ]; do
        	{
		mychar=$mychar$char
    		mywidth=`expr $mywidth + 1`
    		}
    	    done
	else
	    while [ "$width"x != "$mywidth"x ]; do
        	{
		mychar=$mychar$char
    		mywidth=`expr $mywidth + 1`
    		}
    	    done
    	fi
    	} ;;
esac

echo -e ${mycolour}$mychar$off

exit 0
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. :D

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.
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 2225
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Ravas coding goodies

Post#10 by Rava » 22 May 2019, 01:05

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.

Code: Select all

guest@porteus:~$ type top4x
top4x is a function
top4x () 
{ 
    echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;
    /usr/bin/top -bn 1 | head -n 11 | tail -n 5
}
guest@porteus:~$ type top9x
top9x is a function
top9x () 
{ 
    echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;
    /usr/bin/top -bn 1 | head -n 16 | tail -n 10
}
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.

And here is what it looks like:

Code: Select all

guest@porteus:/mnt/DL$ top4x
21.05.2019 23:58:35 ____________________________________________________________
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
26495 root      20   0 1972152 184908   2892 S 747.1   4.7   3:10.27 mksquashfs
24337 guest     20   0 3902432 710380  65496 S  11.8  17.9  97:04.55 palemoon
26529 guest     20   0    6572   2964   2376 R  11.8   0.1   0:00.03 top
 2475 root      20   0  467928 204096  29048 S   5.9   5.1  72:22.56 Xorg
:D

____________
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.)
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 2225
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Ravas coding goodies

Post#11 by Rava » 05 Jul 2019, 20:47

Funny error while script coding - I document it here so that you will not be confused when you happen to run into a similar issue.

This error appeared more than once while I edited an Xdialog script used for reminding me of some TODO stuff:

Code: Select all

/2/tmp/INFO.sh: line 3: syntax error near unexpected token `then'
/2/tmp/INFO.sh: line 3: `f [ $? -ne 0 ]; then   # echo '-eq 0 WHATEVS' ; else'
What was the reason?

Is it disallowed using a comment after an "if test WHATEVER; then"?

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

Code: Select all

if
transformed into

Code: Select all

f
, 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.

HTH. Somehow. :D
Cheers!
Yours Rava

Post Reply