Sample Header Ad - 728x90

Building a sample tree using `mktemp` in a script produces errors "mktemp: failed to create file via template" and " ambiguous redirect"

0 votes
2 answers
55 views
I'm trying to make a test case for an NFS-related bug, where rm -rf fails with "Directory not empty" when it is empty whenever I inspected it. However a simple test script cannot reproduce the error, so I tried to write a script that creates some directory tree, populates it with files, and then removes the tree. As I suspect the problem being related to some filename caching, I'm using mktemp to create random names like this: ~~~lang-bash #!/bin/sh set -x readonly LEVELS=6 readonly BASE_DIR=test-dir DIR="$BASE_DIR" for (( level=0; level 0 )); then DIR="$(mktemp -p "$DIR")" || exit else mkdir "$DIR" || exit fi for (( i=0; i $(mktemp -p "$DIR") || exit done sleep 0.5 done rm -rf "$BASE_DIR" ~~~ However the script fails in a way I don't understand, like this: ~~~lang-bash > ./test-script.sh + readonly LEVELS=6 + LEVELS=6 + readonly BASE_DIR=test-dir + BASE_DIR=test-dir + DIR=test-dir + (( level=0 )) + (( level 0 )) + mkdir test-dir + (( i=0 )) + (( i 0 )) ++ mktemp -p test-dir + DIR=test-dir/tmp.iIbUu2ZDDN + (( i=0 )) + (( i ll test-dir/ total 16 -rw------- 1 windl dvmed 32 Mar 13 12:03 tmp.gbslFgK2eD -rw------- 1 windl dvmed 0 Mar 13 12:03 tmp.iIbUu2ZDDN -rw------- 1 windl dvmed 32 Mar 13 12:03 tmp.lR5NM8zLtb -rw------- 1 windl dvmed 32 Mar 13 12:03 tmp.p5Xcfqa3Xr -rw------- 1 windl dvmed 32 Mar 13 12:03 tmp.YRELcRCk6G ~~~ My guess is that the "ambiguous redirect" is due to mktemp failing to create a valid filename.
Asked by U. Windl (1715 rep)
Mar 13, 2025, 11:38 AM
Last activity: Mar 13, 2025, 11:56 AM