osx - Why is mktemp on OS X broken with a command that worked on Linux? -


in linux shell script suppose work :

# temporary directory me work in mytemp_dir="$(mktemp -t -d zombie.xxxxxxxxx)"  # change temporary directory cd "${mytemp_dir}" 

however, when perform operation on mac, following error:

dhcp-18-189-66-216:shell-scripting-sci-day2 myname$ mytemp_dir="$(mktemp -t -d zombie.xxxxxxxxx)" dhcp-18-189-66-216:shell-scripting-sci-day2 myname$ cd "${mytemp_dir}" -bash: cd: /var/folders/d8/b8d1j9x94l9fr3y21xrnc0640000gn/t/-d.qzdpa9da zombie.nwlenhgdb: no such file or directory 

anyone know wrong? thank you.

on mac os x, -t option mktemp takes argument, prefix temporary file/directory's name. on linux, -t argument indicates prefix should either value of $tmpdir or default, /tmp.

so on mac os x, invocation mktemp -t -d zombie.xxxxxxxxx, signifies -twith argument -d; consequently, mktemp creates file name starts -d inside $tmpdir (/var/folders/d8/b8d1j9x94l9fr3y21xrnc0640000gn/t/-d.qzdpa9da). then, template argument used create another file (zombie.nwlenhgdb, in current working directory). finally, prints both names stdout, become value of variable ${mytemp_dir} (complete newline separator). hence, cd fails.

for platform-independent call, avoid -t flag , use explicit template:

mktemp -d "${tmpdir:-/tmp}/zombie.xxxxxxxxx" 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -