Bash Debugging Techniques: Exercises & Solutions
1.
Write a Bash script that prints "Hello, world!".
Code:
#!/bin/bash
echo "Hello, world!"
Output:
ad@DESKTOP-3KE0KU4:~$ ./test1.sh Hello, world!
Explanation:
In the exercise above,
- echo "Hello, world!": This command prints the string "Hello, world!" to the standard output. The "echo" command is used to output text to the terminal.
2.
Write a Bash script that assigns a value to a variable and then echoes that value.
Code:
#!/bin/bash
# Assign a value to a variable
my_variable="Hello, world!"
# Echo the value of the variable
echo "$my_variable"
Output:
ad@DESKTOP-3KE0KU4:~$ ./test1.sh Hello, world!
Explanation:
In the exercise above,
- my_variable="Hello, world!": This line assigns the string "Hello, world!" to the variable my_variable.
- echo "$my_variable": This line echoes (prints) the value of 'my_variable' to the standard output. The '$' sign is used to reference the value of a variable.
3.
Write a Bash script that checks if a file named "test.txt" exists in the current directory and prints a message accordingly.
Code:
#!/bin/bash
# Check if "test.txt" exists in the current directory
if [ -f "test.txt" ]; then
echo "File 'test.txt' exists in the current directory."
else
echo "File 'test.txt' does not exist in the current directory."
fi
Output:
ad@DESKTOP-3KE0KU4:~$ ./test1.sh File 'test.txt' does not exist in the current directory.
Explanation:
In the exercise above,
- [ -f "test.txt" ]: This is a conditional expression that checks if "test.txt" exists in the current directory. -f is used to test if the given path is a regular file.
- echo "File 'test.txt' exists in the current directory.": If "test.txt" exists, this message is printed.
- echo "File 'test.txt' does not exist in the current directory.": If "test.txt" does not exist, this message is printed.
4.
Write a Bash script that divides two numbers and prints the result.
Code:
#!/bin/bash
# Define the numbers
numerator=100
denominator=4
# Check if the denominator is not zero
if [ $denominator -ne 0 ]; then
# Perform division
result=$((numerator / denominator))
echo "Result of division: $result"
else
echo "Error: Division by zero!"
fi
Output:
ad@DESKTOP-3KE0KU4:~$ ./test1.sh Result of division: 25
Explanation:
In the exercise above,
- numerator=100 and denominator=4: These lines define the numerator and denominator values.
- [ $denominator -ne 0 ]: This is a conditional expression that checks if the denominator is not zero.
- $((numerator / denominator)): This performs the division operation.
- echo "Result of division: $result": This prints the result of the division.
5.
Write a Bash script that checks if a directory named "workarea" exists and creates it if it doesn't.
Code:
#!/bin/bash
# Check if the directory "workarea" exists
if [ ! -d "workarea" ]; then
# If the directory doesn't exist, create it
mkdir workarea
echo "Directory 'workarea' created successfully."
else
echo "Directory 'workarea' already exists."
fi
Output:
ad@DESKTOP-3KE0KU4:~$ ./test1.sh Directory 'workarea' already exists.
Explanation:
In the exercise above,
- if [ ! -d "workarea" ]; then: This conditional statement checks if the directory "workarea" does not exist.
- mkdir workarea: If the directory doesn't exist, this command creates it.
- echo "Directory 'workarea' created successfully.": This prints a message indicating that the directory has been created.
- else: This part of the conditional statement handles the case where the directory already exists.
- echo "Directory 'workarea' already exists.": This prints a message indicating that the directory already exists.
6.
Write a Bash script that reads a user input and echoes it back.
Code:
#!/bin/bash
# Prompt the user to enter some input
echo "Enter your input:"
# Read the input from the user
read userInput
# Echo back the input
echo "You entered: $userInput"
Output:
ad@DESKTOP-3KE0KU4:~$ ./test1.sh Enter your input: 123 You entered: 123 ad@DESKTOP-3KE0KU4:~$ ./test1.sh Enter your input: Hello You entered: Hello
Explanation:
In the exercise above,
- echo "Enter your input:": This prints a message prompting the user to enter some input.
- read userInput: This command reads the input from the user and stores it in the variable 'userInput'.
- echo "You entered: $userInput": This echoes back the input provided by the user. The variable '$userInput' contains the input entered by the user.
7.
Write a Bash script that prints all the files in the current directory.
Code:
#!/bin/bash
# Print all files in the current directory
for file in *; do
if [ -f "$file" ]; then
echo "$file"
fi
done
Output:
ad@DESKTOP-3KE0KU4:~$ ./test1.sh abc.sh archive.tar.gz archive.zip compressed_files.tar.gz cp_document.txt document.txt erorrrr.log file1.txt
Explanation:
In the exercise above,
- for file in *; do: Iterates over all items (files and directories) in the current directory.
- if [ -f "$file" ]; then: Checks if the current item is a file (-f option).
- echo "$file": Prints the name of the file.
8.
Write a Bash script that appends a line to a file named "output.txt".
Code:
#!/bin/bash
# Append a line to output.txt
echo "11. Line 11 with no number. " >> temp.txt
# Check if appending was successful
if [ $? -eq 0 ]; then
echo "Line appended successfully to output.txt."
else
echo "Failed to append line to output.txt."
fi
Output:
ad@DESKTOP-3KE0KU4:~$ cat temp.txt 1. This is line 1 2. Another line with a digit at the beginning 3. Line starting with a letter 4. 12345 Some more text here 5. Line without a digit at the beginning 6. 67890 More text with a digit 7. Line 7 with some text 8. 42 The answer to everything 9. 9th line with a number 10. Line 10 with no number ad@DESKTOP-3KE0KU4:~$ ./test1.sh Line appended successfully to output.txt. ad@DESKTOP-3KE0KU4:~$ cat temp.txt 1. This is line 1 2. Another line with a digit at the beginning 3. Line starting with a letter 4. 12345 Some more text here 5. Line without a digit at the beginning 6. 67890 More text with a digit 7. Line 7 with some text 8. 42 The answer to everything 9. 9th line with a number 10. Line 10 with no number 11. Line 11 with no number.
Explanation:
In the exercise above,
- echo "This is a new line." >> temp.txt: Appends the specified line to the file "output.txt".
- if [ $? -eq 0 ]; then: Checks the exit status of the previous command. If it's 0 (indicating success), print a success message. Otherwise, it prints a failure message.
9.
Write a Bash script that performs a simple arithmetic operation using user input.
Code:
#!/bin/bash
# Ask the user to input two numbers
echo "Input the first number:"
read num1
echo "Input the second number:"
read num2
# Perform arithmetic operations
sum=$((num1 + num2))
difference=$((num1 - num2))
product=$((num1 * num2))
quotient=$((num1 / num2))
remainder=$((num1 % num2))
# Print the results
echo "Sum: $sum"
echo "Difference: $difference"
echo "Product: $product"
echo "Quotient: $quotient"
echo "Remainder: $remainder"
Output:
ad@DESKTOP-3KE0KU4:~$ ./test1.sh Input the first number: 12 Input the second number: 10 Sum: 22 Difference: 2 Product: 120 Quotient: 1 Remainder: 2
Explanation:
In the exercise above,
- echo "Input the first number:": Displays a prompt for the user to enter the first number.
- read num1: Reads the input from the user and stores it in the variable 'num1'.
- Similarly, the script asks the user to input the second number and reads it into 'num2'.
- Arithmetic operations are performed on 'num1' and 'num2' to calculate the sum, difference, product, quotient, and remainder.
- Finally, the results are printed using "echo".
Bash Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/bash-script-exercises/debugging-techniques.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics