Scripting Tutorial (revisi 121003-r04KI)

Return to Lab 03

  • We will use vim for editing NPM.txt.
  • Create a file named NPM.txt. Change NPM with your student ID.
    vim 1202000818.txt
  • Type this sentence 212 times. Change NPM with your student ID. Hint: look at Lab02 to copy.
    Saya NPM suka makan mangga dan minum susu.
  • Change all of the phrase "makan mangga" with "minum susu".
    [Esc]
    :%s/makan mangga/minum susu/
  • Change all of the phrase "minum susu" in the end of every sentence in line 3 - 125 with "jalan-jalan". There are 3 ways that you can use, choose one:
    :3,125s/dan minum susu/dan jalan-jalan/

    OR
    :3,125s/minum susu\./jalan-jalan./

    OR
    :3,125s/dan minum susu\.$/dan jalan-jalan./
  • Change all the phrase "minum susu" in line 126 with "jalan-jalan".
    :126s/minum susu/jalan-jalan/g
  • Change the phrase "jalan-jalan" in line 101 to be "tidur siang".
    :101s/jalan-jalan/tidur siang/
  • Save your work and exit vim
    :wq
  • Now let's create tanyatutor.txt and 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"

    [Esc]
    :wq

    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 03
Return to Wiki