Practice Exercise: Exploring the Bash Shell and Basic Scripting
Objectives
- Learn basic shell scripting concepts.
- Create and run simple Bash scripts.
Scenario
Understanding the Bash shell and its scripting capabilities is crucial for efficiently managing and automating tasks on a Linux system. In this exercise, you'll explore the Bash shell's features and create your own basic scripts.
Tasks
Task 1: Writing Your First Bash Script
- Using a text editor (like
nano
orvim
), create a Bash script namedhello.sh
. - Write a simple script that prints "Hello, World!" to the terminal.
- Save the script and give it execute permissions using
chmod +x hello.sh
. - Run the script and verify that it displays the message.
[intern@intern-a1t-inf-lnx1 ~]$ vim hello.sh [intern@intern-a1t-inf-lnx1 ~]$ cat hello.sh #!/bin/bash echo Hello, World! [intern@intern-a1t-inf-lnx1 ~]$ chmod +x hello.sh [intern@intern-a1t-inf-lnx1 ~]$ ./hello.sh Hello, World!
Task 2: Accepting User Input
- Modify the
hello.sh
script to accept user input. - Prompt the user for their name.
- Use the input to personalize the greeting message.
- Run the modified script and test it with different names.
[intern@intern-a1t-inf-lnx1 ~]$ cat hello.sh #!/bin/bash read -p "Enter your name: " NAME echo Hello, $NAME! [intern@intern-a1t-inf-lnx1 ~]$ ./hello.sh Enter your name: intern Hello, intern! [intern@intern-a1t-inf-lnx1 ~]$ ./hello.sh Enter your name: another_name Hello, another_name!
Task 3: Conditional Statements
- Create a new Bash script named
guess_number.sh
. - Write a script that generates a random number between 1 and 10.
- Prompt the user to guess the number.
- Implement conditional statements to check if the guess is correct or not.
- Provide feedback to the user based on their guess.
- Test the script with various inputs.
#!/bin/bash SECRET_NUMBER=$((1 + RANDOM % 10)) read -p "Guess the number (between 1 and 10): " USER_GUESS if [ "$USER_GUESS" -eq "$SECRET_NUMBER" ]; then echo "Congratulations! You guessed the correct number: $SECRET_NUMBER." else echo "Sorry, that's not correct. The secret number was: $SECRET_NUMBER." fi [intern@intern-a1t-inf-lnx1 ~]$ chmod +x guess_number.sh [intern@intern-a1t-inf-lnx1 ~]$ ./guess_number.sh Guess the number (between 1 and 10): 9 Sorry, that's not correct. The secret number was: 2. [intern@intern-a1t-inf-lnx1 ~]$ ./guess_number.sh Guess the number (between 1 and 10): 9 Congratulations! You guessed the correct number: 9.
Task 4: Looping Constructs
- Enhance the
guess_number.sh
script by adding a loop. - Allow the user to make multiple guesses until they guess the correct number.
- Keep track of the number of attempts.
- Display a congratulatory message when the correct guess is made.
- Test the script to see how many attempts it takes to guess correctly.
[intern@intern-a1t-inf-lnx1 ~]$ cat guess_number.sh #!/bin/bash SECRET_NUMBER=$((1 + RANDOM % 10)) ATTEMPTS=0 while true; do read -p "Guess the number (between 1 and 10): " USER_GUESS ((ATTEPMTS++)) if [ "$USER_GUESS" -eq "$SECRET_NUMBER" ]; then echo "Congratulations! You guessed the correct number: $SECRET_NUMBER." echo "It took you $ATTEMPTS attempts." break else echo "Sorry, that's not correct." fi done [intern@intern-a1t-inf-lnx1 ~]$ ./guess_number.sh Guess the number (between 1 and 10): 1 Sorry, that's not correct. Guess the number (between 1 and 10): 2 Sorry, that's not correct. Guess the number (between 1 and 10): 3 Sorry, that's not correct. Guess the number (between 1 and 10): 4 Sorry, that's not correct. Guess the number (between 1 and 10): 5 Congratulations! You guessed the correct number: 5. It took you 0 attempts.
Task 5: Script Documentation
- Add comments and documentation to your Bash scripts to explain their purpose and usage.
- Include your name, a description, and any necessary instructions.
- Ensure that someone else can understand and use your scripts based on the documentation.
[intern@intern-a1t-inf-lnx1 ~]$ cat guess_number.sh #!/bin/bash # Generates a random number between 1 - 10 SECRET_NUMBER=$((1 + RANDOM % 10)) # Stores the number of attempts to a variable ATTEMPTS=0 # Loops unless there is a break statement while true; do # Ask the user for their guess read -p "Guess the number (between 1 and 10): " USER_GUESS # Increment the attempts ((ATTEPMTS++)) # Checks if the user guess is right or wrong if [ "$USER_GUESS" -eq "$SECRET_NUMBER" ]; then echo "Congratulations! You guessed the correct number: $SECRET_NUMBER." echo "It took you $ATTEMPTS attempts." break else echo "Sorry, that's not correct." fi done
Conclusion
By completing these exercises, you've gained practical experience with the Bash shell and basic scripting. You've created and customized your own Bash scripts, accepted user input, used conditional statements and loops, and explored various methods of script execution. These skills are fundamental for Linux system administration and automation.