Removing created temp files in unexpected bash exit

前端 未结 8 1643
无人及你
无人及你 2021-01-29 21:44

I am creating temporary files from a bash script. I am deleting them at the end of the processing, but since the script is running for quite a long time, if I kill it or simply

8条回答
  •  悲&欢浪女
    2021-01-29 22:35

    I cannot believe that so many people assume that a file name will not contain a space. The world will crash if $TMPDIR is ever assigned to "temporary directory".

    zTemp=$(mktemp --tmpdir "$(basename "$0")-XXX.ps")
    trap "rm -f ${zTemp@Q}" EXIT
    

    Spaces and other special characters like single quotes and carriage returns in file names should be considered in code as a requirement of decent programming habit.

提交回复
热议问题