Interactive file and explorer



description:- we will create a bash script that serves as an interactive file and directory explorer. The script will allow us to explore the files and directories in the current path and provide a character counting feature for the user's input.

The script will have two main parts:-

Part1: File and directory exploration

  1. Upon execution without any command-line arguments, the script will display a welcome message and list all the files and directories in the current path.
  2. For each file and directory, the script will print its name and size in human-readable format (e.g., KB, MB, GB). This information will be obtained using the ls command with appropriate options.
  3. The list of files and directories will be displayed in a loop until the user decides to exit the explorer.

Part2: Character counting

  1. After displaying the file and directory list, the script will prompt the user to enter a line of text.
  2. The script will read the user's input until an empty string is entered (i.e., the user presses Enter without any text).
  3. For each line of text entered by the user, the script will count the number of characters in that line.
  4. The character count for each line entered by the user will be displayed.

Example:

$ ./explorer.sh

Welcome to the Interactive File and Directory Explorer!

Files and Directories in the Current Path:

- file1.txt (100 KB)

- dir1 (2 MB)

- script.sh (3 KB)

...

Enter a line of text (Press Enter without text to exit): Hello, this is a sample line.

Character Count: 27

Enter a line of text (Press Enter without text to exit): Another line to count.

Character Count: 25

Enter a line of text (Press Enter without text to exit):

Exiting the Interactive Explorer. Goodbye!        

Submission Instructions:

  1. Create a bash script named explorer.sh that implements the Interactive File and Directory Explorer as described above.
  2. Add comments in the script to explain the purpose and logic of each part.

Code:

#!/bin/bash

# First line of the script is the shebang which tells the system how to execute

# Part 1: File and Directory Exploration
echo "Welcome to the Interactive File and Directory Explorer "
while true; do
    # List all files and directories in the current path
    echo "Files and Directories in the Current Path:"
    ls -lh

    # Part 2: Character Counting
    read -p "Enter a line of text (Press Enter without text to exit): " input

    # Exit if the user enters an empty string
    if [ -z "$input" ]; then
        echo "Exiting the Interactive Explorer. Goodbye!"
        break
    fi

    # Calculate and print the character count for the input line
    char_count=$(echo -n "$input" | wc -m)
    echo "Character Count: $char_count"
done        


要查看或添加评论,请登录

Rakesh Kumar的更多文章

  • Docker...

    Docker...

    Container: A container is a standard unit of software that packages up code and all its dependencies so the application…

  • Docker..

    Docker..

    What is container? A container is a standard unit of software that packages up code and all its dependencies so the…

  • Git and GitHub...

    Git and GitHub...

    Login aws account----> create two EC2 instance Commands:- sudo su #for admin command apt-get update -y #to update…

  • GIT and GitHub....

    GIT and GitHub....

    Version control system Stages of git/workflow Stages of git and its terminology Introduction: It is the software…

  • Basic of bash scripting

    Basic of bash scripting

    Here we will cover the basics of bash scripting. Task1:Comments In bash scripts, comments are used to add explanatory…

社区洞察

其他会员也浏览了