Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bash like a marshall

Bash like a marshall

Bash is a programming language design for interact with user. Which have a very flexible syntax to take command from user. But in the bottom, it's just a programming language like any others, it provides variable, function and flow control..., we are going to reintroduce bash syntax and tools, some of you might already know, but is still good to review. In the end of this talk, everyone can write bash script like a marshall. (or at least understand Marshall's)

Jimmy Chao

May 11, 2016
Tweet

More Decks by Jimmy Chao

Other Decks in Programming

Transcript

  1. What is bash? A Programming Language designed for interact with

    user Load the interpreter when terminal start under unix Flexible and untyped
  2. Variable Space will separate parameter a=hello world will be interpret

    to a=hello, world should use quotation mark a=“hello world”
  3. Variable variable does not have type, so everything can be

    command a=echo $a $a => echo $a $a $a => echo echo $a $($a $a) => echo
  4. Variable PS1: Prompt PS2: Next line PS3: Read Prompt echo

    $PS1 \h:\W $(parse_git_branch)$(git config user.initials)
  5. Function $@ => all params $1 => first params $#

    => number of parameter $? => last function return value
  6. Function hello () { return 0 # only return 1/0

    } hello () { echo “hello” # return hello }
  7. Function require other source file by: source <filename> declare local

    variable (function scope) by: local a=“hello”
  8. Pipeline Pipeline can redirect an output (file descriptor) to another

    file descriptor echo ‘hello world’ > hello.txt can also redirect error message hello 2> error.txt can redirect thing into blackhole hello &> /dev/null
  9. Pipeline - File descriptor ˻ʒʒʒʒʒʒʒʒʒʒ˼ Keyboard ╾──╼┥0 bash STDOUT 1ʯ╾ʒʒʒ╼

    Display ! STDERR 2ʯ╾ʒʒʒ╼ error.txt ˾ʒʒʒʒʒʒʒʒʒʒ˽ echo “hello” 2> error.txt
  10. Pipeline - File descriptor ˻ʒʒʒʒʒʒʒʒʒʒ˼ Keyboard ╾──╼┥0 bash STDOUT 1ʯ╾ʒʒʒ╼

    hello.txt ! STDERR 2ʯ╾ʒʒʒ╼ error.txt ˾ʒʒʒʒʒʒʒʒʒʒ˽ echo “hello” 2> error.txt 1> hello.txt
  11. Pipeline - File descriptor ˻ʒʒʒʒʒʒʒʒʒʒ˼ Keyboard ╾──╼┥0 bash STDOUT 1ʯ╾ʒʒʒ╼

    display ! STDERR 2ʯ╾ʒʒʒ╼ hello.txt ˾ʒʒʒʒʒʒʒʒʒʒ˽ echo “hello” 1>&2 2> hello.txt
  12. Pipeline - File descriptor ˻ʒʒʒʒʒʒʒʒʒʒ˼ Keyboard ╾──╼┥0 bash STDOUT 1ʯ╾ʒ|

    ! STDERR 2ʯ╾ʒʒʒ╼ hello.txt ˾ʒʒʒʒʒʒʒʒʒʒ˽ echo “hello” 2> hello.txt 1>&2
  13. Pipeline we can pipe output as input to another command/function

    cat names.txt | grep jimmy on function side, the first line can read the piped input from STDIN say_my_name() { read name echo $name } echo “Jimmy” | say_my_name
  14. Flow Control - for for var in <collection> do echo

    $var done collection can be spaced string bash automatically separate params, ex: 1 2 3 4 5
  15. Test command test command check the parameter and return 0/1

    check result: test -z “” # return 0 -z : string is empty test -n “test” # return 0 -n : string is not empty
  16. Test command also written as: [ -n $str ] often

    used in flow control if [ -n $str ]; then echo $str fi
  17. Test command - string Use `man test` for all commands!

    [ -n “test” ] # => not empty [ -z “” ] # => empty [ $str = $str ] # => equal
  18. Test command - integer [ $int1 -eq $int2 ] #

    => equal [ $int -gt $int2 ] # => greater than [ $int -lt $int2 ] # => lesser than
  19. Test command - extended Extended test command [[ ]] use

    a more modern way of comparison: [[ $int1 = $int2 ]] [[ $int1 >= $int2 ]] [[ $int1 <= $int2 ]] It is always prefer to use this rather then [ ]
  20. Test command - file [[ -e ./hello.txt ]] # =>

    file exist [[ -f ./hello.txt ]] # => is file [[ -d /usr ]] # => is directory
  21. Test command - misc [[ ! -n “test” ]] #

    => not [[ -n “test” && -n “test2” ]] # => and, -a in test [[ -z “test” || -z “test2 ]] # => or, -o in test
  22. Unix commands Unix command is the essential part of bash

    Library in bash or bash perform as a glue for those commands. man is your good friend cat,grep,sort,uniq,comm,cut,perl,xargs,find,head, tail
  23. Unix commands - cut cut can extract string from input

    cut -d <delimiter> -f <field> echo ‘jimmy, php, js, ruby’ | cut -d , -f 1,3- => jimmy js ruby
  24. Unix commands - grep grep print line if pattern is

    matched cat names.txt | grep marshall => print line with marshall cat names.txt | grep -v marshall => print line is not marshall egrep can use regex as pattern
  25. Unix commands cat - output file cat name.txt head -

    print first n lines of input head name.txt -n 10 tail - print last n lines of input tail name.txt -n 5
  26. Unix commands wc - count character, word or line of

    file cat name.txt | wc -l sort - sort input by alphabetical order cat name.txt | sort uniq - don’t print continue non-unique line, cat name.txt | uniq
  27. Unix commands diff - show different lines in 2 files

    diff file1 file2 comm - show same lines in 2 files comm file1 file2 find - recursive find files by file name or content find ./ -name name.txt
  28. Unix commands xargs - construct argument list from input echo

    ‘-al’ | xargs ls => ls -al extra extra…
  29. AWK and SED Text process language read text file line

    by line detect pattern process line
  30. SED Most useful SED syntax is replace, just works as

    vim replace syntax cat name | sed s/jimmy/james/
  31. AWK <pattern> { <action> } cat name.txt | awk ‘{

    print $1 }’ cat name.txt | /jimmy/i ‘{ print $0 }’ cat name.txt | awk ‘\ BEGIN { print ‘Start awk’} { print $0 } END { print ‘End awk’}
  32. Perl… perl -pe ‘<perl code>’ expend to a loop, read

    input line by line and execute for each line. http://www.catonmat.net/blog/perl-one-liners-explained-part- one/
  33. Bash like a Marshall print all environments git tag |

    egrep '/.*/' | rev | cut -d / -f 2- | rev | sort | uniq
  34. Bash like a Marshall Run all spec that does not

    require rails find spec -name '*_spec.rb' | xargs egrep -L "require\\s+['\"]support\/rails['\"]" | egrep -v '^spec/nanny/' | grep -v does_boot_spec | xargs ruby bin/rspork
  35. Bash like a marshall Print all story numbers from commits

    story_regex() { local story_number="$1" [[ -z "$story_number" ]] && story_number='\d+' echo '\[ *(\w+ *)?[#0-9, ]*(#'"$story_number"'\b)[#0-9, ]* *]' } stories_in() { git log --format='%b' "$1" | egrep -o "$(story_regex)" | egrep -o '(\d+)' | sort -n | uniq }
  36. Bash like a Marshall find longest commit message and rank

    them git log —format="¶%H¬%B" | tr "\n" ' ' | tr -s ' ' | perl -pe 's/¶/\n/g' | grep -v Conflicts | grep -v 'diff --git' | perl -e 'print sort { length($b) <=> length($a) } <>' | head -25 | cut -d¬ -f 1 | xargs -n1 git log -1 --format='%h %an' | cat -n