T: rewritten with functions.

This commit is contained in:
Ole Tange 2021-03-04 13:05:07 +01:00
parent 112ed47440
commit b02cf11736
2 changed files with 58 additions and 62 deletions

90
T/T
View file

@ -120,6 +120,7 @@ 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
@ -134,73 +135,68 @@ find_tmp_dir() {
fi
}
tmpdir="$(find_tmp_dir .)"
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"
T_DIR="$tmpdir"
#DEBUG_LOG=/tmp/T-debug
DEBUG_LOG=/dev/null
if echo $1 | egrep '^-[0-9]+$' >/dev/null; then
T_BACK_NO=$(echo $1 | sed -e 's:-::')
case "$1" in
(-[0-9]*) oldversion=${1:1}
shift
;;
esac
if [ -z "$1" ]; then
# No filename given. Default = tempfile
T_NO_SLASH=tempfile
name=tempfile
else
# Map / to __ to avoid creating dirs in ~/.tmp
T_NO_SLASH=$(echo $1 | sed -e 's:/:__:g')
fi
T_TEMPFIL=$T_DIR/$T_NO_SLASH
T_BACK_FILENAME=$(find $T_DIR -name "$T_NO_SLASH"-'[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 $T_BACK_NO | head -n 1)
if [ -z "$T_BACK_FILENAME" ]; then
T_TEMPFIL=$T_DIR/$T_NO_SLASH
else
T_TEMPFIL=$T_BACK_FILENAME
fi
else
if [ -z "$1" ]; then
# No filename given. Default = tempfile
T_TEMPFIL=$T_DIR/tempfile
else
# Map / to __ to avoid creating dirs in ~/.tmp
T_NO_SLASH=$(echo $1 | sed -e 's:/:__:g')
T_TEMPFIL=$T_DIR/$T_NO_SLASH
fi
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
T_NY_FIL="$T_TEMPFIL"-`date +"%Y-%m-%dT%H:%M:%S"`-$$
mkdir $T_DIR 2>/dev/null
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 "$T_TEMPFIL" >> $DEBUG_LOG
cat "$T_TEMPFIL"
echo cat "$readpathname" >> $debug_log
cat "$readpathname"
else
# STDIN redir'ed
if [ -t 1 ] ; then
# STDOUT = terminal
# cat | T => T= >file
echo cat '>' "$T_NY_FIL" >> $DEBUG_LOG
cat > "$T_NY_FIL"
echo cat '>' "$writepathname" >> $debug_log
cat > "$writepathname"
else
# STDOUT redir'ed
# cat | T | cat => T= tee file
echo tee "$T_NY_FIL" >> $DEBUG_LOG
tee "$T_NY_FIL"
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
# Remove hardlink to most recent file
rm "$T_TEMPFIL" 2> /dev/null
# make a hardlink to the most recent file
ln "$T_NY_FIL" "$T_TEMPFIL"
fi

View file

@ -26,11 +26,11 @@ column, the line number is treated as the x-axis.
Examples:
seq 1 100 | plotpipe
seq 1 100 | shuf | plotpipe
seq 1 100 | shuf | plotpipe --smooth 10 --log y
paste <(seq 1 100) <(seq 1 100) <(seq 1 100 | shuf) | plotpipe
(echo "#Title"; echo "#Subtitle";
printf "Column1\tColumn2\tColumn3";
paste <(seq 1 100) <(seq 1 100) <(seq 1 100 | shuf) ) | plotpipe
printf "Column 1\tColumn 2\tColumn 3\n";
paste <(seq 1 100) <(seq 51 150) <(seq 1 100 | shuf) ) | plotpipe
Copyright (C) 2020-2021 Ole Tange, http://ole.tange.dk and Free
Software Foundation, Inc.