2 ms·
Remove Temporary File on Exit
trap "rm -f tmpfile; exit 0" 0
trap "rm -f tmpfile; exit 1" HUP INT QUIT PIPE TERM 2>/dev/null||
trap "rm -f tmpfile; exit 1" 1 2 3 13 15
- simonblack 3y agoHow about that gets reformatted so that it's readable? And you tell us what it's intended to do. And why we should use it too. And why it's better to use 'tmpfile' than 'mktemp'.
- beardyw 3y agoI think tmpfile is the filename.
- simonblack 3y agoYes it is. However, hard-coding a file name is one of the ways to run into trouble with scripts. Unless a script meticulously cleans up after itself, there is the risk that somebody like 'root' can leave a file of that name behind and that will break future instances of the script because a normal user can't create, write to, or remove that file until root comes along and cleans it away. However 'mktemp' will produce a new individually-named temporary file that won't clash with other users when they also use that script. And even if the script doesn't clean up after itself, like it should, the likelihood is extremely low that there will be a future name-clash.
- 1vuio0pswjnm7 3y agohttps://www.netmeister.org/blog/mktemp.html https://www.netmeister.org/blog/mktemp.html New title: "Remove File Upon Receipt of Certain Signals" trap "rm -f file; exit 0" 0 trap "rm -f file; exit 1" HUP INT QUIT PIPE TERM 2>/dev/null|| trap "rm -f file; exit 1" 1 2 3 13 15