Commits

rsvp  committed fc42e20 Draft

Modify aw for portability. Into clipboard: sto c.

  • Participants
  • Parent commits 7492e8a

Comments (0)

Files changed (2)

 #!/usr/bin/env bash
-#              bash 4.2.24(1)     Linux Ubuntu 12.04          Date : 2012-09-10
+#              bash 4.2.24(1)     Linux Ubuntu 12.04          Date : 2012-09-13
 #
 # _______________|  aw : use awk as quick custom calculator on command line.
 #
-#           Usage:  aw  'expression'  [decimal places:4]  [format:f]
+#           Usage:  aw  'expression'  [decimal/sig:8]  [format:g]
+#                   aw  bak           #  show log file of previous result.
+#                 
 #
 #    User-defined:  r =   random number strictly between 0 and 1,
 #                            never 0 nor 1, see rand().
-#                   s =   stored result from previous execution.
+#                   s =   stored full result from previous script call.
 #                   pi, e.    
 #                   mil, bil, tri; pc, bp, milli, macro, nano.
-#                   [ Also add your favorite functions. ]
+#                   [ Also add your favorite functions.          ]
+#                   [ Recommended format choices are e, f, or g. ]
 #
 #        Examples:  % aw '(3+2)*r'         
-#                   4.6314                 #  random real between 0 and 5.
-#                   % aw r  0
+#                   4.63147                #  random real between 0 and 5.
+#                   % aw r 0 f
 #                   1                      #  random integer, 0 or 1.
 #                   % aw '10 * pc * 210'
 #                   21.0000                #  10% of 210
 #                   % aw 's/2'
 #                   10.5000                #  take half of previous result.
 #
-#    Dependencies:  gawk (install "gawk" package on Ubuntu 12.04
+#    Dependencies:  gawk (on Ubuntu 12.04 prefer installing "gawk" package
 #                         since inferior mawk of 1996 is the default awk.)
+#                   This script will use system's awk if gawk is unavailable.
 #
 #    Definitive REFERENCE by Arnold Robbins, "gawk: Effective AWK Programming",
 #       http://www.gnu.org/software/gawk/manual/    (2011)
 #
 #    WARNING: Unassigned variables in awk are given value of 0.
 #       Thus do not misspell variable names!!
+#
+#    Exponentiation: the character sequence ** is equivalent to ^.
+#    exp() is available; and the constant e is defined in the script.
+#    log() is the natural logarithm.
+#    int() is integer truncation.
+#    Functions: sqrt, sin, cos, and atan2 are also available in gawk.
 
 
 #  CHANGE LOG  LATEST version available:   https://bitbucket.org/rsvp/gists/src
+#  2012-09-13  Add variable for awkprg, in case gawk is unavailable.
+#                 printf modified for portability.
+#                 Replace both head and tail by sed for portability.
+#                 Memorialize first argument on memf's third line.
 #  2012-09-10  Add printf format as third argument.
 #                 Shadow the full result into storage.
 #  2012-09-09  First version using gawk 3.1.8 under Ubuntu 12.04, 
 #   ^unassigned variables shall be errors.
 #    Example of default VARIABLE ASSIGNMENT:  arg1=${1:-'foo'}
 
-ndec=${2:-'4'}
-#         ^default number of decimal places.
+ndec=${2:-'8'}
+#         ^default number of significant or decimal places.
 #          ndec=0 gives rounded integer result [unlike int()], but
 #          see printf section below for important rounding caveats.
