bash script temp file

Hi, can anyone explain me that two lines to make temp files on bash script?


tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/temp$$

and


tempfile=`mktemp 2>/dev/null || echo /tmp/tempfile$$`

Hi
The tempfile command


DESCRIPTION
tempfile creates a temporary file in a safe manner. It uses tempnam(3)
to choose the name and opens it with O_RDWR | O_CREAT | O_EXCL. The
filename is printed on standard output.

The mktemp command


DESCRIPTION
Create a temporary file or directory, safely, and print its
name.  TEMPLATE must contain at least 3 consecutive `X's in last
component.  If TEMPLATE is not specified, use tmp.XXXXXXXXXX, and
--tmpdir is implied.


Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 11.4 (x86_64) Kernel 2.6.37.6-0.9-desktop
up 1 day 0:22, 3 users, load average: 0.52, 0.18, 0.09
GPU GeForce 8600 GTS Silent - Driver Version: 290.10

mktemp is on my system, but tempfile is not, and the script works :S

anyway, I don undertand why “|| echo /tmp/tempfile$$”, mktemp 2>
/dev/null create a temp file witout printing the name but the rest?

No, not without printing the name, but not displaying any errors which may go to stderr. The || echo /tmp/tempfile$$ is a fallback in case mktemp fails and exits with an error.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The ‘||’ is a literal OR operator. If the first part of the expression
(tempfile command) returns successfully then there is no need to
continue and trying to set tempfile or echo whatever manually because
the first part worked and the entire statement is already true (true ||
false == true). If the first part fails the rest must be checked to see
if true can be reached (false || true == true, but false || false ==
false) so the latter part then runs. Keep in mind that ‘$$’ will be
expanded to the PID of the current process naturally so you end up with
a potentially-unique file name set to ‘tempfile’ in the end.

Of course, the first example sets an environment variable using a
command of the same name. That seems kinda silly to me, but it’s allowed.

Good luck.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJO6QoQAAoJEF+XTK08PnB5ZRIP/3r6WfTGX2+ArEbTiCnmnOo3
e8YTWbUiYfTQZNDVefzva6GsDMGT2Ik6dDPHpDyOmOEmaon8sda8+VvnwX3Ng7My
qGxGWW4YgjBy+BTXQTrcI/Ei/35pCldxVezcONi1OGO33H0BmI8L+0LgldfbZotz
wY8W3UgThmIsOqdhyFXaWAsaOKnnf9LtB7rdN+nZcKXx11SKCY5jk9Aa9J+XubEm
qwsMqjRm/clu2mT7iad3Vp4XKiQmYR4eblwXRcSAXIwLSo7DXw3m+OjgdcGBHk+z
hT/rvuF6kvdt5JhF8Nm4TlZUM71qOVc8XaCqqpfVXsEJllJQxVdULRAEGLxYWIPZ
kb0eD391EhBLl/6jTrZkgAbb+ijb/DV5Flv8jX73r4qhpjM162Sf1ZEQM9JbRS4/
+A6sINj/mJnlBLEtU3JwvzaqBKLrEFnvPpMtL+i3vfL2UhKxcdTAOOe+CeH85XBy
8TxKcKx59uLd/gYZzlBKZ7VPC+k2teNr8SpfOQFgRPkr5+7LglqFbsqnYWhDhKhh
BI0iwbXPGGgy413WcJq83SRs1UB90DWArh3yivrEtSmxgaL8K7I3lTbvN7/qqfA2
l2+lTbrilpF3gmGTPW/FNQ1vVB8SBEzZKaxFZqaQUavZkgfnY6NgaFpZxncmoMgn
mSIdz1CnAvrJ+j+Qyn/P
=4TQt
-----END PGP SIGNATURE-----

It’s no problem since variables are prefixed with $ to dereference, so there is no ambiguity, only slightly confusing to the reader that’s all.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks :slight_smile:


VampirD
No in elenath hîlar nan hâd gîn
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.18 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7pFQkACgkQJQ+0ABWtaVkokwCgsBPyu97H1Kyuq0/XKT86IhSf
c+AAnjFjcgdI6mXG5n1PHf70EnP0NwKx
=q8OT
-----END PGP SIGNATURE-----