Introduction
In this challenge, you will learn how to create a simple shell script that uses conditional statements to execute code based on certain conditions. This is a fundamental skill required for the RHCSA exam, as it allows you to write more complex and dynamic scripts that can adapt to different scenarios.
Create a Conditional Shell Script
In this challenge, you will learn how to create a simple shell script that uses conditional statements to execute code based on certain conditions.
Tasks
- Create a shell script that checks the number of command-line arguments provided.
- If the script is called with no arguments, print a message indicating that the script requires at least one argument.
- If the script is called with one argument, print a message indicating that the script was called with one argument.
- If the script is called with more than one argument, print a message indicating that the script was called with multiple arguments.
Requirements
- The shell script should be named
conditional_script.shand should be located in the~/projectdirectory. - You can use
nanoto edit the script. - The script should use the
if-elif-elseconstruct to handle the different cases. - The script should use the
testcommand or the[]syntax to check the number of arguments. - The script should use
echoorprintfto print the appropriate messages.
Example
sudo chmod +x ~/project/conditional_script.sh
$ ~/project/conditional_script.sh
This script requires at least one argument.
$ ~/project/conditional_script.sh one
This script was called with one argument.
$ ~/project/conditional_script.sh one two three
This script was called with multiple arguments.
Summary
In this challenge, you learned how to create a simple shell script that uses conditional statements to execute code based on the number of command-line arguments provided. This is an essential skill for the RHCSA exam, as it allows you to write more dynamic and adaptable scripts that can handle different scenarios. By completing this challenge, you have demonstrated your ability to use if-elif-else constructs, the test command, and the [] syntax to create a conditional shell script.



