22 Mart 2020 Pazar

bash kodlama - BASH_VERSINFO Değişkeni

Giriş
bash'ten önce de bir sürü shell vardı. Açıklaması şöyle
Between sh and bash there are at least

- sh PWB-Shell (1975),
- csh C-Shell (1978),
- sh Bourne-Shell (1979),
- tcsh T-C Shell (1981) and
- ksh Korn-Shell (1983)
BASH_VERSINFO Değişkeni
Açıklaması şöyle.
A readonly array variable whose members hold version information for this instance of bash. The values assigned to the array members are as follows:

BASH_VERSINFO[0] The major version number (the release).
BASH_VERSINFO[1] The minor version number (the version).
BASH_VERSINFO[2] The patch level.
BASH_VERSINFO[3] The build version.
BASH_VERSINFO[4] The release status (e.g., beta1).
BASH_VERSINFO[5] The value of MACHTYPE.

Örnek
Bash sürümünün 4'ten büyük olduğunu kontrol etmek isteyelim. Açıklaması şöyle.
Notice that BASH_VERSINFO is an array. As with any array in bash, using it as a plain variable refers to its 0-th element. You can also use ((BASH_VERSINFO[0] >= 4)) if you find it more clear.
Şöyle yaparızç
if ((BASH_VERSINFO >= 4)); then ...

20 Mart 2020 Cuma

apt komutu - deb Dosyalarını Kurar

Giriş
apt komutu kilit (lock) kullandığı için aynı anda birden fazla paketin kurulumuna müsaade etmez. apt komutu ile yum komutu kardeştir.

apt vs dpkg
apt altta dpkg komutunu kullanır. apt dosya indirebilme yeteneğine sahip, dpkg ise sadece yerel .deb dosyalarını kurabilir

apt vs snap
Açıklaması şöyle. snap paketleri için snap komutu kullanılır
Apt only works on deb packages. Nothing else.

It doesn't work on Snaps. Nor Flatpaks. Nor Pips. Nor Wheels. Nor Brews. Nor git clones. Nor source code that you compiled or downloaded or write. Nor anything else.
Hepsini çalıştırmak için şöyle yaparız
sudo sh -c "apt update && apt dist-upgrade -y && snap refresh && flatpak upgrade -y"
Kardeş Komutlar
apt-add-repository komutu
apt-cache komutu
apt-file komutu
apt-get komutu
apt-key komutu

apt Uygulama Çalışırken Nasıl Güncelleme Yapar?
Açıklaması şöyle.
In POSIX (and Linux is a POSIX system), at least when using POSIX file systems, files are allowed to remain open after they're deleted (that is, after they're unlinked, by removing the link from a particular file name to the underlying inode object which is what actually identifies a particular string of bytes as the contents of a file). Files in this state continue to be accessible to code that has an open handle to them (a file descriptor in the user land, other structures in the kernel land), although they no longer have a name and anything that tries to open the old name will no longer be able to get them.
apt komutu Diğer Komutları Birleştirir
apt komutu apt-getapt-cache gibi komutları tek bir çatı altında birleştirir.

apt Patterns
apt 2'den itibaren düzenli ifade (regular expression) kullanımı değişti.
Örnek
Şöyle yaparız. Düzenli ifadeden önce ~n karakteri gelir.
apt install '~nlibkf5.*'
Uzun halini kullanmak istersek şöyle yaparız. ?name(...) şeklidedir.
apt install '?name(libkf5.*)'
Düzenli ifade eskiden şöyleydi
apt install libkf5.*
autoremove seçeneği
Şöyle yaparız.
sudo apt autoremove
full-upgrade seçeneği
Şöyle yapaız
#!/bin/bash

sudo apt update
sudo apt full-upgrade -y
sudo apt install jq
sudo apt autoclean -y
sudo apt autoremove
install seçeneği
Eğer bir dosya belirtmezse verdiğimiz ismi repository'de arar

