Commits

rsvp  committed 90d60ef

Add sto.sh to quickly manage temporary storage.

  • Participants
  • Parent commits 06eab4c

Comments (0)

Files changed (2)

 README for gists
 ================
 
-Introduction :: last update : 2012-04-01
+Introduction :: last update : 2012-04-22
 ========================================
 
 "Gists" are useful code snippets or short scripts, where documentation
 Short descriptions below are in reverse chronological order:
 
 
+sto.sh
+======
+
+interim temporary storage with tee, pager, format, diff.
+Fast quintessential command-line management between three 
+storage units. Also supports gzip compressed files.
+
+
 linecol.sh
 ==========
 
+#!/usr/bin/env bash
+#              bash 4.1.5(1)     Linux Ubuntu 10.04           Date : 2012-04-10
+#
+# _______________|  sto : interim tmp storage with tee, pager, format, diff.
+#                         There are THREE storage units.
+#
+#           Usage:  sto [ input: i, j, k     output: oi, oj, ok     diff: d??
+#                        append: I, J, K      xclip: x, y, z       shift: s 
+#                        format: f, g, h    addclip: X, Y, Z  accumulate: a
+#                          copy: ci, cj, ck [new prefix]             cat: c 
+#                          edit: vi, vj, vk                         list: l
+#                                        input only: ii, ij, ik (no tee)
+#                                       append only: iI, iJ, iK (no tee)       
+#                        remove: ri, rj, rk, R ]
+#
+#        Examples:  % expensive_command | sto i
+#                   #    see if output is satisfactory
+#                   % sto vi
+#                   #     ^edits storage-i.
+#                   % sto oi | another_command | sto J
+#                   #     output and transform i-storage, append to j.
+#                   % sto oj
+#                   #     ^review j storage.
+#                   % sto dji
+#                   #     ^compares j and i storage units.
+#                   % sto x | head
+#                   #     ^put clipboard into first storage.
+#                   % sto X
+#                   #     ^append clipboard to first storage.
+#                   % sto s
+#                   #     shift j to k, then copy i to j.
+#                   #     For "chronological" ordering: j, k, i.
+#                   % sto ci foo
+#                   #     copy and rename i-storage to favdir with timestamp.
+#                   % sto l
+#                   #     list all temporary and copied files.
+#                   % sto j bar.txt.gz | tail
+#                   #     plain or compressed gz file can be source 
+#                   #     of input, and temporarily stored decompressed.
+#                   #     (Decompression does not change filename supplied.)
+#                   % sto R
+#                   #     remove i,j,k storage units.
+#
+#    Dependencies:  tee
+#                   xclip    [change programs using User Preferences below]
+#                   fmt
+#                   $PAGER
+
+
+#  CHANGE LOG  LATEST version available:   https://bitbucket.org/rsvp/gists/src
+#
+#  2012-04-10  Allow input and append WITHOUT any further output from tee.
+#                 This is useful, e.g. command  2>  >( sto ii )
+#                 since output from "sto i" overhelm the shell here.
+#  2012-04-05  Replace cat with "zcat -f" for decompression
+#                 of incoming compressed stream or file.
+#  2012-04-01  Change to use fast memory storage: /dev/shm
+#                 Individual storage removal. List storage.
+#  2012-03-31  Change output options, let 'oi' be default.
+#                 Fix program defaults in User Preference. Add --help.
+#  2012-03-30  Option to format text before storage.
+#  2012-03-29  Option to copy storage into favdir.
+#  2012-03-28  Option to accumulate in k-storage.
+#                 Remove storage is now capitalized as "R" option.
+#  2012-03-27  Add options to put clipboard into tmp files via xclip.
+#                 Also options to append to tmp file,
+#                 concatenate tmp files, and edit tmp file. 
+#  2012-03-26  Add options to diff the tmp files.
+#  2012-03-25  Make tee convenient; named after HP calculator function STO.
+
+
+#           _____ PREAMBLE_v2: settings, variables, and error handling.
+#
+LC_ALL=POSIX
+#      locale means "ASCII, US English, no special rules, 
+#      output per ISO and RFC standards." 
+#      Esp. use ASCII encoding for glob and sorting characters. 
+shopt -s   extglob
+#     ^set extended glob for pattern matching.
+set -e
+#   ^errors checked: immediate exit if a command has non-zero status. 
+set -u
+#   ^unassigned variables shall be errors.
+#    Example of default VARIABLE ASSIGNMENT:  arg1=${1:-'foo'}
+
+arg1=${1:-'oi'}
+infile=${2:-'-'}
+#            ^default filename is '-' i.e. standard input.
+
+
+#                       USER PREFERENCES
+tmpdir='/dev/shm'
+#       /dev/shm is virtual memory -- far faster than /tmp on disk!
+favdir="$HOME/net"
+#             ^destination directory for copied files.
+pager=${PAGER:-'less'}
+#       PAGER should flush output completely if sto is piped out.
+diffprg=${DIFFER:-'diff'}
+editor=${VISUAL:-'vim'}
+formatprg='fmt --width=78'
+clipboarder='xclip -utf8 -o'
+lsprg='ls -Altr'
+#            ^^by modified time chronologically
+
+
+
+program=${0##*/}   #  similar to using basename
+tmpi="$tmpdir/sto-${USER}-i.tmp"
+tmpj="$tmpdir/sto-${USER}-j.tmp"
+tmpk="$tmpdir/sto-${USER}-k.tmp"
+#                 ^needed esp. if tmpdir is shared among other users.
+
+
+cleanup () {
+     #  Delete temporary files, then optionally exit given status.
+     local status=${1:-'0'}
+     #  temporary files stay persistent until cleared by R option.
+     [ $status = '-1' ] ||  exit $status      #  thus -1 prevents exit.
+} #--------------------------------------------------------------------
+warn () {
+     #  Message with basename to stderr.          Usage: warn "message"
+     echo -e "\n !!  ${program}: $1 "  >&2
+} #--------------------------------------------------------------------
+die () {
+     #  Exit with status of most recent command or custom status, after
+     #  cleanup and warn.      Usage: command || die "message" [status]
+     local status=${2:-"$?"}
+     cleanup -1  &&   warn "$1"  &&  exit $status
+} #--------------------------------------------------------------------
+trap "die 'SIG disruption. Storage possibly emptied.' 114" 1 2 3 15
+#    Cleanup after INTERRUPT: 1=SIGHUP, 2=SIGINT, 3=SIGQUIT, 15=SIGTERM
+#
+# _______________     ::  BEGIN  Script ::::::::::::::::::::::::::::::::::::::::
+
+
+#                          INPUT: i, j, k   
+#                         APPEND: I, J, K
+#                         OUTPUT: o, p, q   <= correspondence
+case "$arg1" in 
+
+             #  ACCUMULATE STORAGE
+     'a')    sto j | sto K                                     ;;
+             #   i-storage is untouched for scratch work,
+             #   j-storage keeps last result for review, AND
+             #   k-storage keeps the accumulated results.
+
+             
+             #  CONCATENATE AND DISPLAY ALL STORAGE
+     'c')    [ -e $tmpi ] ||  touch $tmpi
+             [ -e $tmpj ] ||  touch $tmpj
+             [ -e $tmpk ] ||  touch $tmpk
+             cat $tmpk $tmpj $tmpi |  $pager                   ;;
+             #       k     j     i  presumably chronologically 
+             #                      due to 's' shift.
+
+             
+             #  COPY STORAGE to favdir/prefix
+             #       where arg2 supplies the prefix.
+             #  --archive preserves time of data collection.
+             #  filename: prefix-with-unix-epoch-seconds-?.txt
+             #  sleep for uniqueness of copy-timestamp.
+    'ci')    arg2=${2:-'sto'}
+             arg2="$favdir/$arg2-$(date '+%s')-i.txt"
+             cp --archive $tmpi "$arg2"
+             warn "copied i-storage to $arg2"                  
+             sleep 1.07                                        ;;
+    'cj')    arg2=${2:-'sto'}
+             arg2="$favdir/$arg2-$(date '+%s')-j.txt"
+             cp --archive $tmpj "$arg2"
+             warn "copied j-storage to $arg2"                  
+             sleep 1.07                                        ;;
+    'ck')    arg2=${2:-'sto'}
+             arg2="$favdir/$arg2-$(date '+%s')-k.txt"
+             cp --archive $tmpk "$arg2"
+             warn "copied k-storage to $arg2"                  
+             sleep 1.07                                        ;;
+
+
+             #  DIFFERENCE BETWEEN STORAGE
+   'dij')    $diffprg $tmpi $tmpj                              ;;
+   'dji')    $diffprg $tmpj $tmpi                              ;;
+   'djk')    $diffprg $tmpj $tmpk                              ;;
+   'dkj')    $diffprg $tmpk $tmpj                              ;;
+   'dik')    $diffprg $tmpi $tmpk                              ;;
+   'dki')    $diffprg $tmpk $tmpi                              ;;
+
+
+             #  FORMAT TO STORAGE
+     'f')    zcat -f "$infile" | $formatprg | tee $tmpi | $pager ;;
+     'g')    zcat -f "$infile" | $formatprg | tee $tmpj | $pager ;;
+     'h')    zcat -f "$infile" | $formatprg | tee $tmpk | $pager ;;
+
+
+             #  INPUT INTO STORAGE
+     'i')    zcat -f "$infile" | tee $tmpi |  $pager           ;;
+     'j')    zcat -f "$infile" | tee $tmpj |  $pager           ;;
+     'k')    zcat -f "$infile" | tee $tmpk |  $pager           ;;
+
+
+             #  INPUT INTO STORAGE without further output
+    'ii')    zcat -f "$infile" > $tmpi                         ;;
+    'ij')    zcat -f "$infile" > $tmpj                         ;;
+    'ik')    zcat -f "$infile" > $tmpk                         ;;
+
+
+             #  LIST STORAGE: temporary and copied...
+   l?(s))    prefix=${2:-'*'}
+             $lsprg "$favdir"/${prefix}*-*[0-9][0-9]-[ijk].txt \
+                    "$tmpdir"/sto-${USER}-[ijk].tmp            \
+                     2> /dev/null                              \
+                     ||  cleanup                               ;;
+             #   Ignore exit code 2 when file is not found.
+
+
+             #  APPEND TO STORAGE
+     'I')    zcat -f "$infile" | tee --append $tmpi |  $pager  ;;
+     'J')    zcat -f "$infile" | tee --append $tmpj |  $pager  ;;
+     'K')    zcat -f "$infile" | tee --append $tmpk |  $pager  ;;
+
+
+             #  APPEND TO STORAGE without further output
+    'iI')    zcat -f "$infile" >> $tmpi                        ;;
+    'iJ')    zcat -f "$infile" >> $tmpj                        ;;
+    'iK')    zcat -f "$infile" >> $tmpk                        ;;
+
+
+             #  OUTPUT FROM STORAGE
+    'oi')    $pager $tmpi                                      ;;
+    'oj')    $pager $tmpj                                      ;;
+    'ok')    $pager $tmpk                                      ;;
+
+
+             #  REMOVE STORAGE 
+     'R')    rm -f  $tmpi $tmpj $tmpk
+             warn "i, j, k storage REMOVED."                   ;;
+    'ri')    rm -f  $tmpi 
+             warn "i-storage removed."                         ;;
+    'rj')    rm -f  $tmpj 
+             warn "j-storage removed."                         ;;
+    'rk')    rm -f  $tmpk
+             warn "k-storage removed."                         ;;
+
+
+             #  SHIFT STORAGE 
+     's')    [ -e $tmpj ] ||  touch $tmpj
+             cp --archive $tmpj $tmpk
+             [ -e $tmpi ] ||  touch $tmpi
+             cp --archive $tmpi $tmpj
+             warn "storage SHIFTED."                           ;;
+
+
+             #  EDIT STORAGE 
+    'vi')    $editor $tmpi                                     ;;
+    'vj')    $editor $tmpj                                     ;;
+    'vk')    $editor $tmpk                                     ;;
+
+
+             #  CLIPBOARD INTO STORAGE
+     'x')    $clipboarder |  tee $tmpi |  $pager               ;;
+     'y')    $clipboarder |  tee $tmpj |  $pager               ;;
+     'z')    $clipboarder |  tee $tmpk |  $pager               ;;
+     'zf')   sto z | sto f                                     ;;
+
+
+             #  APPEND CLIPBOARD TO STORAGE
+     'X')    $clipboarder |  tee --append $tmpi |  $pager      ;;
+     'Y')    $clipboarder |  tee --append $tmpj |  $pager      ;;
+     'Z')    $clipboarder |  tee --append $tmpk |  $pager      ;;
+     'ZF')   sto Z | $formatprg | sto I                        ;;
+
+
+ '--help')   $pager "$( which $0 )"                            ;;
+     '-h')   $pager "$( which $0 )"                            ;;
+
+
+       *)    die "undefined arg: $arg1"  113                   ;;
+esac
+
+
+cleanup
+# _______________ EOS ::  END of Script ::::::::::::::::::::::::::::::::::::::::
+
+#  vim: set fileencoding=utf-8 ff=unix tw=78 ai syn=sh :