29 Nisan 2019 Pazartesi

bash kodlama - time built-in komutu

Giriş
Açıklaması şöyle. Yani /usr/bin/time komutundan farklı.
In ksh, bash and zsh, time is not a command (builtin or not), it's a reserved word in the language like for or while.

Örnek
Şöyle yaparız
{ time ./mycommand ; } &>> Output.txt 

dhclient komutu

Giriş
DHCP client aracıdır.

Örnek
IP adresi almak için şöyle yaparız.
dhclient enp0s3

-r seçeneği
release anlamına gelir. IP adresini bırakır. Şöyle yaparız.
dhclient -r wlan0
-v seçeneği
verbose logging anlamına gelir. Şöyle yaparız.
dhclient -v wlan0


etc/resolv.conf dosyası - DNS Sunucuları Listesi

Giriş
Bu dosyada DNS sunucuları sıralanır. Bu dosya dhclient, dnsmasq veya Network Manager tarafından yaratılır. C kütüphaneleri bu dosyayı okuyup DNS sorgusu yaparlar. Ancak bu dosya ilerleyen zamanlarda artık direkt elle değiştirilemeyecek. Açıklaması şöyle
Back in the days, whenever you wanted to configure DNS resolvers in Linux you would simply open the /etc/resolv.conf file, edit the entries, save the file and you are good to go. This file still exists but it is a symlink controlled by the systemd-resolved service and should not be edited manually.
Çünkü bilgisayarlar artık yavaş yavaş systemd-resolved servisini kullanmaya başlayacaklar. Açıklaması şöyle.
systemd-resolved is a service that provides DNS name resolution to local services and applications and it can be configured with Netplan, the default network management tool on Ubuntu 18.04.

Netplan configuration files are stored in the /etc/netplan directory. You’ll probably find one or two YAML files in this directory. The file name may differ from setup to setup. Usually, the file is named either 01-netcfg.yaml or 50-cloud-init.yaml but in your system, it may be different.

These files allow you to configure the network interface, including the IP address, gateway, DNS nameservers, and so on.
nameserver Alanı
DNS sunucusunu belirtir. /etc/resolv.conf dosyası benim sistemimde şöyle görünüyor. Yani WIFI modem'in IP adresi
nameserver 127.0.0.53
Ancak aslında ben 8.8.8.8 yani Google DNS sunucusunu kullanıyorum. Yani etc/resolv.conf dosyasında bu bilgi yok!

ndots Alanı
Elimizde şöyle bir dosya olsun
nameserver 100.64.0.10
search namespace.svc.cluster.local svc.cluster.local cluster.local eu-west-1.compute.internal
options ndots:5

Açıklaması
şöyle
As you can see there are three directives:
1. The nameserver IP is the Kubernetes service IP of kube-dns
2. There are 4 local search domains specified
3. There’s a ndots:5 option
Açıklaması şöyle. Yani eğer aranılan isimde 5'ten az nokta ile ayrılmış alan varsa search ile tanımlı alanlar ilave edilerek arama yapılır
This means that if ndots is set to 5 and the name contains less than 5 dots inside it, the syscall will try to resolve it sequentially going through all local search domains first and - in case none succeed - will resolve it as an absolute name only at last.
Örnek
Şöyledir.
cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 200.200.200.1
nameserver 200.200.200.2
nameserver 200.200.200.3
Örnek
search domain1.com domain2.com domain3.com
nameserver 10.10.10.10
nameserver 10.10.10.11
options ndots:5
Örnek
Bu dosyayı baştan yaratmak için şöyle yaparız
rm /etc/resolv.conf
echo "nameserver 192.168.1.1" > /etc/resolv.conf

28 Nisan 2019 Pazar

rfkill komutu

Giriş
Açıklaması şöyle.
rfkill is a small userspace tool to query the state of the rfkill switches, buttons and subsystem interfaces. Some devices come with a hard switch that lets you kill different types of RF radios: 802.11 / Bluetooth / NFC / UWB / WAN / WIMAX / FM. Some times these buttons may kill more than one RF type. The Linux kernel rfkill subsystem exposes these hardware buttons and lets userspace query its status and set its status through a /dev/rfkill. Given that at times some RF devices do not have hardware rfkill buttons rfkill the Linux kernel also exposes software rfkill capabilities that allows userspace to mimic a hardware rfkill event and turn on or off RF.
list seçeneği
Şöyle yaparız
rfkill list
Cihaz tipi olarak Wireless LAN,Bluetooth,WiMAX görebiliriz.
Örnek
Çıktı olarak şuna benzer bir çıktı alırız.
0: phy0: Wireless LAN
  Soft blocked: no
  Hard blocked: yes
1: i2400m-usb:1-1.6:1.0: WiMAX
  Soft blocked: yes
  Hard blocked: no
Örnek
Şuna benzer bir çıktı alırız.
0: ideapad_wlan: Wireless LAN
  Soft blocked: no
  Hard blocked: no
1: ideapad_bluetooth: Bluetooth
  Soft blocked: no
  Hard blocked: no
2: hci0: Bluetooth
  Soft blocked: no
  Hard blocked: no
unblock seçeneği
Şöyle yaparız
rfkill unblock all
Şöyle yaparız
rfkill unblock 0

4 Nisan 2019 Perşembe

