VM trap may work differently than a pure install trap.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

That is the last reply I received from my instructor, and I'm looking for some alternatives.

When using the trap command, the echo statement does not return to the console. Instead, there are the spaces, which indicates to me that the echo command is recognized but the termial won't display the characters. This is based on 3 weeks of experience.

I've tried this same script in Knoppix and Ubuntu, both of which are clean installed, and the Toolwire virtual environment that the college provides and cannot get this script to echo as such:

This is a test program
Loop #1
Loop #2
Loop #3
^C Sorry! I have trapped Ctrl-C
Loop #4
Loop #5
Loop #6
Loop #7
^C Sorry! I have trapped Ctrl-C
Loop #8
Loop #9
Loop #10
This is the end of the test program

This is a last ditched effort to solve the problem, the instructor and I have been going at this for a couple of days and it's really time to move on. I still want to solve this so I am reaching out to all of you to see if I can get an understanding of the dynamic happening here. Mostly becuase the book says the script works, it works for the instructor and a classmate, just not me.

I hope I have provided enough information. Let me know if I need to post something else such as a screenshot.

Thank you.
2. Relevant commands, code, scripts, algorithms:

Using the script copied from the text:

Ch. 15, �Script Control,� of Linux� Command Line and Shell Scripting Bible.

System Info:

RHEL = ver 6: 2.6.18-348.4.1.el5
Terminal = Gnome gnome-panel 2.16.1
SHELL = bash
VirtualBox = 4.2.12.r84980

  1. The attempts at a solution (include all code and scripts):

Script

#! /bin/bash
count=1
trap " echo 'I have trapped CTRL-C'" SIGTERM SIGINT
echo "Start of the program..."
while [ $count -lt 10 ]
        do
        echo "Loop #$count"
        sleep 10
        count=$[ count + 1]
        done
echo "End of the program..."

Output

 
 
Start of the program...
Loop #1
# Should Echo Here {^C}
Loop #2
# Should Echo Here {^C}
Loop #3
Loop #4
Loop #5
Loop #6
...echo
Loop #7
...echo
Loop #8
....echo
Loop #9
End of the program...
[me@localhost ~]$ I have trapped CTRL-C <--the trap echos here. 
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

University Of Phoenix - Online; Dr. Lori Nicholson; POS 421

Instructor Feedback:

I don't think it has anything to do with you and your code - after I made a few corrections to your script - but I think it may be your system setup.

The trap is captured - that's not the issue. The issue is what happens after the program execution is stopped by the trap. The system will not let it continue one more step and echo out the display to the screen. The trap stops the program but doesn't continue to execute the shell script to the point where we see the text and the echo command is executed.

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

I pasted the quoted code into a file and ran it and - to my surprise - it worked. I would have expected this:

        count=$[ count + 1]

to caue problems, it "just doesn't seem right", but then, i use Korn Shell most of the times and avoid bash like the plague. I would have written:

        (( count += 1 ))

which should do the same and is POSIXly correct (notice the spaces around "((" and "))", they are necessary), but: never argue with success, yours obviously works.

One thing i noticed, though, was: as soon as i pressed "CTRL-C" the trap was executed but the "sleep 10" was interrupted and then skipped. Pressing CTRL-C often enough let me get through with the script in less than half a minute, whereas 10x 10 seconds of sleeping should have taken 1min 40sec (plus some for the rest).

So, probably there is something with your setup. I have no idea what that would be, though. I tried in an XTerm on Fedora without any real changes and it worked as expected.

I hope this helps.

bakunin

I just wanted to acknowledge your post bakunin. I'm not at a machine now but I'd like to not that when you say, 'sleep 10 was interrupted', that makes sense.

i am familiar with the += accumulator from Java, and haven't tried this syntax yet. So I consider this a step in the positive direction. In addition, I haven't tried XTerm yet either. I have my fingers crossed that this will make a difference. I'm really a hardware guy and in my experience, this is is a display issue. I think that my instructor, you, and myself agree that this has something to do with the way I'm setup, and a possibility of doing this in a virtual environment.

I thought I was doing myself a favor by running VirtualBox so that I could have a couple of Linux distributions and Windows Server 2008 r2 running all under the same umbrella. I haven't tried an actual install yet becuase I just didn't have time, and that's really the next step, besides trying to talk it out here.

I really appreciate your reply.