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

Special Example:

  • $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

$$

Process ID

๐Ÿ“Œ From page 2 table


๐Ÿง  Quick Summary:

  • $ = 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 ๐Ÿ’ป๐Ÿ”ฅ