Practice Exercise: Advanced Bash Shell Scripting Techniques
Objectives
- Explore advanced Bash shell scripting techniques.
- Learn how to use arrays, functions, and advanced scripting concepts.
- Practice creating complex Bash scripts to solve real-world problems.
Scenario
In this exercise, you will dive deeper into advanced Bash shell scripting techniques. You will explore topics like arrays, functions, command-line arguments, and error handling. These techniques are valuable for creating robust and efficient Bash scripts.
Tasks
Task 1: Working with Arrays
- Create a Bash script named
array_operations.sh
. - Define an array that stores the names of your favorite fruits.
- Write a loop to iterate through the array and display each fruit's name.
- Implement an array operation to add a new fruit to the list.
- Modify the script to remove a specific fruit from the array.
- Test the array operations and ensure they work correctly.
#!/bin/bash # Define an array of favorite fruits fruits=("Apple" "Banana" "Cherry" "Grape" "Orange") # Function to display all fruits in the array display_fruits() { echo "My favorite fruits are:" for fruit in "${fruits[@]}"; do echo "- $fruit" done } # Function to add a new fruit to the array add_fruit() { local new_fruit="$1" fruits+=("$new_fruit") echo "$new_fruit has been added to the list of favorite fruits." } # Function to remove a specific fruit from the array remove_fruit() { local fruit_to_remove="$1" for index in "${!fruits[@]}"; do if [ "${fruits[$index]}" == "$fruit_to_remove" ]; then unset fruits[$index] echo "$fruit_to_remove has been removed from the list of favorite fruits." return fi done echo "$fruit_to_remove was not found in the list of favorite fruits." } # Display the initial list of fruits display_fruits # Add a new fruit to the list add_fruit "Mango" # Display the updated list of fruits display_fruits # Remove a specific fruit from the list remove_fruit "Cherry" # Display the final list of fruits display_fruits
[intern@intern-a1t-inf-lnx1 ~]$ chmod +x array_operations.sh [intern@intern-a1t-inf-lnx1 ~]$ ./array_operations.sh My favorite fruits are: - Apple - Banana - Cherry - Grape - Orange Mango has been added to the list of favorite fruits. My favorite fruits are: - Apple - Banana - Cherry - Grape - Orange - Mango Cherry has been removed from the list of favorite fruits. My favorite fruits are: - Apple - Banana - Grape - Orange - Mango
Task 2: Implementing Functions
- Create a Bash script named
math_functions.sh
. - Write a Bash function that calculates the square of a given number.
- Implement another function that finds the factorial of a positive integer.
- Call these functions with different input values and display the results.
- Document your functions and their usage within the script.
#!/bin/bash # Function to calculate the square of a number calculate_square() { local number=$1 local square=$((number * number)) echo "The square of $number is: $square" } # Function to calculate the factorial of a positive integer calculate_factorial() { local n=$1 local factorial=1 if [ $n -lt 0 ]; then echo "Factorial is not defined for negative numbers." return fi for ((i = 1; i <= n; i++)); do factorial=$((factorial * i)) done echo "The factorial of $n is: $factorial" } # Example usages of the functions calculate_square 5 calculate_square 7 calculate_factorial 5 calculate_factorial 0 calculate_factorial -3
[intern@intern-a1t-inf-lnx1 ~]$ chmod +x math_functions.sh [intern@intern-a1t-inf-lnx1 ~]$ ./math_functions.sh The square of 5 is: 25 The square of 7 is: 49 The factorial of 5 is: 120 The factorial of 0 is: 1 Factorial is not defined for negative numbers.
Task 3: Command-Line Arguments
- Create a Bash script named
command_line_args.sh
. - Write a script that accepts command-line arguments.
- Use positional parameters to capture and display the arguments.
- Implement error handling to check if the correct number of arguments is provided.
- Provide a usage message when incorrect usage is detected.
#!/bin/bash # Check the number of command-line arguments if [ $# -eq 0 ]; then echo "Usage: $0 <arg1> <arg2> ..." exit 1 fi # Display the provided command-line arguments echo "You provided the following arguments:" # Loop through and display each argument for arg in "$@"; do echo "$arg" done
[intern@intern-a1t-inf-lnx1 ~]$ chmod +x command_line_args.sh
[intern@intern-a1t-inf-lnx1 ~]$ ./command_line_args.sh
Usage: ./command_line_args.sh <arg1> <arg2> ...
[intern@intern-a1t-inf-lnx1 ~]$ ./command_line_args.sh echo gg
You provided the following arguments:
echo
gg
Task 4: Combining Techniques
- Create a Bash script named
advanced_script.sh
. - Combine the techniques you've learned in previous tasks to create a comprehensive script.
- Use arrays to store data, functions to perform calculations, and command-line arguments for customization.
- Implement error handling to deal with potential issues.
- Test the script with various inputs to ensure it works as expected.
[intern@intern-a1t-inf-lnx1 ~]$ cat advance_script.sh #!/bin/bash # Function to calculate the square of a number calculate_square() { local num=$1 local square=$((num * num)) echo "The square of $num is $square" } # Function to find the factorial of a positive integer calculate_factorial() { local num=$1 local factorial=1 if [ $num -lt 0 ]; then echo "Error: Factorial is undefined for negative numbers." return 1 fi for ((i = 1; i <= num; i++)); do factorial=$((factorial * i)) done echo "The factorial of $num is $factorial" } # Check the number of command-line arguments if [ $# -lt 2 ]; then echo "Usage: $0 <square|factorial> <value>" exit 1 fi # Parse command-line arguments operation="$1" value="$2" case "$operation" in "square") calculate_square "$value" ;; "factorial") calculate_factorial "$value" ;; *) echo "Invalid operation. Supported operations: square, factorial." exit 1 ;; esac
[intern@intern-a1t-inf-lnx1 ~]$ chmod +x advance_script.sh [intern@intern-a1t-inf-lnx1 ~]$ ./advance_script.sh Usage: ./advance_script.sh <square|factorial> <value> [intern@intern-a1t-inf-lnx1 ~]$ ./advance_script.sh asdf 3 Invalid operation. Supported operations: square, factorial. [intern@intern-a1t-inf-lnx1 ~]$ ./advance_script.sh factorial -1 Error: Factorial is undefined for negative numbers. [intern@intern-a1t-inf-lnx1 ~]$ ./advance_script.sh factorial 4 The factorial of 4 is 24 [intern@intern-a1t-inf-lnx1 ~]$ ./advance_script.sh square -2 The square of -2 is 4
Conclusion
In this exercise, you've explored advanced Bash shell scripting techniques, including arrays, functions, command-line arguments, and error handling. These skills are essential for creating complex and reliable Bash scripts to solve real-world problems. Continue to practice and refine your scripting abilities for more advanced tasks.