K4S

            ..stardin kiiresti*1:

            Saan teha ilma karkudeta *2      -- tee megamahtu
            Olen võimeline teha karkudega    -- tõesta
            Ma tean, kuidas karku valmistada -- see on kõrgeim hinnang.

            *1) AiHel, "Stardin", Eeden, 2019 (vene keeles)

            *2) kargud - kõik see, mida te ei pea iseenda osaks 
            ja keegi teine ei pea omakorda enda osaks 
            (näiteks sõber, ori, hobune -- ei ole kargud, 
             kuid programm, auto, nutitelefon, 
             kitsa intellektiga*3 robot -- on kargud)

            *3) NAI -- narrow artificial intellect 
            (others are strong/general and super: 
             https://medium.com/@tjajal
             /distinguishing-between-narrow-ai-general-ai-and-
             super-ai-a4bc44172e22)

# Taylori Seadused. Esimene osa. Kaja, kass ja tr
-------------------------------------------------

Logifailide, mälutõmmiste (core dump) ning krahhitõmmiste (crash dump) lahkamine nõuab, et oleksite võimelised kasutada tööriistu (tools), mis olid selleks loodud. Kui Windows'is me töötasime Powershelli abil, Linux'is selleks on nn. modifitseeritud Taylori seadused. Tuletan meelde, et originaal kõlab nii:

  Never write it in 'C' if you can do it in 'awk';
  Never do it in 'awk' if 'sed' can handle it;
  Never use 'sed' when 'tr' can do the job;
  Never invoke 'tr' when 'cat' is sufficient;
  Avoid using 'cat' whenever possible.
           
           --Taylor's Laws of Programming

Veel madalamal korrusel asub kooli informaatika. 


# Alustame cat 'ist
-------------------

Harjutus 1
```
  $ cat << EOF > hello
  Hello, 
  World!
  EOF
  $ cat hello
  Hello,
  World!
  $ cat -n hello
      1  Hello,
      2  World!
```
See on kõik, mida on vaja teada cat 'ist alustamiseks. Mida see annab?

  -- võime luua lihtsaid tekstifaile redaktorit kasutamata.
  -- saab vaadata tekstifaile.
  -- saab tekstifailide ridu numereerida.


# Vood ja ümbersuunamine  
------------------------

  stdin , stdout , stderr , tee , << , < , > , >> , 2>&1
  
Ei näe põhjust, miks ma peaks kirjutama sellest mida on lihtne leida google, duckduckgo abil või on piraatidelt.

# Harjutus 2.
```
$ sudo apk add bash
$ echo $0
-ash
$ bash
$ echo $0
bash

$ cat sss.c
#include <stdio.h>

int main() {
  fprintf( stderr, "Hello, World!\n");
  return 0;
}

$ ./sss 2>&1 | cat > output; cat output
Hello, World!
```

# Harjutus 3.
```
$ cat sss.c
#include <stdio.h>

int main() {
  fprintf( stdout, "Hello, \n");
  fprintf( stderr, "World!\n");
  return 0;
}

$ ./sss
Hello,
World!

$ ./sss 2> /dev/null
Hello,
$ ./sss 1> /dev/null
World!
$ $ ./sss 2>&1
Hello,
World!
```

# tr Kuninga nimel!
-------------------

  $ sudo apk add zlib-dev ncurses-dev
  
Heirloom arhiivi saame siit:
  https://sourceforge.net/projects/heirloom/files/heirloom/070715/heirloom-070715.tar.bz2/download

Mehed ei nuta, robotid ei laula*

  $ tar xf heirloom-070715.tar.bz2
  $ cd ~/heirloom-070715
  $ make


  $ echo 'в чащах юга жил был цитрус да но фальшивый экземпляр' | \
  tr абвгдежзийклмнопрстуфхцчшщъыьэюя abvgdeZzijklmnoprstufhcCwW\'yqEYA
  Ae ▒▒Ab▒▒Ab▒▒ ▒▒AzAb AnAsAC Ag▒▒AC ▒▒As▒вc▒▒▒o AjAb AqAY ▒▒AbAC▒▒▒▒AsAe▒▒Au ▒▒AhApAlAWAAC▒▒▒c

