source: trunk/packs/dotfiles/mksessiondir.sh @ 18894

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
8progname="`basename $0`"
9sid=$$
10
11usage() {
12  echo "Usage: $progname [-s <session_ID>]" 1>&2
13  exit 1
14}
15
16while getopts s: opt; do
17  case "$opt" in
18  s)
19    sid=$OPTARG
20    ;;
21  \?)
22    usage
23    ;;
24  esac
25done
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.
30dir="/tmp/session-${USER}-${sid}"
31mkdir -m 700 "$dir" > /dev/null 2>&1
32if [ $? -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
49fi
50echo "$dir"
51exit 0
Note: See TracBrowser for help on using the repository browser.