Advance Shell Scripting Tutorial for beginners
- To create multiple directories in given range using script
#!/bin/bash # Shebang to specify the script should be run using Bash
# Function to create directories
create_directories() {
local prefix="$1" # First argument: prefix for the directory names
local start="$2" # Second argument: starting number for the range
local end="$3" # Third argument: ending number for the range
# Loop from start to end
for (( i=$start; i<=$end; i++ )); do
dir_name="${prefix}${i}" # Construct the directory name
mkdir "$dir_name" # Create the directory
echo "Directory '$dir_name' created." # Print confirmation
done
}
# Assign script arguments to variables
prefix="$1" # First argument: prefix for the directory names
start="$2" # Second argument: starting number for the range
end="$3" # Third argument: ending number for the range
# Call the function with the arguments
create_directories "$prefix" "$start" "$end" # Execute the function
- To execute the script run the command
./directiory_name.sh prefix start end
What is Cron and Crontab?
Cron is service in Unix-like operating system to execute scripts or commands at specific times (like scheduling jobs).
Crontab is a list of commands to run each job to efficiently schedule and manage recurring tasks on the system.
Useful Cron Commands
- To display the contents of the system-wide crontab file
cat /etc/crontab
- To list scheduled cron jobs for the current user
crontab -l
- To install crontab on On Red Hat-based systems (like CentOS or Fedora)
sudo yum install cronies
- To edit the crontab file
crontab -e
After executing the command 'crontab -e' you'll be redirected to a text editor such as nano or vi to create cron jobs.
Example Cron Job
- To schedule the
day5.sh
script, edit your crontab:
39 20 * * * cd /home/ec2-user && bash /home/ec2-user/scripts/day5.sh dir 1 10 >> /home/ec2-user/scripts/cronjob.log 2>&1
Means that the cron job will run every day at 8:39 PM (20:39).
Breakdown of the Cron Job Schedule
39
: The minute field (39th minute).20
: The hour field (20th hour, which is 8 PM in 24-hour format).*
: The day of the month field (every day of the month).*
: The month field (every month).*
: The day of the week field (every day of the week).
To Create Users in Unix-like Operating Systems
- To create users in Unix like operating system
sudo useradd username
- To assign a password to a user
sudo passwd username
- To list Usernames of all users
list only the usernames, you can use awk
to extract the first field from /etc/passwd
:
awk -F: '{print $1}' /etc/passwd
Summary of the cronjob Syntax:
To learn more about scheduling cronjobs visit:
Subscribe to my newsletter
Read articles from Muzammil Jan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Muzammil Jan
Muzammil Jan
Software Engineering student at Dha Suffa University, Karachi. Exploring the world of DevOps & Cloud! Love learning & giving back to open source communities. Connect with me on https://www.linkedin.com/in/muzammiljan/