#!/usr/bin/env bash # Runs the supplied bash command string in a temporary workspace directory. # Usage: intemp.sh [-t prefix] # Requires: mktemp set -o errexit set -o nounset set -o pipefail global opt_flag := $(1:-) test -z $(opt_flag) && echo "No command supplied" > !2 && exit 1 if test $(opt_flag) == "-t" { shift global prefix := $(1:-) test -z $(prefix) && echo "No prefix supplied" > !2 && exit 1 shift } else { global prefix := ''temp'' } global cmd := $1 test -z $(cmd) && echo "No command supplied" > !2 && exit 1 global workspace := $[mktemp -d "$(TMPDIR:-/tmp)/$(prefix).XXXXXX] echo "Workspace created: $(workspace)" !1 > !2 proc cleanup { var -r workspace = $1 rm -rf $(workspace) echo "Workspace deleted: $(workspace)" !1 > !2 } trap "cleanup '$(workspace)'" EXIT pushd $(workspace) > /dev/null bash -ceu $(cmd) popd > /dev/null trap - EXIT cleanup $(workspace) (CommandList children: [ (C {(set)} {(-o)} {(errexit)}) (C {(set)} {(-o)} {(nounset)}) (C {(set)} {(-o)} {(pipefail)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:opt_flag) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonHyphen arg_word:{}) spids: [34 37] ) } spids: [33] ) ] spids: [33] ) (AndOr children: [ (C {(Lit_Other "[")} {(-z)} {(DQ (${ VSub_Name opt_flag))} {(Lit_Other "]")}) (AndOr children: [ (SimpleCommand words: [{(echo)} {(DQ ("No command supplied"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[59])] ) (C {(exit)} {(1)}) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ (${ VSub_Name opt_flag))} {(Lit_Other "=") (Lit_Other "=")} {(DQ (-t))} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(shift)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:prefix) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonHyphen arg_word:{}) spids: [96 99] ) } spids: [95] ) ] spids: [95] ) (AndOr children: [ (C {(Lit_Other "[")} {(-z)} {(DQ (${ VSub_Name prefix))} {(Lit_Other "]")}) (AndOr children: [ (SimpleCommand words: [{(echo)} {(DQ ("No prefix supplied"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[122])] ) (C {(exit)} {(1)}) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) (C {(shift)}) ] spids: [-1 89] ) ] else_action: [ (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:prefix) op:Equal rhs:{(SQ )} spids:[137])] spids: [137] ) ] spids: [134 142] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:cmd) op: Equal rhs: {(DQ ($ VSub_Number "$1"))} spids: [145] ) ] spids: [145] ) (AndOr children: [ (C {(Lit_Other "[")} {(-z)} {(DQ (${ VSub_Name cmd))} {(Lit_Other "]")}) (AndOr children: [ (SimpleCommand words: [{(echo)} {(DQ ("No command supplied"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[170])] ) (C {(exit)} {(1)}) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:workspace) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (C {(mktemp)} {(-d)} { (DQ (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonHyphen arg_word: {(Lit_Slash /) (tmp)} ) spids: [187 192] ) (/) (${ VSub_Name prefix) (.XXXXXX) ) } ) ] ) left_token: spids: [181 199] ) } spids: [180] ) ] spids: [180] ) (SimpleCommand words: [{(echo)} {(DQ ("Workspace created: ") (${ VSub_Name workspace))}] redirects: [(Redir op_id:Redir_GreatAnd fd:1 arg_word:{(2)} spids:[210])] ) (FuncDef name: cleanup body: (BraceGroup children: [ (Assignment keyword: Assign_Local flags: ["'-r'"] pairs: [ (assign_pair lhs: (LhsName name:workspace) op: Equal rhs: {(DQ ($ VSub_Number "$1"))} spids: [225] ) ] spids: [221] ) (C {(rm)} {(-rf)} {(DQ (${ VSub_Name workspace))}) (SimpleCommand words: [{(echo)} {(DQ ("Workspace deleted: ") (${ VSub_Name workspace))}] redirects: [(Redir op_id:Redir_GreatAnd fd:1 arg_word:{(2)} spids:[251])] ) ] spids: [218] ) spids: [214 217] ) (C {(trap)} {(DQ ("cleanup '") (${ VSub_Name workspace) ("'"))} {(EXIT)}) (SimpleCommand words: [{(pushd)} {(DQ (${ VSub_Name workspace))}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(/dev/null)} spids:[277])] ) (C {(bash)} {(-ceu)} {(DQ (${ VSub_Name cmd))}) (SimpleCommand words: [{(popd)}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(/dev/null)} spids:[293])] ) (C {(trap)} {(-)} {(EXIT)}) (C {(cleanup)} {(DQ (${ VSub_Name workspace))}) ] )