9 Nisan 2020 Perşembe

find komutu exec seçeneği

Giriş
Açıklaması şöyle
Once you find some files or directories with the find command, you might want to do something for them. Thankfully, we can use the -exec option to run commands on each of the files or directories found. Any code after -exec will run on every file found.
exec seçeneğini kullanırken önce prova yapmak isteyebiliriz. Bu durumda komutun başına echo yazmamız yeterli.

Örnek
Şöyle yaparız. Böylece rm komutunun nasıl çalışacağı görülebilir.
find . type f -exec echo rm {} \;
1. \+ veya 
2. \; karakteri 

ile bitebilir. Açıklaması şöyle
Exec allows us to either pass all arguments at once with {} + or to pass them one by one with {} \;
1. \+ ile bitmesi - komuta çoklu parametre geçilir 
Açıklaması şöyle. -exec seçeneğinden sonra gelen komut mümkün olduğunca çok dosya ismi verilerek çalıştırılır yani her dosya için bir kere çalıştırılmaz.
This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files.  The command line is built in much the same way that xargs builds its command lines.
Örnek - yanlış kullanım
{} \+ arasında sadece boşluk (space) karakteri olabilir. Başka karakter giremez. Açıklaması şöyle
... there may be nothing between {} and + apart from whitespace. There is no way around that.
Dolayısıyla şu komut hata verir.
find . -name '*.png' -exec cp {} /tmp/dest \+
Örnek
exec ile çalıştırılan uygulamanın çıktısı başka uygulamaya geçilebilir. Şöyle yaparız,
Aslında cat a.txt b.txt c.txt | wc -l komutu ile aynıdır
find . -type f -exec cat {} \+ | wc -l
2. \; ile bitmesi - her parametre için komut bir kere daha çalıştırılır
-exec seçeneğinden sonra gelen komut için ayrı bir process yaratılır.
Örnek
Şöyle yaparız
find . -name "*.txt" -exec chown someOwner {} \;
Örnek
Bu komutun şu şekilde çalıştırılması riskli deniyor.
# risky
find -exec sh -c "something {}" \;
find -execdir sh -c "something {}" \;
Şu şekilde çalıştırmak daha güvenli deniyor ancak bu yöntemin pek kullanıldığını görmedim
# safer
find -exec sh -c 'something "$@"' sh {} \;
find -execdir sh -c 'something "$@"' sh {} \;
Örnek
copy için şöyle yaparız
find . -type f -newermt '16 july 2018' -exec cp {} /share/test \;
Örnek
move için şöyle yaparız
find src -name 'abc*' -type f -exec mv -nv {} dest/ \;
Eğer işlemden önce kullanılacak komutu görüp emin olmak istersek şöyle yaparız.
find src -name 'abc*' -type f -exec echo mv -nv {} dest/ \;
Örnek
Şöyle yaparız.
find . \( -name '*.jpg' -o -name '*.jpeg' \) -exec mv '{}' '{}'.new \;
Örnek
Dizin ismi ile dosya ismi aynı ise bunları bulmak için şöyle yaparız.
find . -type d -exec sh -c '
    for dirpath do
        filepath="$dirpath/${dirpath##*/}"
        [ -f "$filepath" ] && printf "%s\n" "$filepath"
    done' sh {} +
3. {} Kullanımı
 \+ veya \; ile geçilen parametreye erişmek için kullanılır

Örnek
Şöyle yaparız.
find . \( -name '*.jpg' -o -name '*.jpeg' \) -exec mv '{}' '{}'.new \;
4. $0 Kullanımı
Örnek - bash ile $0
Tüm results.txt dosyalarının ana dizinini ve dosyanın içini ekrana yazdırmak için şöyle yaparız.
$ find */ -name "result.txt" -exec bash -c 
  'printf "%s,%s\n" "${0%%/*}" "$(cat $0)"' {} \;
5. $1 Kullanımı
Örnek - 
bash ile $1
bash ile $1 kullanılabilir. Şöyle yaparız.
find . \( -name '*.jpg' -o -name '*.jpeg' \) -exec bash -c
  'cjpeg -quality 80 "$1" > "$(dirname "$1")/optimized_$(basename "$1")"' sh {} \;
