How To Write Bash Scripts In Linux - Complete Guide (Part 16 - Arguments)

How To Write Bash Scripts In Linux - Complete Guide (Part 16 - Arguments)

Introduction to Bash Script Arguments

Understanding Arguments in Bash Scripts

  • The episode focuses on adding arguments to bash scripts, allowing them to perform different actions based on user input.
  • A simple script is introduced that uses the echo command to display a message incorporating an argument passed during execution.
  • The variable $1 represents the first argument provided when running the script, demonstrating how arguments replace variables without explicit declaration.

Multiple Arguments and Their Usage

  • The script can handle multiple arguments, with $1, $2, $3, etc., representing each subsequent argument.
  • An example is given where four personal interests are passed as arguments, showcasing how they are displayed in the output.

Practical Applications of Arguments

Real-world Examples of Argument Use

  • Commands like ls -l /etc illustrate how arguments modify command behavior; here, /etc serves as an argument for listing directory contents.
  • A new example script is created using ls -lh $1, emphasizing that while it may not be practical, it effectively demonstrates argument functionality.

Enhancing Script Utility

  • The importance of arguments is highlighted through potential use cases such as backup scripts that require specific paths as input for operation.

Advanced Scripting Techniques

Counting Directory Objects with Arguments

  • A more complex script counts objects in a specified directory by piping ls -lh $1 into wc -l.
  • This approach provides a count of items within the directory referenced by the user's input, enhancing utility over previous examples.

Error Handling and User Input Validation

  • The discussion shifts to error handling; if no argument is provided, invalid output occurs. This highlights the need for user input validation.

Implementing Input Checks

Ensuring Correct Argument Count

  • An if statement checks whether exactly one argument has been supplied using $#, which counts provided arguments.
  • If conditions aren't met (e.g., no or multiple arguments), informative messages guide users on correct usage and exit codes are managed accordingly.

Conclusion and Next Steps

Preparing for Future Lessons

  • The next lesson will focus on creating a backup script requiring user-defined paths as arguments, reinforcing learned concepts about scripting with inputs.
Video description

In this episode of the Bash Scripting series, you’ll learn how to use arguments in your Bash scripts to make them more dynamic and useful. Instead of debating over systemd, desktop environments, or text editors, invest your time in mastering script input! You’ll discover how to pass and handle arguments in Bash, enabling your scripts to adapt to different scenarios and user input. This essential skill will boost your scripting efficiency and help you write more flexible and powerful Linux scripts. Note: This video was re-rendered and re-uploaded due to corruption and misaligned highlights. As a result, comments from the previous version will not be shown here. --- Support Linux Learning (commission earned) --- - Become a channel member here on YouTube ➜ https://learnlinux.link/join - Become a Patron on Patreon ➜ https://learnlinux.link/patron - Receive a 5% discount on an LPI exam voucher ➜ https://learnlinux.link/lpi-voucher - Set up your own cloud server with Akamai Connected Cloud ➜ https://learnlinux.link/akamai SPOIL YOURSELF WITH A LINUX-RELATED GIFT - Mastering Ubuntu Server 4th Edition (Jay wrote that!!! 😲) ➜ http://ubuntuserverbook.com - Affiliate store for Linux compatible hardware/accessories ➜ https://learnlinux.link/amazon - Awesome KVM for your Homelab ➜ https://learnlinux.link/tinypilot --- Individual Sections --- 00:00 - Intro 00:47 - A simple example of using an argument in Bash 02:50 - Using multiple arguments in Bash 04:15 - An even simpler example of using an argument in Bash 06:09 - An example Bash Script that counts the number of items in a directory 10:09 - Making sure arguments are provided in your script *OTHER BASH SCRIPTING SERIES EPISODES* - Class 01 ➜ Introduction: https://linux.video/bash1 - Class 02 ➜ Hello World: https://linux.video/bash2 - Class 03 ➜ Variables: https://linux.video/bash3 - Class 04 ➜ Math Functions: https://linux.video/bash4 - Class 05 ➜ If Statements: https://linux.video/bash5 - Class 06 ➜ Exit Codes: https://linux.video/bash6 - Class 07 ➜ While Loops: https://linux.video/bash7 - Class 08 ➜ Universal Update Script: https://linux.video/bash8 - Class 09 ➜ For Loops: https://linux.video/bash9 - Class 10 ➜ Where To Store Your Scripts: https://linux.video/bash10 - Class 11 ➜ Data Streams: https://linux.video/bash11 - Class 12 ➜ Functions: https://linux.video/bash12 - Class 13 ➜ Case Statements: https://linux.video/bash13 - Class 14 ➜ Scheduling Jobs (Part 1): https://linux.video/bash14 - Class 15 ➜ Scheduling Jobs (Part 2): https://linux.video/bash15 - Class 16 ➜ Arguments: https://linux.video/bash16 - Class 17 ➜ Building a Backup Script: https://linux.video/bash17 - Class 18 ➜ Closing: https://linux.video/bash18 --- Official *🌐 LEARN LINUX TV ON THE WEB* --- - Main site ➜ https://www.learnlinux.tv - Community ➜ https://community.learnlinux.tv --- FAQ --- - Which distro do I use? ➜ https://learnlinux.link/mydistro - My recording gear (commissions earned) ➜ https://learnlinux.link/recording-stuff Learn Linux TV provides technical content that will hopefully be helpful to you and teach you something new. However, this content is provided without any warranty (expressed or implied). Learn Linux TV is not responsible for any damages that may arise from any use of this content. The person viewing Learn Linux TV's content is expected to follow their best judgement and to make their best decisions while working with any related technology. Always make sure you have written permission before working with any infrastructure. Also, be sure that you're compliant with all company rules, change control procedures, and local laws. #linuxtutorial #bashtutorial #commandline