Cron Job
Reference: https://crontab.guru/
Sample Script (sample.sh):
#!/bin/bash
name="dev"
echo "$name" >> /tmp/demo.txt
Add Execute Permission
chmod +x sample.sh
Schedule with Crontab
crontab -e
Add the following line with your desired cron expression:
* * * * * /root/sample.sh
This runs
sample.shevery minute. Adjust the expression as needed using crontab.guru.
Cron Expression Format
* * * * * command
│ │ │ │ │
│ │ │ │ └── Day of week (0–7, Sun=0 or 7)
│ │ │ └──── Month (1–12)
│ │ └────── Day of month (1–31)
│ └──────── Hour (0–23)
└────────── Minute (0–59)
Functions
#!/bin/bash
# Function with a parameter
greeting() {
echo "Executed by user: $1"
}
greeting "devops"
# Function with local variable and return value
add() {
local sum=$(( $1 + $2 ))
echo "The total is: $sum"
}
add 4 6
Key Points
- Use
localinside functions to avoid polluting the global scope. - Pass arguments to functions using
$1,$2, etc. - Functions must be defined before they are called.
wc (Word Count)
wc [options] filename
| Flag | Description |
|---|---|
-l |
Count lines |
-w |
Count words |
-c |
Count bytes/characters |
-m |
Count characters |
Examples:
wc -l /tmp/demo.txt # Count lines in file
wc -w sample.sh # Count words in script
wc -c sample.sh # Count bytes in script
wc /tmp/demo.txt # Show lines, words, and bytes together