mv komutu - Dosya taşır

Giriş
move anlamına gelir. copy komutu taşıma yerine kopyalama için kullanılabilir.

Söz dizimi şöyledir.
mv filedestination_dir
Açıklaması şöyle.
mv is a standard utility to move one or more files to a given target. It can be used to rename a file, if there's only one file to move. If there are several, mv only works if the target is directory, and moves the files there.

So mv foo bar will either move the file foo to the directory bar (if it exists), or rename foo to bar (if bar doesn't exist or isn't a directory). mv foo1 foo2 bar will just move both files to directory bar, or complain if bar isn't a directory.

mv will call the rename() C library function to move the files, and if that doesn't work (they're being moved to another filesystem), it will copy the files and remove the originals.
Neden mv Komutunda -r (recursive) seçeneği Yok?
Bir soru şöyle. Yani mv komutu tek bir şeyi taşır. Dolayısıyla recursive olmasına gerek yok
Q : Why does 'mv' not need '-r' to work with directories, but 'rm' and 'cp' do?
A : 
 rm has to recurse into the directory ...
 cp has to recurse into the directory ...
 mv just has to do a single call 
Örnek - rename
.txt dosyalarını .txt.old olarak yeniden ismlendirmek için şöyle yaparız.
mv dir1/dir2/dir3/file.{txt,txt.old}
-- seçeneği
Bu özel bir seçenek. Şu anlama geliyor. Eğer dosya isimlerinde de "-" karakteri varsa bu isimler seçenek gibi algılanabiliyor. "--" seçeneğiyle artık yeni seçeneklerin kabul edilmemesi sağlanıyor. Açıklaması şöyle
Most command-line utilities have a provision to separate options from arguments.

That prevents a multitude of problems with "special" file names that look like options.

The most common separator indication the end of the options is two hyphens -- or -- which is also supported by mv.
Örnek
Soru şöyle
Here is my problem :
i have a file with -r in the name :
-r.jpg

when i tried to do a :
mv *.jpg old/

i have a :
mv: invalid option -- 'r'
Cevabı şöyle
mv -- *.jpg old/
-t seçeneği
target directory anlamına gelir. Şeklen şöyledir.
mv -t destination_dir  file
Örnek
Tüm dosyaları farklı bir dizine taşımak için şöyle yaparız
printf '%s\0' files/* | xargs -0 mv -t new_files_dir
Örnek
mp3 dosyalarını bir üst dizine taşımak için şöyle yaparız.
find . -path "*/flac/*" -name '*.mp3' -execdir mv -t ../ {} +

3 Nisan 2019 Çarşamba

2 Nisan 2019 Salı

bash kodlama - while döngüsü

Giriş
Açıklaması şöyle. [ ...] test işlemi sadece değişkenler için kullanılır.
[ .. ] is not part of shell syntax like if statements. It is not equivalent to parentheses in C-like languages, if (foo) { bar; }, and should not be wrapped around commands to test.
[ is just regular command, like whoami or grep, but with a funny name (see ls -l /bin/[). It's a shorthand for test.
If you want to check the exit status of a certain command, use that command directly.
If you want to check the output of a command, use "$(..)" to get its output, and then use test or [/[[ to do a string comparison:
Örnek 
Değişken kullanmak için şöyle yaparız.
#!/bin/bash
COUNTER=0

while [ "$COUNTER" -lt 10 ] && ps aux | grep -q "[r]elayevent.sh"   ; do

  sleep 3

  let COUNTER+=1

done
Örnek
Şu kod, ps alt komutu [...] içinde kullandığı için hata verir.
COUNTER=0
while [ ps aux | grep "[r]elayevent.sh" ] && [ "$COUNTER" -lt 10 ]; do
    sleep 3
    let COUNTER+=1
done
Örnek
Aynı değişkeni iki farklı değerler karşılaştırmak için şöyle yaparız
while [  "$choice" != "yes" ] && [ "$choice" != "no" ]
Örnek
Sonsuz döngü kurmak için şöyle yaparız.
#!/bin/bash

while true; do
  echo "something" >> bash.txt
done
Örnek
Sonsuz döngü kurmak için şöyle yaparız. Bu sefer true yerine built-in command olan ":" kullanılıyor.
while :
do
  something
done
Şöyle yaparız.
while :
do
  sleep 10
done
Örnek
Komutun exit kodunu kullanarak dosyanın tüm satırlarını okumak için şöyle yaparız.
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
  echo "Text read from file: $line"
done < "$1"
Aynı satırları silmek için buradaki kod kullanılabilir.
Örnek
Break ile çıkmak için şöyle yaparız.
#!/bin/bash

while true
do
    echo "Please input anything here: "
    read INPUT

    if [ `expr $INPUT % 5` -eq 0 ]; then
        echo "you entered wrong"
        break
    else
        echo "you entered right"
    fi
done
Örnek
Script'e verilen parametreleri parse etmek için şöyle yaparız.
while getopts ":d:f:p:e:" o; do
  case "${o}" in
        d)
            SDOMAIN=${OPTARG}
            ;;
        f)
            FROM=${OPTARG}
            ;;
        p)
            PAGER=${OPTARG}
            ;; 
        e)
            DESTEXT=${OPTARG}
            ;;                       
        *)
            show_usage
            ;;          
    esac
done