I have several scripts with while read line loops that don't execute my cleanup function when I press Ctrl C. For example:

#!/bin/bash

cleanup() {
  stty echo
  exit 0
}

trap cleanup SIGINT SIGHUP SIGTERM

stty -echo
while read -r line; do
  echo "$line"
done &lt; /foo/bar

cleanup


When I press Ctrl-C, my terminal is screwed up because the stty -echo setting is still in effect. I have many other scripts where my cleanup function works flawlessly. The only time I seem to have a problem is when I press Ctrl-C while the script is in a read loop. Is there a way to ensure that the cleanup function will get called when Ctrl-C is pressed while the script is inside a read loop? Or am I just missing something obvious here?

Update: There is something else going on in my scripts. I ran the exact script above, and I can't get it to fail the way my other scripts do. I will have to try to distill the broken scripts down to something that I can get to fail, at which point I will update the question.

Update 2: Okay, I figured it out. I was getting an error from stty (which I wasn't seeing because my real cleanup function was also clearing the screen). The error was: stty: standard input: Inappropriate ioctl for device. I looked this up an apparently it was due to calling stty while stdin was redirected from the file /foo/bar. So I changed my trap call to trap "break" SIGINT SIGHUP SIGTERM and it worked.


Question: Question about Ubuntu 12.10 extracting a .tgz file in the wrong directory
Answer: I am not sure what you wanted to accomplish here, but you told your computer to do exactly that.

running a program via sudo executes it in the context of the root user, all files created in that process will normally belong to root. The -C / parameter of tar, tells tar to extract to the / Directory, you actually need root permissions to do that at all.

If you just want to unroll the file contents into your home directory, simply run
tar -zxvf arm-linux-gcc-4.5.1-v6-vfp-20120301.tgz, you may want to make sure, you are in your home directory first by enterin cd ~.

You might want to consider using a graphical program like xarchiver for unzipping, depending on what you demand of yourself (maybe ou want to become a command line crack, this would be a honourable goal ;-).