Frying Shakespeare (Bash Newbies)

= Example 1: fry1 (review) = echo -e "\tShakespeare once said:\n" if [ ! -e $0 ] then ls elif test ! -s emptyfile ; then cat $0 else head hamlet3.1 fi echo echo -n \     ...or was echo \ it Bacon?
 * 1) !/bin/bash
 * 1) review
 * 1) Tools already used:
 * 2) echo, if, test, [, ls, cat, ;
 * 3) new tool:
 * 4) head

= Example: hamlet3.1 = To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? To die: to sleep; No more; and by a sleep to say we end The heart-ache and the thousand natural shocks That flesh is heir to, 'tis a consummation Devoutly to be wish'd. To die, to sleep; To sleep: perchance to dream: ay, there's the rub; For in that sleep of death what dreams may come When we have shuffled off this mortal coil, Must give us pause: there's the respect That makes calamity of so long life; For who would bear the whips and scorns of time, The oppressor's wrong, the proud man's contumely, The pangs of despised love, the law's delay, The insolence of office and the spurns That patient merit of the unworthy takes, When he himself might his quietus make With a bare bodkin? who would fardels bear, To grunt and sweat under a weary life, But that the dread of something after death, The undiscover'd country from whose bourn No traveller returns, puzzles the will And makes us rather bear those ills we have Than fly to others that we know not of? Thus conscience does make cowards of us all; And thus the native hue of resolution Is sicklied o'er with the pale cast of thought, And enterprises of great pith and moment With this regard their currents turn awry, And lose the name of action. - Soft you now! The fair Ophelia! Nymph, in thy orisons Be all my sins remember'd.

= Example 2: fry2 = cat hamlet3.1 | head -n4 | tail -n 2 echo printf %04X\\n 10 printf "%d\n" 0xff echo tac hamlet3.1|tail -n1
 * 1) !/bin/bash
 * 1) fry2
 * 1) introducing printf, pipes, tail, tac

= Example 3: fry3 (user_shells)= cat /etc/passwd |cut -d: -f7 | sort| uniq | wc
 * 1) /bin/sh
 * 1) tools:
 * 2) cut, sort, uniq, wc
 * 1) This script tells me the number of shells used by users on this
 * 2) system

= Example 4: fry4 = nl hamlet3.1 | paste /etc/passwd -
 * 1) !/bin/bash
 * 1) intro paste, nl
 * 1) cat -n hamlet3.1 | paste /etc/passwd -

= Example 5: fry5 = head hamlet3.1 echo head hamlet3.1 | fold --width=40 echo head hamlet3.1 | fmt -w 40
 * 1) !/bin/sh
 * 1) intro fmt, fold (wraps), long opts

= Example 6: fry6 (newsprint)= DEBUG=true if [ "x" = "x$1" ] ; then # NEW: read builtin read -p "What is the filename? " FILE else FILE=$1 fi LINES_FILE=$(wc -l < $FILE) if [&#91; xtrue = x$DEBUG ]] ; then echo The file ${FILE} has ${LINES_FILE} lines; fi WHAT="What is the number of" if test 'x' = "${COLUMNS}x" then #echo -n $WHAT columns on this screen? echo -n $WHAT columns on this screen? ' '    read COLUMNS fi if test "$LINES"x == x ; then read -p "${WHAT} lines on this screen? " LINES fi if ${DEBUG} ; then echo col=$COLUMNS lin=$LINES fi WANTCOLS=2 WIDTH=$[ ${COLUMNS} / ${WANTCOLS} - 2 ] $DEBUG && echo colwid=$WIDTH $DEBUG && echo creating $FILE.tmp fmt -w $WIDTH ${FILE}> $FILE.tmp LINES_FILE=$(wc -l < ${FILE}.tmp) $DEBUG && echo The file ${FILE}.tmp has ${LINES_FILE} lines $DEBUG && echo splitting into tmp## split -dl $LINES $FILE.tmp tmp $DEBUG && echo pasting together paste tmp00 tmp01 $DEBUG && echo removing temp files rm tmp00 tmp01 $FILE.tmp
 * 1) !/bin/bash
 * 1) make a file read like a newspaper
 * 1) intro true, false, read builtin, test builtin, &&, `, $(), $[], >, <
 * 1) TODO:  check for commandline DEBUG
 * 1) DEBUG=false
 * 1) TODO:  verify file
 * 1) NEW: ``, $
 * 2) LINES_FILE=`wc -l $FILE` # nope!
 * 3) LINES_FILE=`cat $FILE |wc -l`
 * 4)  NEW: redirect
 * 1) NEW: true/false
 * 1) read -p "How many columns would you like $FILE to be in? " WANTCOLS
 * 2) read -p "How many columns would you like $FILE to be in? " WANTCOLS
 * 3) For now, 2 columns desired:
 * 1) NEW: $(), $[]
 * 2) WIDTH=$(( ${COLUMNS} / ${WANTCOLS} - 2 ))
 * 1) NEW: &&
 * 1) LINES_FILE=`cat $FILE.tmp |wc -l`
 * 2) LINES_FILE=$(cat $FILE.tmp |wc -l)
 * 3) LINES_FILE=$(wc -l $(echo ${FILE}).tmp) # nope!
 * 1) head -n $LINES $FILE.tmp > tmp00
 * 2) head -n $[2 * $LINES] $FILE.tmp | tail -n $LINES > tmp01
 * 3) NEW: split

= Example: forFont = for i in /usr/share/kbd/consolefonts/*; do       echo setfont $i sleep 1 done
 * 1) !/bin/bash

= Example: forPerformanceTest = for i in 1 2 3 do       time for j in $(seq 1000) do               $@ done done
 * 1) !/bin/bash

= Example: forRename = for i in *pp; do mv $i ${i/pp}; done

= Example: forThousands = for i in $(seq 5); do mkdir $i; done for i in $(seq 5); do for j in $(seq 5); do mkdir $i/$j;done; done; for i in $(seq 5); do for j in $(seq 5); do       entry=$i/$j echo $entry > $entry/${entry/\//.} done; done;

= Example: stats = 23 878 243 3 423 43 4 54 342 0 2 78 2 1 23 32 3 326 3 1221 2 98000

= Example: whileMenu = menu { echo "1) Check email"       echo "2) Surf web" echo "3) Edit file"       echo "4) Spreadsheet" echo "5) Exit" } menu while read -p "Enter an option: [1-5] " a do       case $a in                1) echo mutt ;;               one)    echo gmail                        ;;                2|ff) echo firefox ;; "3"|'4')       echo openoffice                        ;;                v)      echo vim;; '*')   echo exit                        ;;                *)      exit ;;       esac menu done
 * 1) !/bin/bash

= Example: whileReadStats = echo Products: sort -n < stats | while read a b       do                echo "$a x $b = $[ a * b]" done echo Remainders: while read c d; do       echo "$c mod $d = $[c % d ]" done < stats
 * 1) !/bin/bash