0% found this document useful (0 votes)
6 views2 pages

Write A Bash Shell Script That Displays The Follo...

This Bash script provides a command-line interface with options to display the current directory, list files in a specified directory, show today's date and time, check if a file is a simple file or directory, and execute a command. It runs in a loop, allowing users to select options until they choose to exit. The script includes error handling for invalid choices.

Uploaded by

hung.ho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Write A Bash Shell Script That Displays The Follo...

This Bash script provides a command-line interface with options to display the current directory, list files in a specified directory, show today's date and time, check if a file is a simple file or directory, and execute a command. It runs in a loop, allowing users to select options until they choose to exit. The script includes error handling for invalid choices.

Uploaded by

hung.ho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Bash

#!/bin/bash

# Function to display the current directory


display_current_directory() {
echo "Current directory: $(pwd)"
}

# Function to list files in a given directory


list_files() {
echo "Enter the directory name:"
read directory
ls "$directory"
}

# Function to display today's date and time


display_date_time() {
echo "Today's date and time: $(date)"
}

# Function to check if a file is a simple file or directory


check_file_type() {
echo "Enter the file name:"
read filename
if [[ -f "$filename" ]]; then
echo "$filename is a simple file."
elif [[ -d "$filename" ]]; then
echo "$filename is a directory."
else
echo "$filename does not exist."
fi
}

# Function to execute a command line


execute_command() {
echo "Enter the command:"
read command
eval "$command"
}

# Main loop
while true; do
echo
"*********************************************************************
"
echo "Please choose from the following options; type the option"
echo "number and hit the <Enter> key."
echo "a or A To display the current directory"
echo "b or B To list names of the files in a given directory"
echo "c or C To display today's date and time"
echo "d or D To display whether a file is a simple file or
directory"
echo "e or E To execute a command line"
echo "q or Q To exit the program."
echo
"*********************************************************************
"

read -n 1 choice
choice=$(echo "$choice" | tr '[:upper:]' '[:lower:]')

case "$choice" in
a) display_current_directory ;;
b) list_files ;;
c) display_date_time ;;
d) check_file_type ;;
e) execute_command ;;
q) echo "Exiting..."; exit ;;
*) echo "Invalid choice. Please try again." ;;
esac
done

Sources
1. https://github.com/HungNDHE171455/ws2-he171455-hungnd
2. https://github.com/hoangkimphu65/CODE

You might also like