tmp: Use traps to increase robustness
This commit is contained in:
		
							
								
								
									
										14
									
								
								tmp
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								tmp
									
									
									
									
									
								
							@@ -6,19 +6,25 @@
 | 
				
			|||||||
# Distributed under terms of the MIT license.
 | 
					# Distributed under terms of the MIT license.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Define variables
 | 
					# Define variables
 | 
				
			||||||
name="$(basename "$0" .sh)"
 | 
					name="$(basename "$0" .sh)"
 | 
				
			||||||
tmpdir="$(mktemp -d)"
 | 
					tmpdir="$(mktemp -d)"
 | 
				
			||||||
## Define functions
 | 
					# Verify them
 | 
				
			||||||
 | 
					[ -z ${tmpdir+x} ] && exit 2
 | 
				
			||||||
 | 
					# Define functions
 | 
				
			||||||
function log() {
 | 
					function log() {
 | 
				
			||||||
	[ -z ${1+x} ] && return 1
 | 
						[ -z ${1+x} ] && return 1
 | 
				
			||||||
	printf "${name}: $1\n"
 | 
						printf "${name}: $1\n"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
## Do the do
 | 
					function cleanup() {
 | 
				
			||||||
 | 
						rm -rf "${tmpdir}"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					trap "cleanup" EXIT
 | 
				
			||||||
 | 
					# Do the do
 | 
				
			||||||
log "This folder will be removed when this shell exits"
 | 
					log "This folder will be removed when this shell exits"
 | 
				
			||||||
(
 | 
					(
 | 
				
			||||||
cd "${tmpdir}"
 | 
					cd "${tmpdir}"
 | 
				
			||||||
$SHELL
 | 
					$SHELL
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
rm -rf "${tmpdir}"
 | 
					cleanup
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user