Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
LECABashLib
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
2
Issues
2
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LECASofts
LECABashLib
Commits
8621efee
Commit
8621efee
authored
Apr 23, 2016
by
Eric Coissac
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
manage the creation of temporary directory and its automatical erasement
at the end of the script
parent
e24cef89
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
117 additions
and
0 deletions
+117
-0
tempdir.sh
tempdir.sh
+117
-0
No files found.
tempdir.sh
0 → 100644
View file @
8621efee
# LECA Bash library
#
# The LECA bash library provides a set of function used for helping
# development of bash script mainly to write job script on the luke
# cluster
#
# The files from the LECA Bash library must be sourced from your main script
#
#
include logging
logdebug
"Load tempdir package"
include ifs
include stacks
include atexit
newstack __LECABASHLIB_BASETMPDIRSTACK__
newstack __LECABASHLIB_TMPDIRSTACK__
__LECABASHLIB_TMPBASENAME__
=
"__tmp__
$(
whoami
)
.
${
$}__lecabashlib__
.XXXXX
"
function __remove_tmp__() {
echo "
@@
${
__LECABASHLIB_TMPDIRSTACK__
[@]
}
@@
"
while popvalue __LECABASHLIB_TMPDIRSTACK__ directory ; do
echo coucou tmp
loginfo "
Removing
tempdir
:
$directory
"
rm -rf "
$directory
"
done
}
registeratexit __remove_tmp__
function availabledisk() {
local FILENAME="
${
1
}
"
pushifs
$'
\n
'
local lines=(
$(
df
-k
-P
"
${
FILENAME
}
"
)
)
pushifs ' '
local fields=(
${
lines
[0]
}
)
local values=(
${
lines
[1]
}
)
popifs
popifs
nfield=
${#
fields
[@]
}
i=0
while (( i < nfield )) && [[ "
$(
lower
${
fields
[
$i
]
}
)
" != "
available
" ]] ; do
i=
$((
i
+
1
))
done
if (( i < nfield )) ; then
size=
$((
values[i]
/
1024
/
1024
))
echo
$size
return 0
else
return 1
fi
}
function registertmpdir() {
pushvalue __LECABASHLIB_BASETMPDIRSTACK__ "
${
1
}
"
}
function tempdirectory() {
local minsize="
0
"
if [[ ! -z "
${
1
}
" ]]; then
COMMAND="
${
1
}
=
"
else
COMMAND="
echo
"
fi
shift
if [[ "
$(
stacksize __LECABASHLIB_BASETMPDIRSTACK__
)
" == "
0
" ]]; then
if [[ ! -z "
$TMPDIR
" ]] ; then
registertmpdir "
$TMPDIR
"
else
mkdir -p "
$HOME
/tmp
"
registertmpdir "
$HOME
/tmp
"
fi
fi
if [[ ! -z "
${
1
}
" ]] ; then
minsize="
${
1
}
"
fi
local nbase=
$(
stacksize __LECABASHLIB_BASETMPDIRSTACK__
)
local i=0
while (( i < nbase )) &&
\
((
$(
availabledisk
"
${
__LECABASHLIB_BASETMPDIRSTACK__
[
$i
]
}
"
)
< minsize )) ; do
i=
$((
i
+
1
))
done
if (( i >= nbase )) ; then
return 1
fi
local basedir="
${
__LECABASHLIB_BASETMPDIRSTACK__
[
$i
]
}
"
local directory=
$(
mktemp
-d
"
${
basedir
%/
}
/
${
__LECABASHLIB_TMPBASENAME__
}
"
)
pushvalue __LECABASHLIB_TMPDIRSTACK__ "
$directory
"
local valcmd="
${
COMMAND
}${
directoty
}
"
eval
$valcmd
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment