$30 off During Our Annual Pro Sale. View Details »

Bash 101

Bash 101

16 Mayıs 2015, Istanbul PHP Meetup #008'de yaptığım sunum.

Uğur Özyılmazel

May 16, 2015
Tweet

More Decks by Uğur Özyılmazel

Other Decks in Programming

Transcript

  1. BASH 101
    Geliştiriciler İçin

    View Slide

  2. Uğur "vigo" Özyılmazel
    vigobronx vigo

    View Slide

  3. bash
    Bourne Again
    SHell

    View Slide

  4. Brian J. Fox

    View Slide

  5. View Slide

  6. shell
    Komutları çalıştıran
    Makro işlemci
    1

    View Slide

  7. shell
    Komut yorumlayıcısı
    2

    View Slide

  8. shell
    Programlama Dili
    3

    View Slide

  9. Mac OS X (BSD) komutları ile
    Ubuntu / Linux komutları
    farklılık gösterir

    View Slide

  10. $ help
    # help
    $ help pwd
    help

    View Slide

  11. View Slide

  12. konfigürasyon
    ~/.profile
    ~/.bashrc
    Kullancı Bazında
    }
    /etc/profile
    /etc/bashrc
    Sistem Bazında
    }

    View Slide

  13. konfigürasyon
    1
    ~/.profile
    2
    ~/.bashrc

    View Slide

  14. #!/usr/bin/env bash
    source "$HOME/.bashrc"
    ~/.profile

    View Slide

  15. # non interactive shell
    [ -z "$PS1" ] && return
    export DOTFILES_PATH="$HOME/Dotfiles"
    source $DOTFILES_PATH/shell/env
    source $DOTFILES_PATH/shell/alias
    source $DOTFILES_PATH/shell/ls_colors
    # Load private files
    # ------------------------------------------------------
    # Loads every file under private/ folder!
    if [[ -d $DOTFILES_PATH/private ]]; then
    if [ "$(ls -A $DOTFILES_PATH/private)" ]; then
    ~/.bashrc

    View Slide

  16. Environment Variables
    env

    View Slide

  17. TERM_PROGRAM=Apple_Terminal
    COMP_WORDBREAKS=
    "'><;|&(:
    TERM=xterm-256color
    SHELL=/bin/bash
    HISTSIZE=30000
    TMPDIR=/var/folders/gz/hvbfjmnx5z3bl6k05bp4w04h0000gn/T/
    DOCKER_HOST=tcp://192.168.59.103:2376
    Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.PyiyHWlqqs/
    Render
    TERM_PROGRAM_VERSION=343.7
    TERM_SESSION_ID=59036979-ACF9-409F-9141-9AAA2941844E
    CDPATH=.:..:../..:/Users/vigo/Desktop/Dropbox
    SVN_EDITOR=mate -w
    HISTFILESIZE=30000
    USER=vigo
    DOCKER_TLS_VERIFY=1
    PATH=/Users/vigo/.rbenv/shims:/Users/vigo/.rbenv/bin:/usr/local/heroku
    bin:/usr/local/heroku/bin:/Users/vigo/bin:/usr/local/sbin:/usr/local/
    bin:/usr/bin:/bin:/usr/sbin:/sbin
    PWD=/Users/vigo/Development/Source/sinatra
    EDITOR=mate -w

    View Slide

  18. Prompt Shell
    $PS1

    View Slide

  19. PS1='\h:\W \u\$ '
    \h Ugurs-MacBook-Air
    \W ~
    \u vigo

    View Slide

  20. View Slide

  21. https://github.com/vigo/dotfiles-universal
    http://linuxconfig.org/bash-prompt-basics

    View Slide

  22. gündelik komutlar
    curl touch mkdir alias man
    sudo history grep xargs less
    pwd ls cd cp mv rm chown
    ln find awk
    cat git ps more chmod

    View Slide

  23. komut ?
    $ man
    $ man ls
    $ man pwd
    $ man -k "kelime"

    View Slide

  24. View Slide

  25. h

    View Slide

  26. View Slide

  27. man help (less)
    /
    n sonraki bulunan kelime
    N önceki bulunan kelime
    f 1 ekran ileri git
    b 1 ekran geri git
    g ilk satıra git
    G son satıra git
    q çık

    View Slide

  28. View Slide

  29. alt man sayfaları
    $ man
    $ man 3 getcwd
    $ man 2 chdir

    View Slide

  30. alias
    $ alias # hepsini göster
    $ alias XX=''
    $ alias la='ls -al'
    $ alias la # göster

    View Slide

  31. alias
    $ alias ls='ls -al'
    $ \ls # orijinal ls ile
    Ezdiğiniz orijinal komutları \ ön eki ile çağırabilirsiniz!

    View Slide

  32. Unix Felsefesi
    Birbiriyle konuşan küçük
    küçük programcılar!

    View Slide

  33. pipe
    $ | | | ...

    View Slide

  34. pipe
    $ man 5 -t nanorc | open -f -a preview
    nano'nun konfigürasyon dosyası ile ilgili 5.dökümanı
    pdf haline getirmek. Mac OS X için geçerlidir.

    View Slide

  35. $ find . -iname '*1*.jpg'
    pipe
    ./file_1.jpg
    ./file_10.jpg
    ./file_11.jpg
    ./file_12.jpg
    ./file_13.jpg
    ./file_14.jpg
    ./file_15.jpg
    ./file_16.jpg
    ./file_17.jpg
    ./file_18.jpg
    ./file_19.jpg

    View Slide

  36. $ find . -iname '*1*.jpg' | xargs -n 1 echo "Bulunan:"
    pipe
    Bulunan: ./file_1.jpg
    Bulunan: ./file_10.jpg
    Bulunan: ./file_11.jpg
    Bulunan: ./file_12.jpg
    Bulunan: ./file_13.jpg
    Bulunan: ./file_14.jpg
    Bulunan: ./file_15.jpg
    Bulunan: ./file_16.jpg
    Bulunan: ./file_17.jpg
    Bulunan: ./file_18.jpg
    Bulunan: ./file_19.jpg

    View Slide

  37. $ find . -iname '*1*.jpg' | xargs -n 1 rm
    pipe

    View Slide

  38. redirection
    $ ls tmp/
    file_1.jpg file_12.jpg file_15.jpg file_18.jpg file_20.jpg
    file_5.jpg file_8.jpg file_10.jpg file_13.jpg file_16.jpg
    file_19.jpg file_3.jpg file_6.jpg file_9.jpg file_11.jpg
    file_14.jpg file_17.jpg file_2.jpg file_4.jpg file_7.jpg
    $ ls tmp/ > dir_list.txt

    View Slide

  39. redirection
    $ > dosya

    # yoksa oluştur ya da üzerine yaz
    $ >> dosya

    # ek yap (append)
    $ < dosya

    $ sort -r < dir_list.txt 

    # dosyayı komuta yönlendir

    View Slide

  40. komut kombinasyonu
    $ ; ; 

    # herhangibiri hata verse bile diğeri devam eder.
    $ && && 

    # herhangibiri hata verirse diğer işlemler çalışmaz.
    $ || || 

    # komutlar bitince sekans durur.

    View Slide

  41. BASH
    Programlama Dilidir!

    View Slide

  42. standart
    • Değişken tanımlamaları
    • Fonksiyon tanımlamaları
    • Kontrol mekanizmaları
    • Döngü mekanizmaları
    • Hata yakalama mekanizmaları
    • Regular Expression

    View Slide

  43. değişken
    A=5 # = ile değişken arası
    A="5" # bitişik olmalı!
    A=$(date) # date komutunun değeri
    A=`date` # date komutunun değeri
    export A="Hello"
    Default: Global Scope

    View Slide

  44. fonksiyon
    function (){
    # buraya kod
    }
    function md() {
    mkdir -p "$@" && cd "$@"
    }

    View Slide

  45. fonksiyon
    deneme(){
    echo "$@"
    }
    deneme2(){
    echo "$1"
    }
    deneme 1 2 3 4 # => 1 2 3 4
    deneme2 1 2 3 4 # => 1

    View Slide

  46. fonksiyon
    A=5
    B="lego"
    deneme(){
    local A="vigo"
    echo "\$A: ${A} \$B: ${B}"
    }
    deneme # => $A: vigo $B: lego

    View Slide

  47. kontrol
    if [[ durum ]]; then
    # kod
    fi
    if [[ durum ]]; then
    # kod
    elif [[ durum ]]; then
    # kod
    fi

    View Slide

  48. kontrol
    # Ubuntu ?
    if [[ `which lsb_release` ]]; then
    if [[ `lsb_release -i | awk '{print $3}'` == "Ubuntu" ]]; then
    source $DOTFILES_PATH/shell/ubuntu_specific
    fi
    fi
    # Mac ?
    if [[ `uname` == "Darwin" ]]; then
    source $DOTFILES_PATH/shell/mac_specific
    fi

    View Slide

  49. döngü
    export DOTFILES_PATH="$HOME/Dotfiles"
    for file in $DOTFILES_PATH/private/*; do
    source $file
    done
    x=0
    read_file="/Users/vigo/Desktop/bash101-demo/dummy_text"
    cat $read_file | while read line; do
    x=$(($x+1))
    echo "Satır: ${x}, ${line}"
    done

    View Slide

  50. döngü
    typeset -a test_array
    test_array=(
    "username" "vigo"
    "password" "secret"
    )
    test_array_count=${#test_array[*]}
    for ((i=0; i<=$((($test_array_count/2)-1)); i++)); do
    key="${test_array[(0+($i*2))]}"
    value="${test_array[(1+($i*2))]}"
    echo "${i} : ${key} ${value}"
    done

    View Slide

  51. Teşekkürler, Sorular ?

    View Slide