Create a script that asks for the user's name and then greets them.
- Use
readto get input from the user. - Use
echoto print the greeting.
Example:
#!/bin/bash
echo "Please enter your name:"
read name
echo "Hello, $name! Welcome to the script."Create a script that takes two numbers as input and performs addition, subtraction, multiplication, and division.
- Use
readto get input from the user. - Use arithmetic expansion
$(())to perform calculations.
Example:
#!/bin/bash
echo "Enter first number:"
read num1
echo "Enter second number:"
read num2
sum=$((num1 + num2))
difference=$((num1 - num2))
product=$((num1 * num2))
if [ $num2 -ne 0 ]; then
quotient=$((num1 / num2))
else
quotient="undefined (division by zero)"
fi
echo "Sum: $sum"
echo "Difference: $difference"
echo "Product: $product"
echo "Quotient: $quotient"Create a script that checks if a file exists and prints a message accordingly.
- Use an
ifstatement with the-eflag to check for file existence.
Example:
#!/bin/bash
echo "Enter the filename to check:"
read filename
if [ -e "$filename" ]; then
echo "The file '$filename' exists."
else
echo "The file '$filename' does not exist."
fiCreate a script that prints numbers from 1 to 10.
- Use a
forloop to iterate through a range of numbers.
Example:
#!/bin/bash
for i in {1..10}; do
echo "Number: $i"
doneCreate a script that backs up a specified directory to another directory.
- Use
cp -rto copy directories recursively. - Use
readto get the source and destination directories from the user.
Example:
#!/bin/bash
echo "Enter the source directory to backup:"
read source
echo "Enter the destination directory:"
read destination
if [ -d "$source" ]; then
cp -r "$source" "$destination"
echo "Backup of '$source' completed in '$destination'."
else
echo "The source directory '$source' does not exist."
fiCreate a script that checks the disk usage of your home directory and prints a warning if it exceeds a certain limit.
- Use
du -shto get the disk usage. - Use an
ifstatement to compare the usage with a threshold.
Example:
#!/bin/bash
usage=$(du -sh ~ | cut -f1)
limit="1G"
if [[ "$usage" > "$limit" ]]; then
echo "Warning: Your home directory is using $usage, which exceeds the limit of $limit."
else
echo "Your home directory usage is $usage, within the limit of $limit."
fiCreate a script that asks the user for a number between 1 and 10 and keeps asking until a valid number is entered.
- Use a
whileloop to repeatedly ask for input. - Use
readto get the user's input.
Example:
#!/bin/bash
while true; do
echo "Please enter a number between 1 and 10:"
read number
if [[ "$number" -ge 1 && "$number" -le 10 ]]; then
echo "Thank you! You entered a valid number: $number"
break
else
echo "Invalid number. Please try again."
fi
doneCreate a script that counts down from a given number of seconds.
- Use a
forloop with a decrementing counter. - Use
sleepto wait for one second in each iteration.
Example:
#!/bin/bash
echo "Enter the number of seconds for the countdown:"
read seconds
for (( i=$seconds; i>=0; i-- )); do
echo "$i"
sleep 1
done
echo "Time's up!"Create a script that renames all .txt files in the current directory to have a .bak extension.
- Use a
forloop to iterate through.txtfiles. - Use
mvto rename the files.
Example:
#!/bin/bash
for file in *.txt; do
mv "$file" "${file%.txt}.bak"
doneCreate a script that displays basic system information such as the hostname, current user, and current date and time.
- Use
hostnameto get the hostname. - Use
whoamito get the current user. - Use
dateto get the current date and time.
Example:
#!/bin/bash
echo "Hostname: $(hostname)"
echo "Current user: $(whoami)"
echo "Current date and time: $(date)"Create a script that replaces a specified string with another string in all .txt files in the current directory.
- Use
sedfor find and replace. - Use a
forloop to iterate through.txtfiles.
Example:
#!/bin/bash
echo "Enter the string to replace:"
read search
echo "Enter the replacement string:"
read replace
for file in *.txt; do
sed -i "s/$search/$replace/g" "$file"
doneCreate a script that generates a random password of a specified length.
- Use
trto create random characters. - Use
/dev/urandomas a source of randomness.
Example:
#!/bin/bash
echo "Enter the desired password length:"
read length
password=$(tr -dc 'A-Za-z0-9' < /dev/urandom | head -c "$length")
echo "Generated password: $password"Create a script that monitors a log file and prints new entries as they are added.
- Use
tail -fto follow the log file.
Example:
#!/bin/bash
echo "Enter the log file to monitor:"
read logfile
if [ -e "$logfile" ]; then
tail -f "$logfile"
else
echo "The file '$logfile' does not exist."
fiCreate a script that checks the size of a specified directory and sends an alert if it exceeds a certain size.
- Use
du -shto get the directory size. - Use an
ifstatement to compare the size with a threshold.
Example:
#!/bin/bash
echo "Enter the directory to check:"
read directory
echo "Enter the size limit (e.g., 100M):"
read limit
size=$(du -sh "$directory" | cut -f1)
if [[ "$size" > "$limit" ]]; then
echo "Alert: The size of '$directory' is $size, which exceeds the limit of $limit."
else
echo "The size of '$directory' is $size, within the limit of $limit."
fiCreate a script that prints the system uptime.
- Use the
uptimecommand to get the system uptime.
Example:
#!/bin/bash
echo "System uptime: $(uptime -p)"Create a script that checks if the internet connection is active by pinging a well-known server.
- Use
pingto check connectivity. - Use an
ifstatement to check the ping result.
Example:
#!/bin/bash
ping -c 1 google.com &> /dev/null
if [ $? -eq 0 ]; then
echo "Internet connection is active."
else
echo "No internet connection."
fiCreate a script that creates a directory structure for a project.
- Use
mkdir -pto create directories recursively.
Example:
#!/bin/bash
echo "Enter the project name:"
read project
mkdir -p "$project"/{src,bin,docs}
echo "Directory structure for '$project' created."Create a script that checks the CPU usage and prints a warning if it exceeds a certain threshold.
- Use
mpstator
top to get CPU usage.
2. Use an if statement to compare the usage with a threshold.
Example:
#!/bin/bash
threshold=80
cpu_usage=$(mpstat 1 1 | awk '/Average/ {print 100 - $12}')
if (( ${cpu_usage%.*} > threshold )); then
echo "Warning: CPU usage is at $cpu_usage%, which exceeds the threshold of $threshold%."
else
echo "CPU usage is at $cpu_usage%, within the threshold of $threshold%."
fiCreate a script that lists all installed packages on your system.
- Use
dpkg -lon Debian-based systems orrpm -qaon Red Hat-based systems.
Example for Debian-based systems:
#!/bin/bash
dpkg -lCreate a script that sends an email alert if a specified directory exceeds a certain size.
- Use
mailorsendmailto send emails. - Use
du -shto get the directory size.
Example:
#!/bin/bash
echo "Enter the directory to check:"
read directory
echo "Enter the size limit (e.g., 100M):"
read limit
echo "Enter the email address to send the alert to:"
read email
size=$(du -sh "$directory" | cut -f1)
if [[ "$size" > "$limit" ]]; then
echo "Alert: The size of '$directory' is $size, which exceeds the limit of $limit." | mail -s "Directory Size Alert" "$email"
else
echo "The size of '$directory' is $size, within the limit of $limit."
fiFeel free to try these tasks and ask if you need further assistance or more advanced tasks!