203 lines
4 KiB
Bash
Executable file
203 lines
4 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
: <<=cut
|
|
=encoding utf8
|
|
|
|
=head1 NAME
|
|
|
|
T - make temporary files
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
B<T> [I<-#>] [I<filename>]
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
B<T> can be used in 3 ways.
|
|
|
|
=head2 End of a pipe - save to file
|
|
|
|
If placed last in a pipe B<T> will take standard output from a command
|
|
and save it in a temporary file.
|
|
|
|
Equivalent to: B<cat> >B<tempfile>
|
|
|
|
=head3 Example
|
|
|
|
cat file | T
|
|
|
|
=head2 Start of a pipe - type the file
|
|
|
|
If placed first in a pipe B<T> will type the last temporary file's
|
|
contents to standard output.
|
|
|
|
Equivalent to: B<cat tempfile>
|
|
|
|
=head3 Example
|
|
|
|
T | cat
|
|
|
|
=head2 In the middle of a pipe - save and type the file
|
|
|
|
If placed in the middle of a pipe B<T> will copy standard output from
|
|
a command and save it in a temporary file and pass the standard output
|
|
on.
|
|
|
|
Equivalent to: B<tee tempfile>
|
|
|
|
=head3 Example
|
|
|
|
cat | T | cat
|
|
|
|
=head2 Relative .tmp dir
|
|
|
|
B<T> will use B<$HOME/.tmp> if it cannot find a dir called B<.tmp> in
|
|
any of the (grand*)parent dirs.
|
|
|
|
|
|
=head1 OPTIONS
|
|
|
|
=over 9
|
|
|
|
=item I<filename>
|
|
|
|
If given a filename B<T> saves info to that file in the .tmp-directory
|
|
instead of tempfile
|
|
|
|
=item -#
|
|
|
|
If given option -# which # is a number T will use the #'th last
|
|
version.
|
|
|
|
=back
|
|
|
|
=head1 FILES
|
|
|
|
=over 5
|
|
|
|
=item .tmp/tempfile or .tmp/I<filename>
|
|
|
|
the most recent temporary file created
|
|
|
|
=item .tmp/I<filename>-I<YYYY-MM-DD.hh:mm:ss-pid>
|
|
|
|
the temporary file created at I<YYYY-MM-DD hh:mm:ss> with process id I<pid>
|
|
|
|
=back
|
|
|
|
|
|
=head1 AUTHOR
|
|
|
|
Copyright (C) 2017 Ole Tange,
|
|
http://ole.tange.dk and Free Software Foundation, Inc.
|
|
|
|
|
|
=head1 LICENSE
|
|
|
|
Copyright (C) 2012 Free Software Foundation, Inc.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
at your option any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
=head1 SEE ALSO
|
|
|
|
B<tee>(1), B<cat>(1)
|
|
|
|
|
|
=cut
|
|
|
|
find_tmp_dir() {
|
|
# Search for .tmp in all parent dirs
|
|
dir="$1"
|
|
if [ -d "$1"/.tmp ] ; then
|
|
echo "$1"/.tmp
|
|
return 0
|
|
else
|
|
if [ "/" == $(readlink -f "$dir") ]; then
|
|
echo $HOME/.tmp
|
|
return 0
|
|
fi
|
|
find_tmp_dir "../$dir"
|
|
return $?
|
|
fi
|
|
}
|
|
|
|
oldversion() {
|
|
# Find $oldversion of $name
|
|
local tmpdir="$1"
|
|
local name="$2"
|
|
local oldversion="$3"
|
|
find "$tmpdir" -name "$name"-'[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]?[0-9][0-9]:[0-9][0-9]:[0-9][0-9]-[0-9]*' |
|
|
sort |
|
|
tail -n $oldversion |
|
|
head -n 1
|
|
}
|
|
|
|
#debug_log=/tmp/T-debug
|
|
debug_log=/dev/null
|
|
|
|
tmpdir=$(find_tmp_dir .)
|
|
mkdir -p "$tmpdir"
|
|
|
|
case "$1" in
|
|
(-[0-9]*) oldversion=${1:1}
|
|
shift
|
|
;;
|
|
esac
|
|
|
|
if [ -z "$1" ]; then
|
|
name=tempfile
|
|
else
|
|
name=$(echo "$1" | sed -e 's:/:__:g')
|
|
fi
|
|
|
|
if [ -z "$oldversion" ]; then
|
|
readpathname="$tmpdir/$name"
|
|
writename="$name"-`date +"%Y-%m-%dT%H:%M:%S"`-$$
|
|
writepathname="$tmpdir/$writename"
|
|
else
|
|
readpathname=$(oldversion "$tmpdir" "$name" "$oldversion")
|
|
writepathname="$readpathname"
|
|
fi
|
|
|
|
echo "$tmpdir $pathname $name $oldversion" >> $debug_log
|
|
|
|
if tty -s ; then
|
|
# STDIN is terminal
|
|
# Don't care what STDOUT is
|
|
# T | cat => T= cat file
|
|
# T => T= cat file
|
|
echo cat "$readpathname" >> $debug_log
|
|
cat "$readpathname"
|
|
else
|
|
if [ -t 1 ] ; then
|
|
# STDOUT = terminal
|
|
# cat | T => T= >file
|
|
echo cat '>' "$writepathname" >> $debug_log
|
|
cat > "$writepathname"
|
|
else
|
|
# STDOUT redir'ed
|
|
# cat | T | cat => T= tee file
|
|
echo tee "$writepathname" >> $debug_log
|
|
tee "$writepathname"
|
|
fi
|
|
if [ -z "$oldversion" ]; then
|
|
echo ln -s "$writename" "$readpathname" >> $debug_log
|
|
# Replace symlink to most recent file
|
|
ln -fs "$writename" "$readpathname"
|
|
fi
|
|
fi
|