Houston, meil on siin väike probleem (Houston, we have problem here..)

  $ cd ~
  $ sudo apk add --no-cache cmake make musl-dev gcc gettext-dev libintl
  $ wget https://gitlab.com/rilian-la-te/musl-locales/-/archive/master/musl-locales-master.zip
  $ unzip musl-locales-master.zip
  $ cd musl-locales-master
  $ cmake -DLOCALE_PROFILE=OFF -D CMAKE_INSTALL_PREFIX:PATH=/usr . 
  $ make && sudo make install
  $ locale -a
  C
  C.UTF-8
  $ cat /etc/profile
  export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  export PAGER=less
  export PS1='\h:\w\$ '
  export MUSL_LOCPATH=/usr/share/i18n/locales/musl
  umask 022

  for script in /etc/profile.d/*.sh ; do
          if [ -r $script ] ; then
                  . $script
          fi
  done
  
  $ cat /etc/profile.d/locale
  CHARSET=UTF-8
  LANG=en_US.UTF-8
  LANGUAGE=en_US.UTF-8
  LC_ALL=en_US.UTF-8
  
Set time zone

  $ sudo apk add tzdata
  $ sudo cp /usr/share/zoneinfo/Europe/Tallinn /etc/localtime
  $ sudo bash -c 'echo "Europe/Tallinn" >  /etc/timezone'
  $ date
  Wed Apr 29 01:00:03 EEST 2020
  
Uuesti:

  $ echo 'в чащах юга жил был цитрус да но фальшивый экземпляр' | \
    tr абвгдежзийклмнопрстуфхцчшщъыьэюя abvgdeZzijklmnoprstufhcCwW\'yqEYA
  Ye AAYbAAYbAA AAYzYb YnYsYC YgAAYC AAYsAAAAAAAA YjYb YyYE AAYbYCAAAAYsYeAAYu AAYhYpYlYWYAYCAAAA

Houston, meil on ikkagi probleem (Houston, we still have problem..)

  $ cd alpine-pkg-glibc/
  $ mkdir releases
  $ cd releases
  $ wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.31-r0/glibc-2.31-r0.apk
  $ wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.31-r0/glibc-bin-2.31-r0.apk
  $ wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.31-r0/glibc-dev-2.31-r0.apk
  $ wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.31-r0/glibc-i18n-2.31-r0.apk
  $ sudo apk add --allow-untrusted /home/opilane/alpine-pkg-glibc/releases/glibc-2.31-r0.apk
  $ sudo apk add --allow-untrusted /home/opilane/alpine-pkg-glibc/releases/glibc-bin-2.31-r0.apk
  /usr/glibc-compat/sbin/ldconfig: /usr/glibc-compat/lib/ld-linux-x86-64.so.2 is not a symbolic link
  OK: 1224 MiB in 267 packages
  $ sudo apk add --allow-untrusted /home/opilane/alpine-pkg-glibc/releases/glibc-dev-2.31-r0.apk
  /usr/glibc-compat/sbin/ldconfig: /usr/glibc-compat/lib/ld-linux-x86-64.so.2 is not a symbolic link
  OK: 1292 MiB in 268 packages
  $ sudo apk add --allow-untrusted /home/opilane/alpine-pkg-glibc/glibc-i18n-2.31-r0.apk
  $ ls -l /usr/glibc-compat/lib/ld-linux-x86-64.so.2
  -rwxr-xr-x 1 root root 173800 Mar  9 23:04 /usr/glibc-compat/lib/ld-linux-x86-64.so.2
  $ cd /usr/glibc-compat/lib/
  $ sudo mv ld-linux-x86-64.so.2 ld-linux-x86-64.so
  $ cd /home/opilane/alpine-pkg-glibc
  $ # just curious to see whatif ?
  $ bash glibc-bin.trigger
  /usr/glibc-compat/sbin/ldconfig: Can't link /usr/glibc-compat/lib/ld-linux-x86-64.so.2 to ld-2.31.so
  /usr/glibc-compat/sbin/ldconfig: Can't create temporary cache file /usr/glibc-compat/etc/ld.so.cache~: Permission denied
  $ sudo bash glibc-bin.trigger

  $ sudo apk add bison flex zfs-dev
  $ sudo cp /usr/include/dirent.h /usr/include/sys/
  $ sudo cp /usr/include/libspl/sys/mkdev.h /usr/include/sys/
  $ make
  
Viskab vea ning lõpetab poole peal, kuid ta ehitas juba kõike, mida vajame.

  $ cd tr
  $ make
  $ sudo cp tr /usr/local/bin/tr-utf8
  $ cd ~
  
Proovime uuesti 

  $ echo 'Maša Žukova ütles et jõi öösel õunaäädikat.' | \
    tr-utf8 ŠšŽžÕõÄäÖöÜü ШшЖж662288Yy
  Maшa Жukova ytles et j6i 88sel 6una22dikat.

Töötab!

---------

Räägitakse, et FreeBSD-s on tr Unicode'iga sõbrad. Kuid sellest teine kord ...

  $ sudo apk add --no-cache  zlib-dev ncurses-de
  $ sudo apk add subversion
  $ mkdir FBSD
  $ cd FBSD
  $ svn checkout https://svn.freebsd.org/base/releng/12.1 .

# Viited ja kirjandus
---------------------

 *1) https://www.eki.ee/heli/index.php?option=com_content&view=article&id=6:k%C3%B5nes%C3%BCntees&catid=2:uncategorised&Itemid=465#SAPI5

  2. https://grrr.tech/posts/2020/add-locales-to-alpine-linux-docker-image/

  3. https://iweb.dl.sourceforge.net/project/heirloom/heirloom/070715/heirloom-070715.tar.bz2

  4. https://wiki.alpinelinux.org/wiki/How_to_get_regular_stuff_working
 
  5. https://grrr.tech/posts/2020/add-locales-to-alpine-linux-docker-image/

  6. https://wiki.alpinelinux.org/wiki/Setting_the_timezone


