Linux Input Reading

LinuxLinuxBeginner
Practice Now

Introduction

In the age of dinosaurs, the world is a perilous place where survival is a constant struggle. You are a fearless dinosaur hunter, equipped with extensive knowledge and skills to navigate the treacherous landscape in search of the most elusive and dangerous creatures.

Your mission is to harness the power of the Linux system's input reading capabilities to track the movements of a fearsome T-Rex through the dense wilderness. By mastering the read skill, you will uncover vital clues and gain insight into the ancient creature's behavior, allowing you to anticipate its next move and outmaneuver it in this thrilling adventure.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") subgraph Lab Skills linux/read -.-> lab-271367{{"`Linux Input Reading`"}} end

Setting up Input Reading

In this step, you will begin by setting up the environment for reading input in a bash script.

  1. Create a script file named input_reader.sh in the /home/labex/project directory.

    touch /home/labex/project/input_reader.sh
  2. Open the input_reader.sh file for editing.

    nano /home/labex/project/input_reader.sh
  3. In the input_reader.sh script, add the following code to read user input and display it.

    #!/bin/bash
    echo "Enter your observation: "
    read observation
    echo "Your observation: $observation"
  4. Save and close the file. Add the following code to the end of the script to run it when the script is executed.

    chmod +x /home/labex/project/input_reader.sh
    /home/labex/project/input_reader.sh

    Output:

    labex:project/ $ chmod +x /home/labex/project/input_reader.sh
    /home/labex/project/input_reader.sh
    Enter your observation:
    labex
    Your observation: labex

Enhancing Input Reading

In this step, you will enhance the input reading script to handle multiple inputs and provide feedback.

  1. Modify the input_reader.sh script to include a loop that allows multiple observations to be entered.

    #!/bin/bash
    echo "Enter your observations (press Q or q to quit): "
    while true; do
      read observation
      if [[ "$observation" == [Qq] ]]; then
        echo "Exiting input mode."
        break
      fi
      echo "Observation: $observation"
    done
  2. Test the script by running it and entering multiple observations. Press Q or q to exit the input mode.

    labex:project/ $ /home/labex/project/input_reader.sh
    Enter your observations (press Q or q to quit):
    labex
    Observation: labex
    skill-labs
    Observation: skill-labs
    q
    Exiting input mode.

Summary

In this lab, we have designed a captivating scenario that transports learners into a prehistoric world where they embody the role of a daring dinosaur hunter. By leveraging the Linux read skill, students will gain hands-on experience in reading user input and developing interactive scripts. This interactive learning journey equips them with essential skills for script development and user interaction in the Linux environment, fostering a deeper understanding and appreciation of the power of input reading.

Other Linux Tutorials you may like