Okay, here is a Bash CLI script that does that. It uses the standard cat command to display the file content and the time command to measure how long the cat command takes.
#!/bin/bash
# Get the start time (optional, but good for demonstrating manual timing)
# START_TIME=$(date +%s)
# --- Configuration ---
# Use /usr/bin/time for more control over output format,
# or just 'time' for the shell built-in (format might vary).
# /usr/bin/time is generally preferred for consistent scripting.
TIME_COMMAND="/usr/bin/time"
# Format for the time output (used with /usr/bin/time)
# %E: Elapsed real time (H:MM:SS.dd format)
TIME_FORMAT="\n----------------------------------------\nFile display finished.\nTime taken: %E"
# --- Script Start ---
# Check if a file argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <text_file>"
echo "Scrolls the content of <text_file> and reports the time taken."
exit 1
fi
TEXT_FILE="$1"
# Check if the file exists and is readable
if [ ! -r "$TEXT_FILE" ]; then
echo "Error: File not found or not readable: $TEXT_FILE" >&2
exit 1
fi
echo "Starting display of: $TEXT_FILE"
echo "----------------------------------------"
# Use the time command to measure the execution of cat
# The time output is typically sent to standard error (stderr)
# by default, which keeps it separate from the file content (stdout).
# We specify the full path to 'time' to ensure we use the external command
# if available, allowing us to control the output format.
# If /usr/bin/time is not available, this command will fail.
# A fallback could be implemented, but this is common on Linux/macOS.
# Check if /usr/bin/time exists, otherwise use the shell built-in time
if command -v "$TIME_COMMAND" &>/dev/null; then
# Use /usr/bin/time with custom format
# The time output is printed to stderr by default.
"$TIME_COMMAND" -f "$TIME_FORMAT" cat "$TEXT_FILE"
TIME_EXIT_STATUS=$?
else
# Fallback to shell built-in time if /usr/bin/time is not found
# The output format of the built-in time is not controllable.
echo "Warning: $TIME_COMMAND not found. Using shell built-in 'time'. Output format may differ." >&2
time cat "$TEXT_FILE"
TIME_EXIT_STATUS=$? # Built-in time's exit status is the command's status
fi
# The exit status of the `time` command is the exit status of the command it timed (`cat`).
# So we can check if `cat` succeeded.
if [ "$TIME_EXIT_STATUS" -ne 0 ]; then
echo "An error occurred during file display (cat returned $TIME_EXIT_STATUS)." >&2
exit 1
fi
# The time report is printed by the time command itself,
# no need for an extra echo at the end here unless we used manual timing.
exit 0
How to Use:
- Save: Save the code above into a file, for example,
scroll_timed.sh.
- Make Executable: Open your terminal and run:
chmod +x scroll_timed.sh
- Run: Execute the script followed by the path to the text file you want to scroll:
./scroll_timed.sh /path/to/your/textfile.txt
Explanation:
#!/bin/bash: Specifies that the script should be executed with Bash.
if [ "$#" -ne 1 ]; then ... fi: Checks if exactly one command-line argument (the file path) is provided. If not, it prints a usage message and exits.
TEXT_FILE="$1": Assigns the first command-line argument to the variable TEXT_FILE.
if [ ! -r "$TEXT_FILE" ]; then ... fi: Checks if the file exists (-f could also be used, but -r implies existence and readability) and is readable by the script. If not, prints an error and exits.
echo "Starting display of: ..." and echo "--------------------": Prints a message indicating the start of the file display.
command -v "$TIME_COMMAND": Checks if the external /usr/bin/time command is available.
"$TIME_COMMAND" -f "$TIME_FORMAT" cat "$TEXT_FILE": This is the core command.
"$TIME_COMMAND": Executes the time command (either /usr/bin/time or potentially the built-in depending on the check).
-f "$TIME_FORMAT": (Used with external time) Specifies the output format for the timing information. We include newlines and descriptive text.
cat "$TEXT_FILE": This is the command that time will execute and measure. cat reads the file and prints its entire content to standard output, effectively "scrolling" it through the terminal.
- The output of
cat goes to standard output.
- The output of
time (the duration report) goes to standard error by default. This is usually desired as it keeps the timing information separate from the actual file content.
time cat "$TEXT_FILE": This is the fallback if /usr/bin/time is not found. It uses the shell's built-in time command. Its output format is fixed and cannot be customized with -f.
TIME_EXIT_STATUS=$?: Captures the exit status of the last executed command, which in this case is the time command. The time command's exit status is the exit status of the command it ran (cat).
if [ "$TIME_EXIT_STATUS" -ne 0 ]; then ... fi: Checks if the cat command (via the time command's exit status) finished successfully. If not, it prints an error.
- : Exits the script successfully.
When you run this, the contents of the file will be printed to your terminal, and after it finishes (or if you interrupt it with Ctrl+C), the script will print the duration taken by the cat command on a separate line below the file content (sent to standard error).