Örnek
Şöyle yaparız. Burada file komutuna $1 ile dosya ismi geçiliyor. Dosya tipi PDF olanlar grep ile seçiliyor. 
find . -type f \
    -exec sh -c 'file "$1" | grep -q PDF' _ {} \;  \
    -exec sh -c 'echo mv -- "$1" "${1%%jpg}pdf"' _ {} \;
"_ {}" dizisinin açıklaması şöyle
_ {} is needed so that the contents of {} is assigned to $1 not $0
6. Komutun Sonucu
Başarısız olan uygulamaları görmek için şöyle yaparız
find . \! -exec yourscript {} \; -print

8 Nisan 2020 Çarşamba

LC_TIME Ortam Değişkeni

Örnek
Eğer zaman gösterimini LANG ile atanmış değerden farklı gösterme imkanı da bulunur. Şöyle yaparız.
$ LC_TIME=fr_FR.UTF-8 date
jeudi 22 août 2013, 10:41:30 (UTC+0100)

5 Nisan 2020 Pazar

/usr/local/bin Dizini - Kendi Geliştirdiğimiz Uygulamalar

Giriş
Bu dizin tüm kullanıcılar tarafından erişilebilecek root erişimine ihtiyaç duyan ve kendi geliştirdiğimiz  uygulamalarımız/betiklerimizi kurmak içindir.

Kaynak kodda derlediğimiz uygulamaları /usr/local altına koyabiliriz.

1. /usr/local/bin vs /usr/bin
/usr/bin dizini bu amaç için uygun değildir. Çünkü bu dizin paket yöneticisi tarafından kurulan uygulamalar tarafından sürekli değiştirilir. Açıklaması şöyle.
/usr/local/bin is for normal user programs not managed by the distribution package manager, e.g. locally compiled packages. You should not install them into /usr/bin because future distribution upgrades may modify or delete them without warning.
2. /usr/local/bin vs $HOME/.local/bin
Eğer uygulamayı sadece biz kullanacaksak $HOME/.local/bin dizinine kurmak daha mantıklı. Açıklaması şöyle
But for completeness I want to add that if you would like to install a script under your own account without root access (which can be a good alternative for programs that are not related to system administration), I would suggest putting it in $HOME/.local/bin.

2 Nisan 2020 Perşembe

/etc/default/grub Dosyası

Giriş
Bu dosyanın yolu şöyle. Bu dosyadaki tüm alanlar GRUB_XXX şeklinde.
/etc/default/grub
Bu dosyada değişiklik yaptıktan sonra update-grub komutu çağrılmalıdır. Şöyle yaparız.
sudo update-grub
GRUB_BADRAM Alanı
Bu alan normalde etkin değil. Nasıl kullandığına örnek şu satır var. Eğer RAM'DE arızalı alanlar varsa onların işletim sistemi tarafından kullanılmamasını sağlar.
# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"
Açıklaması şöyle.
The first parameter is easy. That is the base address of the bad memory....

The second parameter is a mask. You put 1s where the address range you want shares the same values and 0s where it will vary. This means you need to pick your address range such that only the low order bits vary.
GRUB_CMDLINE_LINUX_DEFAULT Alanı
Kernel parametrelerini belirtir. quite , splash, nomodeset değerlerini alabilir.
- quite seçeneği
Açıklaması şöyle. Kernel çıktı üretmez
this option tells the kernel to NOT produce any output (a.k.a. Non verbose mode). If you boot without this option, you'll see lots of kernel messages such as drivers/modules activations, filesystem checks and errors. Not having the quiet parameter may be useful when you need to find an error.
- splash seçeneği
Açıklaması şöyle. Splash screen gösterir.
this option is used to start an eye-candy "loading" screen while all the core parts of the system are loaded in the background. If you disable it and have quiet enable you'll get a blank screen.
- nomodeset seçeneği
Açıklaması şöyle.
tells the kernel to not start video drivers until the system is up and running.
Açıklaması şöyle.
The newest kernels have moved the video mode setting into the kernel. So all the programming of the hardware specific clock rates and registers on the video card happen in the kernel rather than in the X driver when the X server starts.. This makes it possible to have high resolution nice looking splash (boot) screens and flicker free transitions from boot splash to login screen. Unfortunately, on some cards this doesn't work properly and you end up with a black screen. Adding the nomodeset parameter instructs the kernel to not load video drivers and use BIOS modes instead until X is loaded.
Örnek
Normalde şöyledir.
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
Örnek
Disklerin journal kullanarak mount edilmesi için şöyle yaparız
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash root‐flags=data=journal"
GRUB_DEFAULT Alanı
En son açılan işletim sistemi hatırlasın istersek GRUB_DEFAULT = 0 yerine GRUB_DEFAULT = saved yaparız.
#GRUB_DEFAULT=0 # Rather than option #1, we'll always default to last boot choice.
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true
GRUB_TIMEOUT Alanı
Gub menüsünün 12 saniye görünmesini istersek şöyle yaparız.
GRUB_TIMEOUT=12
GRUB_TIMEOUT_STYLE Alanı
Şöyle yaparız.
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=0
/etc/grub.d/40_custom Dosyası
ISO dosyasından boot etmek için şöyle yaparız
menuentry "isoname ISO" {
  rmmod tpm
  set root=(hdX,Y)
  set isofile="/[path]/[isoname].iso"
      loopback loop $isofile
      linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile toram --
      initrd (loop)/casper/initrd
}
Açıklaması şöyle
Where (hdX,Y) is the disk and partition the ISO is on, for example /dev/sda3 would be (hd0,3). [path] is the path to the folder the ISO file is in, and [isoname] is the name of the ISO, for example /isos/ubuntu-20.04.2-desktop-amd64.iso is used if the Ubuntu ISO is located in a folder named isos on the root partition. rmmod tpm is only needed when booting in UEFI mode.

