0

Prefacing this with that I found identical questions but none of them have answers that are working for me.

I need to make a temporary .json file (it needs to be json because I'll be working with jq later in the script).

I thought based on the answers to this question that it would be the following, but they're creating files named .json and XXXXXXXX.json respectively.

STACKS=$(mktemp .json)
STACKS=$(mktemp XXXXXXXX.json)

This will need to run on both mac OS, and a linux box. I can't specify a path for the file because it will be run both locally and by Jenkins, which have an unidentical file structure. What's the proper syntax?

Community
  • 1
  • 1
Alex
  • 2,555
  • 6
  • 30
  • 48

1 Answers1

3

if you are using openBSD mktemp you can

STACKS="$(mktemp XXXXXX).json"

and then write a trap so the tmps are removed when script finishes:

function cleanup {
    if [ -f "$STACKS" ] && [[ "$STACKS" =~ ".json"$ ]]; then
        rm -f "$STACKS"
    fi
}
trap cleanup EXIT

so when script finishes (no matter how) it will try to remove $STACKS if it is a file and if it ends with .json (for extra safety).

odradek
  • 993
  • 7
  • 14
  • I get the following; `mktemp: illegal option -- - usage: mktemp [-d] [-q] [-t prefix] [-u] template ... mktemp [-d] [-q] [-u] -t prefix` – Alex Apr 06 '17 at 15:16
  • what shell are you using? are you positive it's bash? also, what does mktemp --version says? – odradek Apr 06 '17 at 15:17
  • yes bash (just checked with `echo $0` to confirm), and mktemp --version gives me the same error, `illegal option -- -` etc – Alex Apr 06 '17 at 15:21
  • how about `mktemp -V`? or `-v`? i belive you are using mktemp from openBSD rather than the one from GNU Coreutils – odradek Apr 06 '17 at 15:26
  • both -V and -v are giving me illegal option errors, does that support your theory? – Alex Apr 06 '17 at 15:28
  • apparently yes, check this cli interface: http://man.openbsd.org/mktemp.1 what system are you running, mac OS? – odradek Apr 06 '17 at 15:31
  • yep, we use macs for work so I'm stuck on mac OS. I could test in a VM but this may be run locally by coworkers so a mac friendly script is necessary. – Alex Apr 06 '17 at 15:34
  • check if you have an implementation of `mkstemps` in mac OS, which permits templates to have a suffix, so your example of `XXXXXXX.json` should be valid. check `mkstemps` as an extension for mktemp here (note the s): http://www.manpages.info/macosx/mktemp.3.html – odradek Apr 06 '17 at 15:37
  • Nope, I take that back. I can view the man page for it but get a bash mkstemps: command not found error – Alex Apr 06 '17 at 15:46
  • oops, sorry, my bad, that was from the C library. sorry, i don't think i can help you with a generic solution unless this workaround is of any interest to you `STACKS="$(mktemp XXXXXX).json"` – odradek Apr 06 '17 at 15:55
  • it looks like that works but doesn't remove the file after the script finishes. If you can include a reliable way to delete the file afterwards (and edit your post to match) I'd be happy to accept your answer! (and either way, thanks for your help) – Alex Apr 06 '17 at 15:59
  • i updated the answer, i think a `trap` can handle what you mean by "realiable way" – odradek Apr 06 '17 at 16:10
  • Not sure if this would guarantee uniqueness of the generated file name. Suppose the generated prefix is /tmp/temp.abcdef but the file /tmp/temp.abcdef.ps already exists? – Ajay Brahmakshatriya Dec 05 '19 at 04:22