Örnek
deb dosyasını kurmak için şöyle yaparsak hata alırız.
sudo apt install vs.deb

Reading package lists... Done   
Building dependency tree          
Reading state information... Done   
E: Unable to locate package vs.deb   
E: Couldn't find any package by glob 'vs.deb'
Şöyle yapmak gerekir
sudo apt install ./vs.deb
Örnek
Kurmadan sadece indirmek için şöyle yaparız
sudo apt install package_name --download-only
list seçeneği
Kurulu paketlerin bilgilerini verir. 
Örnek
Kurulu tüm paketleri görmek için şöyle yaparız.
$ apt list --installed
Örnek - Bir Paketi Görmek
gedit paketini görmek için şöyle yaparız.
$ apt list --installed gedit
gedit/stable 3.22.0-1
Örnek - Bir Paketi Görmek
Şöyle yaparız
apt list '?narrow(?installed,?name(wine))'
Örnek
Wildcard kullanmak istersek şöyle yaparız
apt list --installed '*wine*'
"(using the ~i and ~n shorthands for ?installed and ?name)" şeklinde düşünürsek şöyle yaparız
apt list '?narrow(~i,~nwine)'
purge seçeneği
-s simulation anlamına gelir. Belirtilen paketi kaldırırsak ne olacağını görmek için şöyle yaparız
apt purge -s python2.7
-s seçeneği
simulate anlamına gelir. Açıklaması şöyle
Passing the -s option to apt causes it to simulate installation but not actually install or modify anything. That shows what you can expect to see from apt when you install the package, including which packages if any are pulled in to satisfy its (direct and indirect) dependencies.
...
That shows you what steps would be taken by sudo apt install apache2.

The -s option can also be spelled as any of --simulate, --just-print, --dry-run, --recon, --no-act. So if you see (or write) any of those, they're doing the same thing.

