Here are the 50 practical scenarios on how to create simple shell scripts in linux, which can help you prepare for the RHCSA exam:
Basic Shell Scripting:
1. Create a shell script that prints “Hello, World!” to the console.
2. Write a script that displays the current date and time.
3. Create a script that asks the user for their name and then greets them.
4. Write a script that calculates and prints the sum of two numbers provided by the user.
5. Create a script that checks if a file exists in a specific directory.
Variables and Input:
6. Write a script that uses variables to store and display your name, age, and city.
7. Create a script that accepts two numbers as input and multiplies them.
8. Write a script that reads a file name from the user and displays its contents.
9. Create a script that asks the user for their favorite color and prints it.
Conditional Statements:
10. Write a script that checks if a given number is even or odd.
11. Create a script that verifies if a file exists and whether it’s a directory or a regular file.
12. Write a script that compares two numbers and prints the larger one.
13. Create a script that checks if a file is readable and writable.
Loops:
14. Write a script that uses a for loop to print numbers from 1 to 10.
15. Create a script that uses a while loop to count down from 10 to 1.
16. Write a script that lists all files in a directory using a for loop.
17. Create a script that prints the first 10 multiples of a given number using a loop.
Functions:
18. Write a script that defines and calls a simple function.
19. Create a script that calculates the factorial of a number using a function.
20. Write a script that checks if a number is prime using a function.
21. Create a script that converts temperatures between Celsius and Fahrenheit using functions.
File Operations:
22. Write a script that renames a file in a specific directory.
23. Create a script that copies a file from one location to another.
24. Write a script that moves all text files from one directory to another.
25. Create a script that deletes all files with a certain extension in a directory.
String Manipulation:
26. Write a script that reverses a given string.
27. Create a script that counts the number of words in a text file.
28. Write a script that replaces all occurrences of a word in a text file.
29. Create a script that extracts and prints the domain from an email address.
Arrays:
30. Write a script that defines an array of fruits and prints them.
31. Create a script that finds and prints the largest number in an array.
32. Write a script that adds elements to an array and prints its contents.
33. Create a script that sorts an array of numbers in ascending order.
Command Line Arguments:
34. Write a script that accepts a filename as a command-line argument and displays its size.
35. Create a script that performs a mathematical operation on two numbers passed as arguments.
36. Write a script that checks if a file exists based on a filename provided as an argument.
37. Create a script that accepts multiple filenames as arguments and counts the lines in each file.
Error Handling:
38. Write a script that handles errors gracefully when a file is not found.
39. Create a script that validates user input for a positive number.
40. Write a script that checks for the existence of required command-line arguments.
Interactive Scripts:
41. Create a script that provides a menu for performing various tasks.
42. Write a script that prompts the user to enter a password and validates it.
43. Create a script that generates a random number and asks the user to guess it.
File Backup Script:
44. Write a script that creates a backup of a specified directory.
45. Create a script that compresses and archives files older than a certain date.
46. Write a script that rotates log files and keeps a specified number of backups.
System Information:
47. Create a script that displays system information such as hostname, CPU, and memory.
48. Write a script that checks the disk usage and alerts if it exceeds a certain threshold.
49. Create a script that monitors a specific service and restarts it if it’s not running.
50. Write a script that generates a report of logged-in users and their activities.
Note :
These scenarios cover various aspects of shell scripting and can help you practice and build your skills for the RHCSA exam. Be sure to practice writing and running these scripts on a Linux system to gain hands-on experience.