19 Mayıs 2019 Pazar

bash kodlama - if koşulu

Giriş
bash kodlarken if koşulu notlarım

Logical Kontroller
- String eşitlik için "==" kullanılır
- Sayısal Eşitlik için "eq" kullanılır
- Sayısal farklılık için "ne" kullanılır
- bash test yazısına bakabilirsiniz.

1. If koşulu tek satırsa fi'den önce ve sonra ; gelir
Örnek
Şu kod yanlış cünkü fi'den önce ; gelmiyor.
if [ -f ~/.bashrc ]; then   . ~/.bashrc fi
Şöyle yaparız
if [ -f ~/.bashrc ]; then   . ~/.bashrc; fi
Örnek
Şöyle yaparız.
function cgo() { if g++ -std=c++11 "$1" ; then ./a.out; fi; }
# ........................................................^
2 Test Koşulu

2.1 Çift Köşeli Parantez
If koşulunda çift köşeli parantez ile içerdeki değişkenler boşluk ile ayrılmalıdır.

Yanlış Kod
Şu kod yanlış çünkü [[ ile $x bitişik.
if [[$x = "foo"]]; then
Şu kod doğru çünkü [[ ile $x ayrık
if [[ $x = "foo" ]]; then
Örnek - eq koşulu
Şöyle yaparız.
i=4
if [[ $i -eq 4 ]] && false ; then
     echo this will not print
fi
if [[ $i -eq 4 ]] && true ; then
     echo this will print
fi
Örnek - == Koşulu
Şöyle yaparız
if [[ "$1" == "" ]]               # if parameter 1 is blank
then
    LOGFILE="/var/log/syslog"     # LOGFILE set to /var/log/syslog
else
    LOGFILE="$1"                  # LOGFILE set to parameter 1
fi
Bu kodun kısa halı şöyle.
LOGFILE=${1:-/var/log/syslog}
Örnek - == Koşulu
Şöyle yaparız. Burada txt uzantılı dosyalar üzerinde dolaşıp bunları tek bir dosyada birleştiriyoruz. Aralarına da \n ekliyoruz. Ancak çıktı dosya da newfile.txt olduğu için for döngüsünde bu dosyayı atlamak gerekiyor. Bu yüzden cat işlemi || (or) ile yapılıyor.
for f in *.txt; do 
    [[ "$f" = newfile.txt ]] || { cat -- "$f"; printf "\n"; }
done > newfile.txt
Eğer newfile.txt kontrolünü yapmazsak şu hatayı alırız. Yani çıktı olan dosyayı aynı anda okumaya da çalışıyoruz anlamına gelir.
cat: newfile.txt: input file is output file
Örnek - ne Koşulu
Şöyle yaparız
if [[ $EUID -ne 0 ]]; then
  echo "You must be root to run this script."
  exit
fi
2.2 Tek Köşeli Parantez
Örnek
Bash ile iki tane eşittir kullanılır. Şöyle yaparız.
if [ "$var" == "foo" ]; then  # bash
sh ile tek eşittir kullanılır. Şöyle yaparız.
if [ "$var" = "foo" ]; then   # sh
Örnek
Şöyle yaparız.
BCKGRND=yes
if [ "$BCKGRND" = "yes" ]; then
    sleep 5 &
else
    sleep 5
fi
Örnek
Şöyle yaparız. Burada "command substitution" yapılıyor. Sonra -n ile string'in boş olmadığı kontrol ediliyor.
if [ "$(id -u)" -eq 0 ]
then
    if [ -n "$SUDO_USER" ]
    then
        printf "This script has to run as root (not sudo)\n" >&2
        exit 1
    fi
    printf "OK, script run as root (not sudo)\n"
else
    printf "This script has to run as root\n" >&2
    exit 1
fi
3. && Kullanımı
Şöyle yaparız.
if [ "$i" -eq 4 ] && command1; then
   echo 'All is well'
else
   echo 'i is not 4, or command1 returned non-zero'
fi

diff komutu

- r seçeneği
Örnek
İki tane dizindeki farklı dosyaları bulmak için kullanılabilir. Şöyle yaparız
$ unzip -q hazelcast-enterprise-5.2.3.zip 
$ unzip -q hazelcast-enterprise-5.2.3-slim.zip 
$ diff -qr hazelcast-enterprise-5.2.3-slim hazelcast-enterprise-5.2.3
Only in hazelcast-enterprise-5.2.3: custom-lib
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-3-connector-common-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-3-connector-interface-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-avro-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-cdc-debezium-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-cdc-postgres-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-csv-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-elasticsearch-7-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-files-azure-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-files-gcs-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-files-s3-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-grpc-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-hadoop-all-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-kafka-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-kinesis-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-protobuf-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-python-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-jet-s3-5.2.3.jar
Only in hazelcast-enterprise-5.2.3/lib: hazelcast-mapstore-5.2.3.jar
Only in hazelcast-enterprise-5.2.3: management-center
--suppress-common-lines seçeneği
Şöyle yaparız.
diff "code1.js" "code2.js" --suppress-common-lines | tee outputFile

13 Mayıs 2019 Pazartesi

host komutu

Giriş
Açıklaması şöyle.
host is normally part of bind-utils (or equivalent) and like both nslookup and dig uses only DNS

DNS Sorgusu Araçları
DNS sorgusu için kullanılabilecek araçlar şöyle 

dig komutu
host komutu
nslookup komutu (çok eski)

Örnek

Şöyle yaparız.
host somename
Örnek
Şöyle yaparız. Burada DNS kaydı bulunamıyor
$ host exodus.babeldat.com 192.168.1.2
Using domain server:
Name: 192.168.1.2
Address: 192.168.1.2#53
Aliases: 

Host exodus.babeldat.com not found: 3(NXDOMAIN)

/etc/nsswitch.conf Dosyası

Giriş
Açıklaması şöyle. Name Service Switch (NSS) tarafından kullanılır.
On Linux name resolution is most commonly controlled by NSS which is configured by /etc/nsswitch.conf

Örnek
Şöyle yaparız.
hosts:          files dns
Açıklaması şöyle
In the above entry you can see that the first thing to be queried is files followed by dns, meaning that /etc/hosts will be queried before DNS. Other options exist including LDAP, Multicast DNS and WINS.
Örnek
Şöyle yaparız.
passwd:     files sss
shadow:     files sss
group:      files sss
hosts:      files dns myhostname
(...) 


12 Mayıs 2019 Pazar

apt-get komutu

Giriş
add-apt-repository, apt ile kardeştir. Kurulan paketler global (system wide) olarak kurulur. Kullanıcı silinince paket silinmez. System wide kurulum yapmayan başka paket yöneticileri de var ancak ben kullanmadım.

apt-get komutunun nasıl çalıştığı burada.

Yeni Kurulum Yapınca, Açık ve Çalışmakta Olan Sürüm Etkilenmez
Açıklaması şöyle.
With Windows, replacing a file that is in use by a running process can badly affect that process. The process will reference locations within that file and get incorrect information from it, usually with catastrophic results. That's why a Windows update generally requires a reboot to ensure that all processes are using correct versions of libraries etc.

With Unix, once a file has been opened by a process, that same file will always be available to the process even if the original file is removed from the filesystem.

After an update, the filesystem will contain a different version of the file, and all process that start after the update will use that new file. But, unlike Windows, all old Unix processes will continue using the original files that they started with. Even though no longer accessible via the filesystem, those files will persist as long as any process is using them. Eventually, when no processes are using the files, the old version of the files will finally be deleted.
Tek problem uygulamanın spawn veya fork()/exec() yapması. Chrome'un kullandığı çözüm şöyle.
Typically, updating a program while it's already open is no problem – as the other answerers have explained, a running process can continue to run even if its executable is deleted.

However, due to Firefox's multi-process model, you may get a prompt to restart it after an update anyway. This is because Firefox spawns new processes to isolate different websites, so if it spawns a new process after you've updated it but before you restart Firefox, the new process will be a newer version of Firefox than the rest of the browser. This can cause various issues, so Firefox might prompt you to restart it before allowing you to continue.

Incidentally, Chrome avoids this by using a "zygote" process that sits around doing nothing; when the browser needs to spawn a new process, instead of asking the OS to execute the browser executable again (which would execute the possibly-updated binary) it asks the zygote process to duplicate itself, and one of the copies then becomes a normal renderer process.
Tarihçe Dosyası
Şöyle
/var/log/apt/history.log
Paket İsmi
Örnek
Eğer paket isminden sonra eksi karakteri geliyorsa paket kaldırılır. Açıklaması şöyle.
If a hyphen is appended to the package name (with no intervening space), the identified package will be removed if it is installed. Similarly a plus sign can be used to designate a package to install.
Şöyle yaparız.
sudo apt-get install python3-
autoremove seçeneği
Örnek ver.

install seçeneği
Belirtilen paketi yükler. Şöyle yaparız.
apt-get install tree
Sorulara yes cevabı vermek için şöyle yaparız.
apt-get install -y openjdk-8-jre openjdk-8-jdk
print-uris seçeneği
İndirilen paketlerin adreslerini görmek için şöyle yaparız
apt-get --print-uris update
purge seçeneği
Örnek ver.

remove seçeneği 
Paketin ismini bilmiyorsak ancak içindeki bir dosyayı biliyorsak, paket ismini öğrenmek için için dpkg -S komutu kullanılabilir.
Paket silinse bile home dizininde kullanıcıya mahsus ayarlar dosyası kalabilir. Ayarlar dosyası ~/.config dizininde veya silinen paketin kendi ismi ile bir dizinde (örneğin ~/.foo) olabilir.

Örnek
Şöyle yaparız
sudo apt remove foo
Örnek
Şöyle yaparız.
sudo apt-get remove gdb
update seçeneği
Örnek
Şöyle yaparız
sudo apt-get update
Eğer devam etmekte olan bir kurulum varsa şuna benzer bir hata verir
Reading package lists... Done
E: Could not get lock /var/lib/apt/lists/lock. It is held by process 1131 (packagekitd)

N: Be aware that removing the lock file is not a solution and may break your system.

E: Unable to lock directory /var/lib/apt/lists/
Hata mesajındaki "process 1131 (packageitd)" kısmına dikkat etmek gerekiyor. Dosyayı hangi uygulamanın kilitlediği burada yazılır. Açıklaması şöyle
The lock file was created by process 1131 (packagekitd). It was created so that that process could make changes to the package system without worrying about other programs doing other changes at the same time.

If you remove the lock file, you make it possible for another process, such as apt-get, to make changes simultaneously with packagekitd. Since there was no lock file, apt-get will also assume it is the only process making changes.
upgrade seçeneği
Örnek ver

7 Mayıs 2019 Salı

PS1 Ortam Değişkeni

Giriş
PS1 komut satırında görünen prompt karakteridir. Değiştirmek için şöyle yaparız.
export PS1=%
PS1 genellikle ~/.bashrc dosyasından export edilir.

$ işareti
Ben hep bash kullandığım için $ işareti görünüyor.Açıklaması şöyle
The % prompt is common in csh-type shells while sh-shells (like bash and ksh93) ordinarily uses a $ as the prompt.
sh kabuğunda şöyle görünür.
% more tinyUF.txt
10
4 3
3 8
6 5
# işareti
bash'te root iken # işareti görünüyor. Açıklaması şöyle
The prompt usually changes to # for the root user since a sufficiently powerful user should be reminded of that power by having an alternate prompt (as the POSIX standard puts it).

The primary prompt in sh-type shells are determined by the value of the shell variable PS1.
\H seçeneği
Açıklaması şöyle. Promt olarak hostname görmek istersek kullanırız.
- \h to get host name up to the first dot
- \H to get full host name
Örnek
Şöyle yaparız
export PS1="[\u@\H ]#"
Örnek
Interactive shell olup olmadığını anlamak için şöyle yaparız.
# If running interactively, then:
if [ "$PS1" ]; then

  # If this is an xterm 
  case $TERM in
  xterm*)
    ...
    ;;
  *)
    ;;
  esac
fi