-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmktmpdir.sh
executable file
·88 lines (74 loc) · 1.58 KB
/
mktmpdir.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
usage () {
echo "Usage: $0 [-p] [-n NAME]" 2>&1
}
TMP_DIR_NAME="temp"
PERSIST=0
OPTIND=1
# parse the arguments
while getopts "n:p" options;
do
case $options in
p)
# persist mode, don't delete on exit
PERSIST=1
;;
n)
# name the command prompt :)
TMP_DIR_NAME=$OPTARG
;;
:)
# requires arguments
echo "option -$OPTARG requires an argument"
usage
exit 1
;;
*)
echo "unexpected option: $1"
usage
exit 1
;;
esac
done
# save the current directory
SAVE_PWD=${PWD:-}
# create the temp directory
cd /tmp
echo "creating temporary directory..."
TEMP_DIR=`mktemp -d`
# enter the temporary directory
echo "entering $TEMP_DIR..."
cd $TEMP_DIR
# change the command prompt
SAVED_PS1=${PS1:-}
if (( PERSIST == 1 ));
then
PS1="($TMP_DIR_NAME-PERSIST) ${SAVED_PS1:-}"
else
PS1="($TMP_DIR_NAME) ${SAVED_PS1:-}"
fi
export PS1
# add a persist function so you can change your mind mid way
# typing persist will flip the persist status
function persist {
if (( PERSIST == 1 ));
then
PS1="($TMP_DIR_NAME) ${SAVED_PS1:-}"
PERSIST=0;
else
PS1="($TMP_DIR_NAME-PERSIST) ${SAVED_PS1:-}"
PERSIST=1;
fi
}
# hook the exit
function exit {
if (( PERSIST != 1 ));
then
rm -rf "$TEMP_DIR"
echo "Deleted temporary working directory $TEMP_DIR"
fi
PS1=${SAVED_PS1:-}
cd $SAVE_PWD
unset -f exit
}
trap exit exit