basic scripts

✅ Shell Basics

echo $SHELL    # Check the default shell for the user (e.g., /bin/bash, /bin/zsh)

✅ Shebang

#!/bin/bash    # Shebang line to specify the script should run using the bash shell

✅ Script: exp1.sh

#!/bin/bash

# Script to print static welcome message

echo -e "Hi\nThis is DevOps class"
echo "Started on Nov"

🔧 Execution Methods

./exp1.sh        # Executes script directly (make sure it's executable)
bash exp1.sh     # Executes using bash explicitly
sh exp1.sh       # Executes using default shell interpreter

✅ Script: exp2.sh

🔧 Execution Example


✅ Input Arguments in Shell Script

  • $0 → Script name

  • $1 to $9 → 1st to 9th arguments

  • ${10}, ${11} → 10th argument onward

🧠 Example:


✅ Script: exp3.sh

✅ Conditional Statement Syntax


✅ Script: exp4.sh – Check if Number is 5


✅ Numeric Comparison Operators in Bash

Operator
Meaning

-eq

Equal to

-ne

Not equal to

-lt

Less than

-le

Less than or equal to

-gt

Greater than

-ge

Greater than or equal to


✅ Script: exp5.sh – Biggest of Two Numbers


✅ Script: exp6.sh – Biggest of Two Numbers with Argument Check


✅ Special Shell Variables

Variable
Meaning

$?

Exit status of last command (0 = success, non-zero = failure)

$$

Process ID of the current shell script

$!

PID of the last background command

$*

All arguments as a single string

$@

All arguments as individual strings (useful in loops)

$#

Total number of input arguments


✅ Loop Syntax

🔁 for Loop

🔁 while Loop



✅ Script : Disk Usage Alert if More Than 90%


✅ Cron Job Syntax Reference

Field
Range
Description

Minute

0–59

Minute of the hour

Hour

0–23

Hour of the day

Day of Month

1–31

Day of the month

Month

1–12

Month of the year

Day of Week

0–6 (Sun–Sat)

Day of the week


✅ Useful Cron Commands


✅ Cron Job Examples


✅ Display Employees Over Age 40

📄 input File: employees.txt

✅ Script: filter_employees.sh