๐ŸŒŸ Bash Challenge Bonanza ๐Ÿš€

๐ŸŒŸ Bash Challenge Bonanza ๐Ÿš€

ยท

4 min read

Ready to flex your bash scripting muscles? Dive into these fun bash challenges, each designed to sharpen your skills and give you hands-on experience. Let's tackle these with the power of scripting! ๐Ÿ’ช๐Ÿ”

๐ŸŒŸBash Challenge #1: Print the number of CPU cores

Create a bash script named cores.sh that prints out the number of CPU cores you have in your system.

Hint: Use the nproc command.๐Ÿ–ฅ๏ธ

๐ŸŒŸBash Challenge #2: Print Calendar of a given year

Create a bash script named cal. sh that would display the calendar of a given year.

The script would prompt the user to enter a year; then, it would display the corresponding yearโ€™s calendar.

Hint: Use the cal command.

In this challenge covers How to use variables and data types.

๐ŸŒŸBash Challenge #3: Convert Case

Create a bash script named upper. sh that would display file content in upper case letters.

The file path must be passed to the script as an argument.

Hint: Use the tr command.

In this challenge covers How to use Arguments in scripts.

.๐ŸŒŸBash Challenge #4: Sort an Array

Consider the following sorted.sh bash script:

#!/bin/bashnum=(1 2 3 5 4)echo "Before sorting array num: "echo ${num[@]}You_code_goes_hereecho "After sorting array num: "echo ${num[@]}

You need to edit the sorted. sh script so that the num array becomes sorted as you can see in the following output:

elliot@allsafe:~/scripts$ ./sorted.shBefore sorting array num:1 2 3 5 4After sorting array num:1 2 3 4 5

Restrictions: You are only allowed to add and delete elements from the array.

Hint: Swap the last two elements of the array; use unset to delete the fourth element of the array and then add it back!

In this challenge covers How to use an array in the script.

๐ŸŒŸBash Challenge #5: Calculate net salary

Create a bash script named salary. sh that would calculate the total net salary of an employee. The script would prompt the user to enter a monthly gross salary (before) and a tax rate (in percentage). Finally, the script would calculate and output the total net annual salary (after tax).

Hint: Use the bc command to handle decimals.

This challenge covers How to use Arithmetic operations in the script.

๐ŸŒŸBash Challenge #6: Remove Asterisks from String

Create a bash script named trim. sh that would do the following:

โ€ข Ask the user to enter a string with asterisks.

โ€ข Remove all asterisks (*) in the string.

โ€ข Change all the letters in the string to uppercase.

โ€ข Output the updated string to the terminal.

Restriction: You are only allowed to use the echo command!

Hint: Use the escape character with the asterisk!

This challenge covers How to use string operations

๐ŸŒŸBash Challenge #7: Leap Year

Create a bash script named isleap.sh that would determine whether or not a given year is a leap year. A year is a leap year if one of the following conditions is satisfied:

โ€ข Year is multiple of 400.

โ€ข Year is multiple of 4 and not multiple of 100.

You can pass the year as an argument to your script.

Hint: Use the remainder operator.

This challenge covers How to use Decision-making in script

๐ŸŒŸBash Challenge #8: Ping a bunch of servers

Create a bash script named subnet. sh that would ping every single server in the 23.227.36.x subnet where x is a number between 0 and 255. If a ping succeeded, display the statement โ€œServer 23.227.36.x is up and runningโ€. Otherwise, if a ping

failed, display the statement โ€œServer 23.227.36.x is unreachable.โ€

Hint: Use a for loop and the ping command.

This challenge covers How to use loops in the scripts

๐ŸŒŸBash Challenge #9: Calculate the GCD

Create a script named gcd. sh that will calculate the greatest common divisor of two numbers: num1 and num2 (passed as script arguments).

In mathematics, the greatest common divisor of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers.

In your script, create a function named common_divisor that tests whether a number evenly divides num1 and num2.

Hint: Remainders and loops are your best friends!

The challenge covers How to use functions in the scripts

๐ŸŒŸBash Challenge #10: Automatic Downtime Monitor

Create a bash script named outage. sh that would automatically detect whether a server in your environment is down! Store all your server hostnames that you want to monitor in a file named servers.txt as follows:

elliot@allsafe:~/scripts$ cat servers.txt 
server1
server2
server3
server4
server5

If there is a server outage, send an email to yourself specifying which server is unreachable.

Your script should run every 2 hours.

Hint: Use mail and cron.

This challenge covers How to use the cron command to schedule specific scripts run automatically in a given time

These challenges will level up your bash scripting game, covering variables, data types, functions, and more. Happy scripting!

If this post was helpful, please do follow and click the like button below to show your support ๐Ÿ˜„

Thank you for reading๐Ÿ’š

Sprasad ๐ŸŒปโœจ

ย