Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6855977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:49:36+00:00 2026-05-27T01:49:36+00:00

I am trying to do a homework assignment and it is very confusing. I

  • 0

I am trying to do a homework assignment and it is very confusing. I am not sure if the professor’s example is in Perl or bash, since it has no header. Basically, I just need help with the meat of the problem: capturing the input and outputting it. Here is the assignment:

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

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

  3. 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

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

  5. 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

Currently when I run this script I get

command read: 10: arg count

What is causing that?

======UPDATE=========

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:49:36+00:00Added an answer on May 27, 2026 at 1:49 am

    Since the example posted used sh, I’ll use that in my reply. You need to break down each requirement into its specific lines of supporting code. For example, in order to “provide a command prompt that includes the working directory” you need to actually print the current working directory as the prompt string for the read command, not by setting the $PS variable. This leads to a read command that looks like:

    read -p "`pwd -P`\$ " _command
    

    (I use leading underscores for private variables – just a matter of style.)

    Similarly, the requirement to do several things on either a trap or a normal exit suggests a function should be created which could then either be called by the trap or to exit the loop based on user input. If you wanted to pretty-print the exit message, you might also wrap it in echo commands and it might look like this:

    _cleanup() {
       rm -f $_LOG
       echo
       echo $0 ended with $_success successful commands and $_fail unsuccessful commands.
       echo
       exit 0
    }
    

    So after analyzing each of the requirements, you’d need a few counters and a little bit of glue code such as a while loop to wrap them in. The result might look like this:

    #/usr/bin/sh
    
    # Define a function to call on exit
    _cleanup() {
       # Remove the log file as per specification #5a
       rm -f $_LOG
    
       # Display success/fail counts as per specification #5b
       echo
       echo $0 ended with $_success successful commands and $_fail unsuccessful commands.
       echo
       exit 0
    }
    
    # Where are we?  Get absolute path of $0
    _abs_path=$( cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P )
    
    # Set the log file name based on the path & PID
    # Keep this constant so the log file doesn't wander
    # around with the user if they enter a cd command
    _LOG=${_abs_path}/$$.cmd
    
    # Print ctrl+c msg per specification #4
    # Then run the cleanup function
    trap "echo aborted by ctrl+c;_cleanup" 2
    
    # Initialize counters
    _line=0
    _fail=0
    _success=0
    
    while true
    do
       # Count lines to support required logging format per specification #3
       ((_line++))
       # Set prompt per specification #1 and read command
       read -p "`pwd -P`\$ " _command
    
       # Echo command to log file as per specification #3
       echo "$_line: $_command" >>$_LOG
    
       # Arrange to exit on user input with value 'exit' as per specification #5
       if [[ "$_command" == "exit" ]]
       then
          _cleanup
       fi
    
       # Execute whatever command was entered as per specification #2
       eval $_command
    
       # Capture the success/fail counts to support specification #5b
       _status=$?
       if [ $_status -eq 0 ]
       then
         ((_success++))
       else
         ((_fail++))
       fi
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still not very good with data structures, but I have this homework assignment
I'm trying to solve this problem, its not a homework question, its just code
I am working on a homework assignment and I'm trying to figure out a
For homework, I'm trying to create a CustomButton that has a frame and in
Firstly, this is a homework assignment, and I am very new to programming in
as a part of a homework assignment, I'm trying to read a single char
I'm writing an HTTP server for a homework assignment, and I'm trying to make
I'm pretty new to JSON, javascript, YUI and trying to compelte a homework assignment.
I'm trying to do my homework and hacking through some example code I saw
For a homework assignment, we are working on CSV parser. I'm trying to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.