SHELL SCRIPTING

🧠 SHELL SCRIPTING REVIEWER

(Based on your handout )


πŸ’» 1. What is Shell Scripting?

In simple terms:
Shell scripting = automating commands instead of typing them one by one.

Key Points:

  • The shell acts as a middleman between:

    • πŸ‘€ User

    • βš™ Kernel (OS core)

  • It’s a CLI (Command-Line Interface) β€” text-based siya, walang GUI.

πŸ‘‰ Think of it like:

Instead of doing tasks manually, you tell the computer:
β€œDo this… then this… then this…” automatically.

Real-life Example:

Instead of typing:

mkdir folder
cd folder
touch file.txt

You write a script, then boom πŸ’₯β€”isang run lang, tapos na.


🧠 Quick Summary:


  • Shell = interface


  • Script = automation


  • Goal = less effort, more efficiency

πŸ‘‰ Mnemonic:

S.H.E.L.L = System Helper for Executing Logical Lines


βš™ 2. Bash & Script Execution

Key Idea:

  • Bash = most common shell (Bourne Again SHell)


  • Script files end in .sh

Important Notes:


  • Scripts are executed top to bottom


  • Created using editors like:


    • vim


    • gedit


    • emacs

Basic Script Example:

#!/bin/sh
# This is a comment
echo "Hello World"

Breakdown:

  • #!/bin/sh β†’ start of script

  • # β†’ comment

  • echo β†’ prints output


🧠 Quick Summary:


  • Bash = scripting language

  • .sh = script file


  • Executes line-by-line

πŸ‘‰ Mnemonic:

B.A.S.H = Basic Automation Script Handler


πŸ“ 3. Names and Strings

Important Rules:


  • ❗ Case-sensitive:

    • file.txt β‰  File.txt


  • Allowed:


    • letters, numbers, ., _


  • Avoid:

    • > < | : &

Why important?

Kasi kahit small mistake (like uppercase), error agad 😭


🧠 Quick Summary:


  • Case-sensitive ALWAYS


  • Avoid special characters

πŸ‘‰ Tip:

β€œIf it looks the same to you, hindi ibig sabihin same siya kay Bash πŸ˜…β€


πŸ“¦ 4. Variables and Operators

What are Variables?

Storage ng data na pwede mong gamitin ulit.

Types of Variables:

Type

Meaning

Local

Exists only in current script

Shell

Built-in variables

Environment

Accessible by child processes

πŸ“Œ Examples:

greetings="Hello"
echo greetings</code></pre><h4id="59ed2802βˆ’fc3cβˆ’4a60βˆ’bf12βˆ’e59adb2b0885"dataβˆ’tocβˆ’id="59ed2802βˆ’fc3cβˆ’4a60βˆ’bf12βˆ’e59adb2b0885"collapsed="false"seolevelmigrated="true">SpecialExample:</h4><ul><li><p><code>greetings</code></pre><h4 id="59ed2802-fc3c-4a60-bf12-e59adb2b0885" data-toc-id="59ed2802-fc3c-4a60-bf12-e59adb2b0885" collapsed="false" seolevelmigrated="true">Special Example:</h4><ul><li><p><code>BASH_VERSION β†’ shows bash version



🧠 Quick Summary:


  • Variables = storage

  • $ = ginagamit to access value

πŸ‘‰ Mnemonic:

V.A.R = Value Assigned & Reused


πŸ”’ 5. Special Variables

Super important β€˜to kapag may arguments πŸ‘€

Variable

Meaning

$0

Script name

$1, $2

Arguments

#

Number of arguments

*

All arguments

$?

Last command status

