In this lesson, you’ll learn how to capture user inputs in a shell script. Two common approaches exist for obtaining the mission name: via command line arguments or interactively using the read command. Choosing the right method depends on your use case and scripting needs. Below, you’ll find detailed explanations and examples for each approach.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Using Command Line Arguments
One approach is to pass the mission name as a command line argument when executing the script. The script retrieves the first argument (i.e., $1) and uses it as the mission name. This method is especially useful when you want the script to run without manual intervention.Prompting for Input with the read Command
For scenarios requiring interactive input, theread command is an effective option. The following examples illustrate two variations: one without a custom prompt and another with a clear, guiding message.
Without a Custom Prompt
When you useread without the -p option, the script simply waits for input without displaying any guidance. This approach is straightforward but may be less user-friendly.
With a Custom Prompt
To improve clarity, you can add a custom message using the-p option with the read command. This modification makes it evident what input is expected.
mission_name variable and subsequently used in the launch sequence commands.
For enhanced usability, consider designing your script to support both methods. If a command line argument is provided, use it. Otherwise, prompt the user interactively. This approach offers flexibility and makes your script suitable for both manual and automated scenarios.
Choosing the Appropriate Method
When developing your script, consider the following:| Scenario | Recommended Approach |
|---|---|
| Running unattended or automating processes | Command Line Arguments |
| Requiring user confirmation (e.g., deleting files) | Interactive Input with a Custom Prompt |
| Supporting both manual and automated execution | A hybrid approach: check for command line arguments and prompt if missing |
- For simple scripts that rely on manual user input, using the
readcommand with a custom prompt is ideal. - For scripts designed to operate without human intervention or to be called by other scripts, command line arguments provide a seamless and efficient solution.
Summary
In this session, you learned to capture inputs in shell scripts using two methods:- Command line arguments for unattended runs.
- The
readcommand (with and without a custom prompt) for interactive user input.