What you will build
Write a script named stamp.sh in your lab home directory. It reads standard input one line at a time. For each line it writes the current time in the form HH:MM:SS, a space, and then the line itself, all to standard output. The script must keep the line exactly as it was, including leading spaces and backslashes, so it does not split or trim the text.
The script should accept an optional file name. When a file is given, it reads that file instead of standard input. If the file cannot be read, it prints an error to standard error and exits with status 1. With no argument and nothing on standard input, it exits with status 0 and prints nothing.
Test cases
A good logger is defined by its edge cases, so write the tests before the script. The first test is a normal three-line input, where each output line should begin with a timestamp followed by the original text. The second test is a line with leading spaces, which must keep them after the timestamp. The third test is a missing file, where the status must be 1 and the error must go to standard error, not to the output.
Run each test in a pipeline and check the result. Use printf to generate the input, because it gives exact control over every byte, including tabs and newlines.
printf 'first\n indented\nlast\n' | bash stamp.shA correct run prints three lines, each starting with a time such as 10:04:05. The second line still begins with two spaces after the timestamp, which proves that the script did not trim the input.
Design notes
The core of the script is a loop that reads lines. Use while IFS= read -r line. The empty IFS keeps leading and trailing spaces, and -r stops read from treating backslashes as escapes. Without these two details the logger silently changes the data it is supposed to record.
For the timestamp, use date +%H:%M:%S. Call date once per line, which is simple and fast enough for a learning build. Print the line with printf rather than echo, because echo handles backslashes differently across shells, and printf with an explicit format is predictable.
Send the usage and file errors to standard error with >&2. The data stream must stay clean, because the logger may be placed in the middle of a pipeline.
Handling standard input and a file argument
The logger has two sources of data, standard input and an optional file. The cleanest design chooses the source once, at the top of the script, and then runs one loop that reads from it. Using the path /dev/stdin as the default source keeps the loop identical in both cases, because the redirection at the end of the loop works for a file name as well.
Keep the argument check simple. Test that the file is readable before the loop starts, so a bad name fails with a clear message and status 1 rather than producing half an output stream. A pipeline that reads from the logger expects complete lines, and a partial run would be worse than none.
A reference solution
The reference below is one correct version. Try your own before reading it, and compare the output on the three test cases.
#!/bin/bash
in=/dev/stdin
if [ $# -ge 1 ]; then
if [ ! -r "$1" ]; then
echo "cannot read $1" >&2
exit 1
fi
in=$1
fi
while IFS= read -r line; do
printf '%s %s\n' "$(date +%H:%M:%S)" "$line"
done < "$in"The script defaults to standard input, checks the file when one is given, and reads through the chosen source with a redirection at the end of the loop. The printf format keeps the line intact, and the timestamp comes from date. Errors go to standard error and set the status.
Checking your work
Run the three tests and note the exit status of each one with echo $?. Confirm that the error case writes nothing to standard output by sending standard output to a file and checking its size. Finally, put the logger in a pipeline after a command that produces output, such as ls, and watch the timestamps appear. A logger that passes three tests and handles a pipe is ready to use.