Results 1 to 3 of 3
Can anyone offer an explanation of how this:
i=`tempfile -m 0600`
exec 3<>"$i"
rm -- "$i"
echo "Hello, world" >&3
shred - >&3
exec 3>-
code works please. (Taken from ...
- 03-23-2010 #1Just Joined!
- Join Date
- Mar 2010
- Posts
- 16
Explanation of Bash scriptlette please
Can anyone offer an explanation of how this:
i=`tempfile -m 0600`
exec 3<>"$i"
rm -- "$i"
echo "Hello, world" >&3
shred - >&3
exec 3>-
code works please. (Taken from man page for shred)
- 03-25-2010 #2
Ok, I'll bite. It looks kind of odd and obscure at first, but it kind of makes sense.
The purpose of "shred" is to completely erase a file so that it cannot be recovered. The script adds a little to that so that the commands to erase the file are executed in a new shell with exec that holds a temp file open to demonstrate shredding temp files.
The linereplaces the current shell with a new bash shell, opens file descriptor 3 for input from the temp file and redirects all standard output to the temp file. This action will hold on the temp file open even after the entry is rm'ed and unlinked.Code:exec 3<>"$1"
The next lineremoves the file entry. The "--" terminates command line args so a filename that begins with a single '-' can be removed as well. This unlinks the entry for the tempfile, but it is still held open, so its not gone yet, but not accessible by anyone else.Code:rm -- "$1"
Now because the new shell is holding file descriptor 3 open it can write some data to it ("hello world" or whatever).
Finally shred is invoked to shred the standard output and writes its output to the open file descriptor 3.
Now the last part I am a little fuzzy on, but I think the finalis used to close out file descriptor 3 when a new shell is exec'ed in place and restores input from standard input. And thus the script is done. The final exec is really just there to be nice and return you to a working bash shell prompt after closing file descriptors.Code:exec 3>-
It basically is trying to demonstrate how to shred temp files by opening a file descriptor to them so that when unlinked you still have them open and can write to them. Once you close that file descriptor, by either exiting the current shell, or exec a new one, the temp file is finally removed.
At least this is how I think this script works. Anyone else?
- 03-30-2010 #3Just Joined!
- Join Date
- Mar 2010
- Posts
- 16
Thanks. That helped me to make sense of that particular info that I have on scripting. Unfortunately it turns out not to resolve this problem {/161786-shreds-testing-my-head.html#post769583} on this sub-forum.


Reply With Quote