</code></p></td><tdcolspan="1"rowspan="1"><p>ProcessID</p></td></tr></tbody></table><p><spandataβˆ’name="pushpin"dataβˆ’type="emoji">πŸ“Œ</span>Frompage2table</p><divdataβˆ’type="horizontalRule"><hr></div><h4id="c4d9a593βˆ’76f0βˆ’4b1bβˆ’916aβˆ’38de78a6d4be"dataβˆ’tocβˆ’id="c4d9a593βˆ’76f0βˆ’4b1bβˆ’916aβˆ’38de78a6d4be"collapsed="false"seolevelmigrated="true"><spandataβˆ’name="brain"dataβˆ’type="emoji">🧠</span>QuickSummary:</h4><ul><li><p><code></code></p></td><td colspan="1" rowspan="1"><p>Process ID</p></td></tr></tbody></table><p><span data-name="pushpin" data-type="emoji">πŸ“Œ</span> From page 2 table </p><div data-type="horizontalRule"><hr></div><h4 id="c4d9a593-76f0-4b1b-916a-38de78a6d4be" data-toc-id="c4d9a593-76f0-4b1b-916a-38de78a6d4be" collapsed="false" seolevelmigrated="true"><span data-name="brain" data-type="emoji">🧠</span> Quick Summary:</h4><ul><li><p><code> = special access


  • Used for inputs & system info

  • πŸ‘‰ Mnemonic:

    β€œ = Special Power”


    ⌨ 6. Input & Arithmetic

    Input:

    read name

    πŸ‘‰ ginagamit to accept user input


    Arithmetic Operators:

    Operator

    Meaning

    +

    Add

    -

    Subtract

    *

    Multiply

    /

    Divide

    %

    Modulo

    Example:

    let a=5+3

    ⚠ Important:

    [ $a == $b ] 

    βœ… [a==$$b] ❌


    🧠 Quick Summary:

    • read = input

    • let = math


    • spaces matter ❗

    πŸ‘‰ Mnemonic:

    R.A.M = Read And Math


    βš– 7. Relational Operators

    Used for comparisons (numbers)

    Operator

    Meaning

    -eq

    equal

    -ne

    not equal

    -gt

    greater than

    -lt

    less than

    -ge

    β‰₯

    -le

    ≀

    πŸ“Œ From page 3


    🧠 Quick Summary:


    • Used in conditions


    • Mostly inside [ ]

    πŸ‘‰ Mnemonic:

    β€œEQ = Equal, GT = Greater, LT = Lower”


    πŸ”€ 8. Conditional Statements

    IF-ELSE Logic:

    if [ condition ]
    then
      command
    else
      command
    fi

    πŸ‘‰ Example from handout:

    If equal β†’ print OK

    Else β†’ ERROR


    Case Statement (Cleaner Alternative)

    Instead of maraming if-else 😡

    case $var in
      option1) ;;
      option2) ;;
      *) default ;;
    esac

    Parts:

    • case β†’ start

    • ) β†’ option

    • ;; β†’ end case

    • * β†’ default


    🧠 Quick Summary:


    • IF = decision


    • CASE = cleaner decision

    πŸ‘‰ Mnemonic:

    β€œIF = Think, CASE = Choose”


    πŸ” 9. Loops

    For Loop

    πŸ‘‰ fixed number of times

    for i in 1 2 3
    do
      echo $i
    done

    While Loop

    πŸ‘‰ runs while condition is TRUE

    while [ condition ]
    do
      command
    done

    Until Loop

    πŸ‘‰ opposite ng while

    Runs until condition becomes TRUE


    Comparison Table:

    Loop

    Behavior

    for

    fixed iterations

    while

    runs while true

    until

    runs until true

    πŸ“Œ From page 4


    🧠 Quick Summary:


    • For = count-based


    • While = condition TRUE


    • Until = condition FALSE β†’ TRUE

    πŸ‘‰ Mnemonic:

    F.W.U = For, While, Until


    πŸ”₯ FINAL RECAP (Memory Tricks)

    Alright, eto na yung quick recall cheat sheet mo:

    πŸ’‘ FLOW of Shell Script:


    1. Shell β†’ interface


    2. Bash β†’ language


    3. Script β†’ automation


    4. Variables β†’ storage


    5. Input/Operators β†’ processing


    6. Conditionals β†’ decisions


    7. Loops β†’ repetition


    🧠 SUPER MNEMONIC:

    πŸ‘‰ β€œS.B.S V.I.C.L” (read as: β€œSibs Vicel πŸ˜‚β€)

    • Shell

    • Bash

    • Script

    • Variables

    • Input

    • Conditionals

    • Loops


    πŸ’¬ Final Advice (Gen Z mode ON):

    This part is important kasi foundation ito ng automation + Linux skills mo.

    Pag gets mo β€˜to, next level ka na sa scripting πŸ’»πŸ”₯