In Terminal run:
- sudo update-grub
-Reboot and select Ubuntu from the grub menu. Install as usual.

29 Mart 2020 Pazar

X11 İstemcisi - Client

X11 Kütüphanesi
Şu satırı dahil ederiz. Kütüphanenin ismi xlib
#include <X11/Xlib.h>
X11'in API'si oldukça zor. Direkt X11 kodlaması yerine QT, GTK gibi kütüphaneler tercih edilmeli.
Açıklaması şöyle.
Libraries such as qt, gtk, sdl, are next — they make it easier to use X11, and work on other systems such as wayland, Microsoft's Windows, or MacOS.
Xlib ile ilgili bazı eleştiriler şöyle
Xlib is an excellent piece of work, but there are applications for which it is not ideal, for example:

- Small platforms: Xlib is a large piece of code, and it's difficult to make it smaller
- Latency hiding: Xlib requests requiring a reply are effectively synchronous: they block until the reply appears, whether   the result is needed immediately or not.
- Direct access to the protocol: Xlib does quite a bit of caching, layering, and similar optimizations. While this is   normally a feature, it makes it difficult to simply emit specified X protocol requests and process specific responses.
- Threaded applications: While Xlib does attempt to support multithreading, the API makes this difficult and error-prone.
- New extensions: The Xlib infrastructure provides limited support for the new creation of X extension client side code.
Window Manager
X Client'ın çalışması için Window Manager şart değil. Açıklaması şöyle
No, you don't need to be running a window manager to allow an X client to work. Some systems provide an option to just run a terminal at startup, and from that you can start additional programs, including window managers. Some kiosk setups which only want one application to run don't need a window manager. Some implementations of X for microsoft windows avoid an X window manager by letting the OS manage the windows.

Without a window manager you typically need to specify the geometry to the programs so you don't have everything placed in the top left corner.
Java Swing ve Window Manager
Açıklaması şöyle. Yani Java Swing uygulamaları için Window Manager gerekir.
@StephenKitt Java apps (swing) steal the focus upon starting, which means that they completely break any focus-follows-mouse model, unless treated specially. Just like Firefox, java apps need special assistance from the window manager, otherwise drop-down menus won't open, entry boxes won't focus, etc. I don't know if that's still the case, but java was assuming that a window manager is reparenting, unless it was named "LG3D" or similar (java library had a select list of non-parenting WMs, and all the other non-reparenting WMs had to lie about their name ;-))
...
@mosvy that's still the case, at least for openjdk. E.g. in xmonad, you have to configure "LG3D" as window manager-name. Otherwise java-applications with a gui won't work
X11 İstemcileri Birbirleri İle Etkileşebilirler
Açıklaması şöyle.
All the X11 clients on a desktop can access each other in depth, including getting the content of any window, changing it, closing any window, faking key and mouse events to any other client, grabbing any input device, etc.

The X11 protocol design is based on the idea that the clients are all TRUSTED and will collaborate, not step on each other's toes (the latter completely broken by modern apps like Firefox, Chrome or Java).