-letter=${3:-'f'}
-#           ^control printf format, e.g. f, e, g -- for
-#            float, exponent, mixture with max significant digits.
+letter=${3:-'g'}
+#           ^control printf format, e.g. e, f, g -- for
+#            exponent, float, or mixture with max significant digits.
 
 
 program=${0##*/}   #  similar to using basename
 #
 # _______________     ::  BEGIN  Script ::::::::::::::::::::::::::::::::::::::::
 
-#  #  DEBUG
-#  [ -s /usr/bin/gawk ]  ||  die "gawk is not installed." 113
-#  #  sanity check, since Ubuntu 12.04 now defaults to inferior mawk.
+if  ( which gawk > /dev/null ) ; then
+     awkprg='gawk'
+else
+     #  warn "gawk is not installed. Falling back on system's awk."  #-DEBUG
+     awkprg='awk'
+     #       Ubuntu 12.04 awk defaults to inferior mawk.
+fi
 
 
 #  Retrieve result from previous execution:
 if [ -s $memf ] ; then
-     stored="$( tail -n1 $memf )"
-     #          ^pick out the second printf from gawk output.
+     stored="$( sed -n -e '2p' $memf )"
+     #                    ^pick out the second printf from gawk output.
+     if [ "$1" = 'bak' ] ; then
+          cat $memf
+          cleanup
+     fi
 else
      stored=0
      #      ^expected awk behavior.
 fi
 
 
-#-------------------------------------- gawk at work...
-{ gawk -f - <<EOHereDoc
+#-------------------------------------- awk at work...
+{ $awkprg -f - <<EOHereDoc
 BEGIN { 
 s   = "$stored"
 e   = 2.718281828459045
 evala = ( $1 )
 #     ^ evaluated argument from command line.
 
-printf "%1\$.${ndec}${letter}\n", evala
-#            ^Default: floating-point with ndec number of decimal places.
+
+printf "%.${ndec}${letter}\n", evala
+#-X  printf "%1\$.${ndec}${letter}\n", evala  # OK on gawk, not on mawk.
+#                   Floating-point with ndec number of decimal places.
 #       ___ATTN___  printf does NOT round the decimal portion.
 #       For ndec=0, we do not get integer truncation. 
 #       Rounding is "unbiased," which means it doesn't always round 
 #       CONVFMT's default value is "%.6g", which yields a value 
 #       with at least six significant digits.
 
-printf "%1\$.15e\n", evala
+
+printf "%.15e\n", evala
+#-X  printf "%1\$.15e\n", evala  # OK on gawk, not on mawk.
 #       SECOND printf reports the FULL version for storage,
 #       since the first printf may not have fully expressed 
 #       the complete result due to user formatting.
 }
 EOHereDoc
-}          >  $memf
-#    Preserve TWO results into memory.            
-#-------------------------------------- gawk done.
+}           >  $memf
+#     Preserve TWO results into memory.            
+#-------------------------------------- awk done.
 
 
 
 #    Show just the requested format result on STDOUT,
-#    i.e. the first printf within gawk.
-head -n1 $memf
+#    i.e. the first printf within awk.
+sed -n -e '1p' $memf
+#-X  head -n1 $memf  #  replaced for portability.
+
+
+echo "$1"  >>  $memf
+#               memorialize first argument on memf's third line.
 
 
 
 #!/usr/bin/env bash
-#              bash 4.1.5(1)     Linux Ubuntu 10.04           Date : 2012-04-10
+#              bash 4.1.5(1)     Linux Ubuntu 10.04           Date : 2012-09-13
 #
 # _______________|  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 
+#                                           clipput: c
+#                          copy: ci, cj, ck [new prefix]         display: d 
 #                          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 ]
+#                        remove: ri, rj, rk, R ]  [file, file.gz, stdin]
 #
 #        Examples:  % expensive_command | sto i
 #                   #    see if output is satisfactory
 #                   #     of input, and temporarily stored decompressed.
 #                   #     (Decompression does not change filename supplied.)
 #                   % sto R
-#                   #     remove i,j,k storage units.
+#                   #     Remove i, j, k storage units.
+#                   % echo "Important" | sto c
+#                   #     Result displays and enters clipboard.
+#                   % sto d
+#                   #     display k, j, i units together in that order;
+#                   #     useful with shift operator s.
 #
 #    Dependencies:  tee
 #                   xclip    [change programs using User Preferences below]
 
 #  CHANGE LOG  LATEST version available:   https://bitbucket.org/rsvp/gists/src
 #
+#  2012-09-13  Allow storage into clipboard using flag c.
+#                 Therefore, flag d is used for display.
 #  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.
 editor=${VISUAL:-'vim'}
 formatprg='fmt --width=78'
 clipboarder='xclip -utf8 -o'
+#                  ^undocumented feature for output from clipboard.
+clipput='xclip -in -filter'
+#   put  stuff  into the clipboard.
+#              -in alone will wait until another xclip event happens, 
+#                  if this script is called from another script!
+#              but -filter forces pipe back out to stdout 
+#              which apparently breaks the waiting. 
+#              Thus -filter only applies to *piped* input.
+#   ___ATTN___ Expensive:  xclip will cost 46 milliseconds.
+#                          But it's cheaper than manual highlighting ;-)
 lsprg='ls -Altr'
 #            ^^by modified time chronologically
 
 case "$arg1" in 
 
              #  ACCUMULATE STORAGE
-     'a')    sto j | sto K                                     ;;
+       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'}
+      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'}
+      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'}
+      ck)    arg2=${2:-'sto'}
              arg2="$favdir/$arg2-$(date '+%s')-k.txt"
              cp --archive $tmpk "$arg2"
              warn "copied k-storage to $arg2"                  
              sleep 1.07                                        ;;
 
 
