RPI apt-get issues buster/main old stable

I had issues with buster and apt keeps suggesting that I must mark it as old stable, but what I have found as a fix is to delete the cached lists.

cd /var/lib/apt/lists
sudo rm -r *

then do a sudo apt-get update that should resolve it.

after this you can do a >sudo apt-get dist-upgrade

NPM Cheat Sheet

To check for outdated and update

npm outdated
npm update
npm install

To Update all packages

npm install -g npm-check-updates
and then
ncu -u

List globally installed packages.

npm list -g –depth=0

Check packages not used

npm install -g depcheck
depcheck

npm uninstall

Delphi Key Value Pairs

We get so used to the data structures in more modern languages that its frustrating using old Delphi 7 but I will show you how you can at least create a list map like structure using TStringList.

 var
  KeyValuePairsSample: TStringList;
  iIndex : integer;
  sname, svalue, svalueindex: string;
begin
  KeyValuePairsSample := TStringList.Create;
  KeyValuePairsSample.Clear;
  KeyValuePairsSample.NameValueSeparator := '=';
    KeyValuePairsSample.Add('ZERO=0'); // name=value pair //
  KeyValuePairsSample.Add('ONE=1');
  KeyValuePairsSample.Add('TWO=2');
  KeyValuePairsSample.Add('THREE=3');
  KeyValuePairsSample.Add('FOUR=4');
  KeyValuePairsSample.Add('FIVE=5');
  Memo1.Clear;
  Memo1.Lines.Add(KeyValuePairsSample.Names[1]);
  Memo1.Lines.Add(KeyValuePairsSample.Values['THREE']);
  Memo1.Lines.Add(KeyValuePairsSample.Names[0]);

  Memo1.Lines.Add(inttostr(KeyValuePairsSample.IndexOfName('THREE')));
 Memo1.Lines.Add(KeyValuePairsSample.ValueFromIndex[KeyValuePairsSample.IndexOfName('THREE')]);
  KeyValuePairsSample.free;
end;


Output:
ONE
3
ZERO
3
3

Best way to start a process in background and logout on Linux

I very often have to start a process on Linux and logout leaving it running. I used to do a nohub.

nohub /path/to/some/pogram &

This does work but seeing the output of it you probably have to pipe the output to file like

nohub /path/to/some/pogram > /path/to/output &

But i will show you a better way SCREEN to the rescue to install:

sudo apt-get install screen

then simply run screen before you run the process and to disconnect the terminal and leave it running do a CRTL-A D which will take you back to the calling shell and you can log out leaving it running. You can do this with multiple processes.

to access it again type

screen -r

There are several suitable screens on:
	6078.your-server-detail	(SOMEDATE)	(Detached)
	4249.your-server-detail	(SOMEDATE)	(Detached)

and to reconnect do

screen -r 4249

This is ideal if you have to start some docker processes on remote servers.

Docker Short Cheat Sheet

To stop all docker containers:

$ docker stop $(docker ps -a -q)

Then to remove all docker containers:

$ docker rm $(docker ps -a -q)

To remove all docker images:

$ docker rmi $(docker images -q)

Stop specific docker container:

docker ps -a
then copy container is ie: c38ba5c03ab7
docker stop c38ba5c03ab7

Run bash shell in docker image:

$ docker exec -it RUNNING_CONTAINER_NAME /bin/bash

Javascript Object to Array and visa versa

const obj = {"1":"5","2":7,"3":9,"4":155,"6":"189","7":587,"8":"455","9":"89","10":"80","12":"887.6","13":89,"14":9}
const objToArr = Object.keys(obj).map(function(key) {
  return [key, Number(obj[key]) ];
});
console.log(objToArr)
/*
[ [ '1', 5 ],
  [ '2', 7 ],
  [ '3', 9 ],
  [ '4', 155 ],
  [ '6', 189 ],
  [ '7', 587 ],
  [ '8', 455 ],
  [ '9', 89 ],
  [ '10', 80 ],
  [ '12', 887.6 ],
  [ '13', 89 ],
  [ '14', 9 ] ]
*/


let resultObj = objToArr.reduce(function(result, item, index, array) {
   Object.assign(result, { [ item [ 0 ] ]:item[1]} )
  return result;
}, {}) //watch out the empty {}, which is passed as "result"
console.log(resultObj)
/*
{ '1': 5,
  '2': 7,
  '3': 9,
  '4': 155,
  '6': 189,
  '7': 587,
  '8': 455,
  '9': 89,
  '10': 80,
  '12': 887.6,
  '13': 89,
  '14': 9 }
*/

resultObj2 = {...objToArr};
console.log(resultObj2)
/*
{ '0': [ '1', 5 ],
  '1': [ '2', 7 ],
  '2': [ '3', 9 ],
  '3': [ '4', 155 ],
  '4': [ '6', 189 ],
  '5': [ '7', 587 ],
  '6': [ '8', 455 ],
  '7': [ '9', 89 ],
  '8': [ '10', 80 ],
  '9': [ '12', 887.6 ],
  '10': [ '13', 89 ],
  '11': [ '14', 9 ] }
*/





Linux File Permissions

Let’s take a look at our ls -l output and look the first column of the listing:

touch myfile 
ls -l myfile
-rw-rw-r-- 1 hano hano 0 Apr 16 08:57 myfile
sudo chown hano.root myfile
ls -l myfile 
-rw-rw-r-- 1 hano root 0 Apr 16 08:57 myfile

The you have 3 access value bits in this case rw- for the owner in this case hano then rw- for the group which corresponds to the root group and then all other users r–.

rRead Access
wWrite Access
xExecute Access

To change the access use chmod (Change Mod)

chmod 754 myfile
ls -l myfile 
-rwxr-xr-- 1 hano root 0 Apr 16 08:57 myfile
ModeDecimal
rwx7
rw-6
r-w5
r–4
-wx3
-w-2
–x1
0

If you know binary you will notice that it is the decimal value of binary digits 001 = 1 010 = 2 100 = 4 so combinations will then be 101 = 4 + 1 = 5 corresponding to r-w

ES6 Arrow functions and correct using of bind(this)

function Desciples(firstname, attributes) {
  this.firstname = firstname;
  this.attributes = attributes;
  this.listAttributes = function() {
    return this.attributes.map(function (attrib)  {
      console.log(this.firstname + " did " + attrib);
    });
  };
}

let desciple1 = new Desciples("John", [
  "Author of Gospel of John ",
  "Author of Reveleations",
  "Desicple that Jesus loved"
]);

desciple1.listAttributes();

retuns

"undefined did Author of Gospel of John "
"undefined did Author of Reveleations"
"undefined did Desicple that Jesus loved"

But if we change it to

this.listAttributes = function() {
    return this.attributes.map(attrib => {
      console.log(this.firstname + " did " + attrib);
    });


"John did Author of Gospel of John "
"John did Author of Reveleations"
"John did Desicple that Jesus loved"

We can also create local variable to the function and assign this to it

 this.listAttributes = function() {
    self = this;
    return this.attributes.map(function (attrib)  {
      console.log(self.firstname + " did " + attrib);
    });
  };

We can also use the BIND on the function like:

  this.listAttributes = function() {
    return this.attributes.map(function (attrib)  {
      console.log(this.firstname + " did " + attrib);
    }.bind(this));
  };

Or the MAP has a second paramenter and we can pass this there that will also work

  this.listAttributes = function() {
    return this.attributes.map(function (attrib)  {
      console.log(this.firstname + " did " + attrib);
    },this);
  };