input and returning the input: output. Here is the assignment

In the session, provide a command prompt that includes the working directory, e.g., $./logger /home/it244/it244/hw8$

Accept user’s commands, execute them, and display the output on the screen.

During the session, create a temporary file “PID.cmd” (PID is the process ID) to store the command history in the following format (index: command):

1: ls

2: ls -l

If the script is aborted by CTRL+C (signal 2), output a message “aborted by ctrl+c”.

When you quit the logging session (either by “exit” or CTRL+C),

a. Delete the temporary file

b. Print out the total number of the commands in the session and the numbers of successful/failed commands (according to the exit status).

here is my code so far (which did not go well, I would not try to run it)

#!/bin/sh
  trap 'exit 1' 2
  trap 'ctrl-c' 2

  echo $(pwd)
  while true
  do
    read -p command
    echo "$command:" $command >> PID.cmd
 done

Dani AI

Generated

A few concrete fixes and a compact example that follow the assignment requirements.

The original errors from come from mixing POSIX /bin/sh with bash-only features (for example read -p and ((...))), an incorrect string test, and overwriting the history file instead of appending. Use a bash shebang, create the history file named with the script PID ($$.cmd), trap SIGINT to print the required "aborted by ctrl+c" message, and trap EXIT to remove the temp file and print the summary (total / success / failed). Keep the prompt showing the working directory, append each command as "index: command", execute the command in a subshell and check its exit status.

Example (minimal, original) implementation:

#!/usr/bin/env bash

logfile="$$.cmd"
count=0; ok=0; fail=0

on_int() {
  printf 'aborted by ctrl+c\n'
  exit 130
}
on_exit() {
  rm -f "$logfile"
  printf 'Session summary: total=%d, success=%d, failed=%d\n' "$count" "$ok" "$fail"
}

trap on_int INT
trap on_exit EXIT

while true; do
  printf '%s$ ' "$PWD"
  IFS= read -r line || break
  [[ -z "$line" ]] && continue
  [[ "$line" == exit ]] && break

  count=$((count+1))
  printf '%d: %s\n' "$count" "$line" >> "$logfile"

  bash -c -- "$line"
  rc=$?
  if (( rc == 0 )); then ok=$((ok+1)); else fail=$((fail+1)); fi
done

Notes and troubleshooting

  • Avoid #!/bin/sh if using read -p, (( )), or [[ ]]; use #!/usr/bin/env bash. The earlier "read: arg count" error is consistent with a shell that lacks -p.
  • Quote tests and variables: use [[ "$line" == exit ]] (or [ "$line" = "exit" ]) to detect the quit command reliably.
  • Use >> to append history lines; > will clobber the file each loop.
  • Running the typed string with bash -c -- "$line" preserves arguments and quoting better than naive word-splitting; avoid careless eval unless understood.

This addresses the issues shown in the thread and produces the required PID.cmd, per-command indexing, run/output behavior, SIGINT handling, and final cleanup/summary.

I currently only receive this output

command read: 10: arg count

what is not allowing me to input the commands.

Ok I made some progress not quite working all the way it doesnt like my bashtrap or incremental index

#!/bin/sh
    index=0
    trap bashtrap INT
    bashtrap(){
         echo "CTRL+C aborting bash script"
    }
    echo "starting to log"
    while : 
    do
        read -p "command:" inputline
        if [ $inputline="exit" ]
        then
                echo "Aborting with Exit"
                break
        else
                echo "$index: $inputline" > output
                $inputline 2>&1 | tee output
                (( index++ ))   
        fi
    done
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.