The -s option is documented in man apt-get and not in man apt (the latter of which doesn't document most features and options), but both apt-get -s install ... and apt -s install ... are supported.

You can likewise simulate other actions with -s, such as the remove action.

Unlike with apt commands that actually make changes to your system, running apt -s as root is optional, so you can omit sudo.
Örnek
Şöyle yaparız
apt -s install apache2
search seçeneği
Örnek
Şöyle yaparız
apt search mongodb
show seçeneği
Açıklaması şöyle
Show information about the given package(s) including its dependencies, installation and download size, sources the package is available from, the description of the packages content and much more. It can e.g. be helpful to look at this information before allowing apt(8) to remove a package or while searching for new packages to install.
Örnek
Şöyle yaparız
$ apt show kate | grep 'APT-Sources'
APT-Sources: http://ar.archive.ubuntu.com/ubuntu focal/universe amd64 Packages
Örnek - version
Paketin sürümünü gösterir. Şöyle yaparız
$ apt show usb-creator-gtk | grep -i version

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Version: 0.3.5ubuntu18.04.2
unattended-upgrades seçeneği
Açıklaması şöyle
No, apt on its own won’t reboot.

You can check for /var/run/reboot-required after running apt to see if a reboot is required. If you use unattended-upgrades, you can configure that to reboot for you.
update seçeneği - Sadece listeyi günceller
Açıklaması şöyle
The apt update updates your software repository lists, and apt upgrade upgrades packages from those lists (but may leave some behind until you perform the apt full-upgrade
Örnek
Şöyle yaparız
#!/bin/bash

apt update -y
apt upgrade ufw sshguard unattended-upgrades wget curl git zip unzip tree -y

netstat komutu

Giriş
Bu komut artık eskidi. netstat yerine ss komutu kullanılabilir. Açıklaması şöyle
NOTE
   This program is obsolete.  Replacement for netstat is ss.   Replacement
   for  netstat -r is ip route.  Replacement for netstat -i is ip -s link.
   Replacement for netstat -g is ip maddr.
Seçenekler şöyle
NAME
  netstat  -  Print  network connections, routing tables, interface statistics, 
    masquerade connections, and multicast memberships

SYNOPSIS
  netstat 
    [address_family_options]
    [--tcp|-t]
    [--udp|-u]
    [--raw|-w]
    [--listening|-l]
    [--all|-a]  
    [--numeric|-n]  
    [--numeric-hosts]  
    [--numeric-ports] 
    [--numeric-users]
    [--symbolic|-N] 
    [--extend|-e
    [--extend|-e]]  
    [--timers|-o]  
    [--program|-p]  
    [--verbose|-v]
    [--continuous|-c]

-a seçeneği
All anlamına gelir. netstat -t ile sadece dışarı giden bağlantıları görebiliriz.  Tüm bağlantıları görmek icin netstat - a -t yaparız.

-g seçeneği
Şöyle yaparız. Sunucunun dahil olduğu multicast gruplarını gösterir.
netstat -g
-l seçeneği
Listening anlamına gelir. Açıklaması şöyle
-l, --listening
   Show only listening sockets.  (These are omitted by default.)
Örnek
Dinleme yapan TCP sunucularını görmek için şöyle yaparız.
netstat -lptn
Çıktı olarak şunu alırız. -l ile sadece LISTEN durumundaki, -p ile PID, -t ile TCP ve -n ile metin yerine IP numaraları gösterilir.
Proto Recv-Q Send-Q Local Address     Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:9092    0.0.0.0:*               LISTEN      18949/java
tcp        0      0 127.0.0.1:38021   0.0.0.0:*               LISTEN      3518/java
tcp        0      0 0.0.0.0:8081      0.0.0.0:*               LISTEN      18949/java
tcp        0      0 0.0.0.0:38417     0.0.0.0:*               LISTEN      3518/java
tcp        0      0 127.0.0.1:53      0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:41623   0.0.0.0:*               LISTEN      15189/code
tcp        0      0 127.0.0.1:631     0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:43097   0.0.0.0:*               LISTEN      18949/java
-n seçeneği
Bağlantıların IP ve port numaralarını sayısal olarak gösterir. Açıklaması şöyle
--numeric, -n
   Show numerical addresses instead of trying to determine symbolic host,
   port or user names.
Örnek
Şöyle yaparız Linux'ta Listening TCP portları listelenmesi anlatılmış.
-t : Tcp only
-l : listening ports
-n : just numbers
-p : show process info
me@myhost:~$ sudo netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address   Foreign Address   State    PID/Program name
tcp        0      0 127.0.0.1:53    0.0.0.0:*         LISTEN   1004/dnsmasq    
tcp        0      0 0.0.0.0:22      0.0.0.0:*         LISTEN   380/sshd        
tcp        0      0 127.0.0.1:631   0.0.0.0:*         LISTEN   822/cupsd       
tcp6       0      0 :::22           :::*              LISTEN   380/sshd        
tcp6       0      0 ::1:631         :::*              LISTEN   822/cupsd 
-p seçeneği

Linux'ta process numaralarını gösterir. Açıklaması şöyle
-p, --program
   Show the PID and name of the program to which each socket belongs.
Örnek
Şöyle yaparız. 2401 process numarasıdır.
# netstat -apn | grep fe80::cef1:3
tcp6       0      0 fe80::cef1:12345 fe80::cef1:34660 ESTABLISHED 2401/xxxx
Örnek
Şöyle yaparız. En sağdaki sütunda process numaraları görülebilir.
$ netstat -tulpen
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name    
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      112        62240      -                   
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      1000       65495      48618/python        
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      101        23068      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          27689      -                   
tcp6       0      0 :::5432                 :::*                    LISTEN      112        62241      -                   
tcp6       0      0 :::9090                 :::*                    LISTEN      0          71091      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      0          27691      -                   
udp        0      0 127.0.0.53:53           0.0.0.0:*                           101        23067      -                   
udp        0      0 192.168.0.15:68         0.0.0.0:*                           100        23071      -                   
udp6       0      0 fe80::224:e8ff:fe22:546 :::*                                100        23086      -  
-t seçeneği
TCP protokolü içindir.  Açıklaması şöyle
--tcp|-t

--udp|-u

-l, --listening
   Show only listening sockets.  (These are omitted by default.)

-p, --program
   Show the PID and name of the program to which each socket belongs.

--numeric, -n
   Show numerical addresses instead of trying to determine symbolic host,
   port or user names.
Örnek
TCP ve UDP dinleyen uygulamaları görmek için şöyle yaparız.
netstat -tulpn
-u seçeneği
UDP protokolü içindir. Açıklaması şöyle
--udp|-u
Örnek
netstat -antup ile şuna benzer bir çıktı alırız.
13:tcp        0      0 0.0.0.0:3333 0.0.0.0:*  LISTEN      26566/./test


mmv komutu - Genellikle Dosya İsmi Değiştirmek (rename) İçin Kullanılır

Örnek
İsmi 3 karakter + '-' karakteri ile başlayan dosyaları bulup bu 4 karakteri silerek yeniden isimlendirmek için şöyle yaparız
$ mmv '???-*' '#4'

Linux ve Asenkron Programlama

Dave Cutler Kimdir
Açıklaması şöyle.
Dave Cutler is well known for his contributions to operating systems, having led the effort on VAX VMS at DEC and Windows NT at Microsoft. According to his Wikipedia page, he is also known for his attitudes toward Unix.
Unix İşletim Sistemine Eleştirisi
Dave Cutler Unix işletim sistemini asenkron programlamayı tam desteklemediği için eleştiriyor.
Açıklaması şöyle.
[Cutler] expressed his low opinion of the Unix process input/output model by reciting "Get a byte, get a byte, get a byte byte byte" to the tune of the finale of Rossini's William Tell Overture.
Halbuki asenkron programlama yeni değil. Açıklaması şöyle.
The I/O model on "Cutler systems" -- RSX-11M, VAX/VMS, Windows NT -- is an asynchronous packet-driven I/O model, rather than the fundamentally synchronous I/O model of Unix. At its core, you fire off an I/O request, and get a notification of when it's complete. Meanwhile, execution continues.

Of course, it's trivial for the system to provide synchronous I/O routines on top of that: have the I/O request completion set a condition ('event flag') and then wait on that condition.

In contrast, adding asynchrony on top of a synchronous I/O system is rather more involved.

usbfs Dosya Sistemi

Giriş
Bu dosya sistemi artık kullanılmıyor. Açıklaması şöyle.
The deprecated usbfs refers to a pseudo-filesystem which was typically mounted under /proc/bus/usb and was completely removed from the kernel with the 3.5 release.

However the functionality provided by the old usbfs filesystem is still present, via sysfs and the “usbfs driver”.
Bu dosya sistemin eskiden sağladığı bilglere /sys/bus/usb/drivers/usbfs dizinden ulaşılabilir.

15 Mart 2020 Pazar

kill komutu

-l seçeneği
Gönderilebilecek sinyalleri listelenir.
Örnek
Şöyle yaparız.
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX
kill -0
Ardından pid gelir. Açıklaması şöyle.
$ man 2 kill
...
If sig is 0, then no signal is sent, but error checking is still performed; 
this can be used to check for the existence of a process ID or process 
group ID.
...
Ben şöyle anlıyorum. kill ile normalde 1'den başlayan sinyaller uygulamaya gönderilir. kill -0 ile uygulamaya sinyal gönderme hakkımız olup olmadığını kontrol edebiliriz.

Örnek
Şöyle yaparız.
if ! kill -0 $(cat /path/to/file.pid); then
  ... do something ...
fi
pid 0 ise
Açıklaması şöyle.
If pid equals 0, then sig is sent to every process in the process group of the calling
 process.
pid değerini 0 olarak vermek istersek şöyle yaparız. -- seçeneklerin bittiğini gösterir.
kill -- -0
Böylece kill -0'dan farklı bir komut olduğu anlaşılır.