How to create a temporary portable shell file in a safe way?

I want to create a temporary file in the POSIX shell ( /bin/sh ).

I found that mktemp(1) does not exist in my AIX block, and according to How portable is mktemp (1)? It is not so portable and / or secure in any way.

So what should I use instead?

+7
source share
3 answers

Why not use /dev/random ?

It may be neat with perl, but od and awk will do something like:

 tempfile=XXX-$(od -N4 -tu /dev/random | awk 'NR==1 {print $2} {}') 
+8
source

You have not defined β€œsafe” exactly, but one element of it should probably be cleared after you.

 trap "rm -f \"$tmpfile\"" 0 1 2 3 15 

You can probably man 3 signal see if there are other signals that could erase the temp file. A zero signal means "clean output."

+3
source

Get here from Google for portable mtemp. My needs are less secure than OP, so I just used the script PID:

 tempx=/tmp/mytemp.$$ 
0
source

All Articles