murex Shell Docs

Command Reference: tmp

Create a temporary file and write to it

Description

tmp creates a temporary file, writes the contents of STDIN to it then returns its filename to STDOUT.

You can optionally specify a file extension, for example if the temporary file needs to be read by open or an editor which uses extensions to define syntax highlighting.

Usage

<stdin> -> tmp [ file-extension ] -> <stdout>

Examples

» out: "Hello, world!" -> set: tmp

» out: $tmp
/var/folders/3t/267q_b0j27d29bnf6pf7m7vm0000gn/T/murex838290600/8ec6936c1ac1c347bf85675eab4a0877-13893

» open: $tmp
Hello, world!

Detail

The temporary file name is a base64 encoded md5 hash of the time plus murex function ID with murex process ID appended:

package io

import (
    "crypto/md5"
    "encoding/hex"
    "io"
    "os"
    "strconv"
    "time"

    "github.com/lmorg/murex/lang"
    "github.com/lmorg/murex/lang/types"
    "github.com/lmorg/murex/utils/consts"
)

func init() {
    lang.GoFunctions["tmp"] = cmdTempFile
}

func cmdTempFile(p *lang.Process) error {
    p.Stdout.SetDataType(types.String)

    ext, _ := p.Parameters.String(0)
    if ext != "" {
        ext = "." + ext
    }

    fileId := time.Now().String() + ":" + strconv.Itoa(int(p.Id))

    h := md5.New()
    _, err := h.Write([]byte(fileId))
    if err != nil {
        return err
    }

    name := consts.TempDir + hex.EncodeToString(h.Sum(nil)) + "-" + strconv.Itoa(os.Getpid()) + ext

    file, err := os.Create(name)
    if err != nil {
        return err
    }

    defer file.Close()

    _, err = io.Copy(file, p.Stdin)
    if err != nil {
        return err
    }

    _, err = p.Stdout.Write([]byte(name))
    return err
}

This should should provide enough distance to run tmp in parallel....should you ever want to.

tmp files are also located inside a unique per-process murex temp directory which itself is located in the appropriate temp directory for the host OS (eg $TMPDIR on macOS).

See Also

This site is rebuilt weekly, the content is automatically generated from murex's source code. Last built on Mon Aug 3 06:12:53 UTC 2020 against commit 2557f7c2557f7cc7535c452c21d89164bd38ed8996f02fd. Downloadable murex binaries are also built weekly. Current version is 0.52.2000 BETA which has been verified against tests.