Revision 18894,
1.1 KB
checked in by rbasch, 22 years ago
(diff) |
Add mksessiondir, a script which creates a temporary directory for the
user session, ensuring that the directory does not already exist.
|
-
Property svn:executable set to
*
|
Line | |
---|
1 | #!/bin/sh |
---|
2 | |
---|
3 | # $Id: mksessiondir.sh,v 1.1 2003-02-19 23:53:24 rbasch Exp $ |
---|
4 | # This script creates a temporary directory for the user session, |
---|
5 | # ensuring that the directory does not already exist. It outputs |
---|
6 | # the name of the created directory. |
---|
7 | |
---|
8 | progname="`basename $0`" |
---|
9 | sid=$$ |
---|
10 | |
---|
11 | usage() { |
---|
12 | echo "Usage: $progname [-s <session_ID>]" 1>&2 |
---|
13 | exit 1 |
---|
14 | } |
---|
15 | |
---|
16 | while getopts s: opt; do |
---|
17 | case "$opt" in |
---|
18 | s) |
---|
19 | sid=$OPTARG |
---|
20 | ;; |
---|
21 | \?) |
---|
22 | usage |
---|
23 | ;; |
---|
24 | esac |
---|
25 | done |
---|
26 | |
---|
27 | # First try to create the default directory name, based only on the |
---|
28 | # user name and session ID. If that fails, we will use this name as |
---|
29 | # the prefix in finding a unique directory name. |
---|
30 | dir="/tmp/session-${USER}-${sid}" |
---|
31 | mkdir -m 700 "$dir" > /dev/null 2>&1 |
---|
32 | if [ $? -ne 0 ]; then |
---|
33 | # Default name failed, try to uniquify. |
---|
34 | prefix="$dir" |
---|
35 | maxtries=500 |
---|
36 | i=0 |
---|
37 | while [ `expr $i \< $maxtries` -eq 1 ]; do |
---|
38 | dir="${prefix}-`expr $$ + $i`" |
---|
39 | mkdir -m 700 "$dir" > /dev/null 2>&1 |
---|
40 | if [ $? -eq 0 ]; then |
---|
41 | break |
---|
42 | fi |
---|
43 | i=`expr $i + 1` |
---|
44 | done |
---|
45 | if [ `expr $i \>= $maxtries` -eq 1 ]; then |
---|
46 | echo "$progname: Cannot create a directory" 1>&2 |
---|
47 | exit 1 |
---|
48 | fi |
---|
49 | fi |
---|
50 | echo "$dir" |
---|
51 | exit 0 |
---|
Note: See
TracBrowser
for help on using the repository browser.