+             #  Store incoming INTO X selection CLIPBOARD... 
+       c)    sin="$( cat - )"
+             echo    "$sin" 
+             #       ^double quotes to preserve newlines.    
+             #              Leave out $pager for mere snippet;
+             #              real purpose is to insert snippet.
+             echo -n "$sin" | $clipput > /dev/null             ;;
+             #    ^no trailing newline.
+             #
+             #  So the double echo is almost like "tee"
+             #  (with the exception of the last newline).
+
+
              #  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                              ;;
+     dij)    $diffprg $tmpi $tmpj                              ;;
+     dji)    $diffprg $tmpj $tmpi                              ;;
+     djk)    $diffprg $tmpj $tmpk                              ;;
+     dkj)    $diffprg $tmpk $tmpj                              ;;
+     dik)    $diffprg $tmpi $tmpk                              ;;
+     dki)    $diffprg $tmpk $tmpi                              ;;
 
 
+             #  Concatenate and DISPLAY ALL STORAGE
+       d)    [ -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.
+             
+
              #  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 ;;
+       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           ;;
+       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                         ;;
+      ii)    zcat -f "$infile" > $tmpi                         ;;
+      ij)    zcat -f "$infile" > $tmpj                         ;;
+      ik)    zcat -f "$infile" > $tmpk                         ;;
 
 
              #  LIST STORAGE: temporary and copied...
 
 
              #  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  ;;
+       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                        ;;
+      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                                      ;;
+      oi)    $pager $tmpi                                      ;;
+      oj)    $pager $tmpj                                      ;;
+      ok)    $pager $tmpk                                      ;;
 
 
              #  REMOVE STORAGE 
-     'R')    rm -f  $tmpi $tmpj $tmpk
+       R)    rm -f  $tmpi $tmpj $tmpk
              warn "i, j, k storage REMOVED."                   ;;
-    'ri')    rm -f  $tmpi 
+      ri)    rm -f  $tmpi 
              warn "i-storage removed."                         ;;
-    'rj')    rm -f  $tmpj 
+      rj)    rm -f  $tmpj 
              warn "j-storage removed."                         ;;
-    'rk')    rm -f  $tmpk
+      rk)    rm -f  $tmpk
              warn "k-storage removed."                         ;;
 
 
              #  SHIFT STORAGE 
-     's')    [ -e $tmpj ] ||  touch $tmpj
+       s)    [ -e $tmpj ] ||  touch $tmpj
              cp --archive $tmpj $tmpk
              [ -e $tmpi ] ||  touch $tmpi
              cp --archive $tmpi $tmpj
 
 
              #  EDIT STORAGE 
-    'vi')    $editor $tmpi                                     ;;
-    'vj')    $editor $tmpj                                     ;;
-    'vk')    $editor $tmpk                                     ;;
+      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                                     ;;
+       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                        ;;
+       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 )"                            ;;
+       -h)   $pager "$( which $0 )"                            ;;
+   --help)   $pager "$( which $0 )"                            ;;
+--version)   $pager "$( which $0 )"                            ;;
 
-
-       *)    die "undefined arg: $arg1"  113                   ;;
+        *)   die "undefined arg: $arg1"  113                   ;;
 esac