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 ๐ปโจ