Find the answer to your Linux question:
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 ...
  1. #1
    Just 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)

  2. #2
    Just Joined! sixdrift's Avatar
    Join Date
    Jan 2007
    Location
    In and around and about Cary, NC
    Posts
    44
    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 line
    Code:
    exec 3<>"$1"
    replaces 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.

    The next line
    Code:
    rm -- "$1"
    removes 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.

    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 final
    Code:
    exec 3>-
    is 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.

    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?

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...