Scripting Tutorial (revisi 140717-r01KI)

Return to Lab 00

  • Let's create tanyatutor.txt and fill it with text aaBBcccDdyyXXZZZZ. Type this:
    $ vi tanyatutor.txt
    i

    aaBBcccDdyyXXZZZZ

    [Esc]
    :wq
  • Create a file named kodetutor and fill it with below script. Change NPM with your student ID, and TOKEN with your token.
    $ vi kodetutor
    i

    # NPM is learning string manipulation.
    # ZCZC TOKEN
    # This is how you make comment in BASH scripting.
    
    teks=`cat $1`
    echo "$teks"

    Hint: the (`) sign in `cat $1` is located on the top left side of the keyboard, below [Esc] and beside 1.
  • Let's change kodetutor access privilege so we can execute the file.
    [Esc]
    :wq
    $ chmod 755 kodetutor
  • Run kodetutor script.
    $ ./kodetutor tanyatutor.txt

    aaBBcccDdyyXXZZZZ

    Turns out the above script is used to get the text content in tanyatutor.txt, put it into teks variable and then print the content of teks variable.
  • Now let's modify kodetutor content so that we can count the total character in teks variable.
    len=`expr length $teks`
    echo "$len"

    [Esc]
    :wq
    $ ./kodetutor tanyatutor.txt

    aaBBcccDdyyXXZZZZ
    17
  • Let's add a script to get character from position 5 to 8 (4 characters).
    stringA=`expr substr $teks 5 4`
    echo "$stringA"

    [Esc]
    :wq
    $ ./kodetutor tanyatutor.txt

    aaBBcccDdyyXXZZZZ
    17
    cccD
  • Now let's add a script to get 5 last characters.
    stringB=`expr "$teks" : '.*\(.....\)'`
    echo "$stringB"

    [Esc]
    :wq
    $ ./kodetutor tanyatutor.txt

    aaBBcccDdyyXXZZZZ
    17
    cccD
    XZZZZ
  • Find out how many times of substring "cc" and "ZZ" is repeated in tanyatutor.txt.
    jmlCC=`grep -o cc $1 | wc -w`
    echo "$jmlCC" 
    jmlZZ=`grep -o ZZ $1 | wc -w`
    echo "$jmlZZ"

    [Esc]
    :wq
    $ ./kodetutor tanyatutor.txt

    aaBBcccDdyyXXZZZZ
    17
    cccD
    XZZZZ
    1
    2
  • Bash scripting also accommodate arithmetic operations. Let's add simple arithmetic operation:
    a=10
    b=2
    echo `expr $a + $b`
    echo `expr $a - $b`
    c=`expr $a \* $b`
    echo "$c" 
    echo `expr $a / $b`
    echo `expr $a - 5`

    [Esc]
    :wq
    $ ./kodetutor tanyatutor.txt

    aaBBcccDdyyXXZZZZ
    17
    cccD
    XZZZZ
    1
    2
    12
    8
    20
    5
    5
  • In the end kodetutor script will have this content:
    # # NPM is learning string manipulation.
    # ZCZC TOKEN
    # This is how you make comment in BASH scripting.
    
    teks=`cat $1`
    echo "$teks" 
    len=`expr length $teks`
    echo "$len" 
    stringA=`expr substr $teks 5 4`
    echo "$stringA" 
    stringB=`expr "$teks" : '.*\(.....\)'`
    echo "$stringB" 
    jmlCC=`grep -o cc $1 | wc -w`
    echo "$jmlCC" 
    jmlZZ=`grep -o ZZ $1 | wc -w`
    echo "$jmlZZ" 
    a=10
    b=2
    echo `expr $a + $b`
    echo `expr $a - $b`
    c=`expr $a \* $b`
    echo "$c" 
    echo `expr $a / $b`
    echo `expr $a - 5`
  • Save the output of your script to jawabtutor.txt.
    ./kodetutor tanyatutor.txt > jawabtutor.txt
  • This marks the end of Scripting Tutorial.

Return to Lab 00
Return to Wiki