Frying Shakespeare (Bash Newbies)

From UCLUG
Revision as of 19:37, 6 May 2011 by Chruck (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Example 1: fry1 (review)[edit]

#!/bin/bash

# review

# Tools already used:
# echo, if, test, [, ls, cat, ;
# new tool:
# head

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?

Example: hamlet3.1[edit]

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[edit]

#!/bin/bash

# fry2

# introducing printf, pipes, tail, tac

cat hamlet3.1 | head -n4 | tail -n 2
echo
printf %04X\\n 10
printf "%d\n" 0xff
echo
tac hamlet3.1|tail -n1

Example 3: fry3 (user_shells)[edit]

#/bin/sh

# tools:
# cut, sort, uniq, wc

# This script tells me the number of shells used by users on this
# system

cat /etc/passwd |cut -d: -f7 | sort| uniq | wc

Example 4: fry4[edit]

#!/bin/bash

# intro paste, nl

nl hamlet3.1 | paste /etc/passwd -
#cat -n hamlet3.1 | paste /etc/passwd -

Example 5: fry5[edit]

#!/bin/sh

# intro fmt, fold (wraps), long opts

head hamlet3.1
echo
head hamlet3.1 | fold --width=40
echo
head hamlet3.1 | fmt -w 40

Example 6: fry6 (newsprint)[edit]

#!/bin/bash

# make a file read like a newspaper

# intro true, false, read builtin, test builtin, &&, `, $(()), $[], >, <

# TODO:  check for commandline DEBUG

#DEBUG=false
DEBUG=true

if [ "x" = "x$1" ] ; then
    #  NEW: read builtin
    read -p "What is the filename? " FILE
else
    FILE=$1
fi

# TODO:  verify file

# NEW: ``, $()
#LINES_FILE=`wc -l $FILE`  # nope!
#LINES_FILE=`cat $FILE |wc -l`
#  NEW: redirect
LINES_FILE=$(wc -l < $FILE)

if [[ 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
   
# NEW: true/false
if ${DEBUG} ; then
    echo col=$COLUMNS lin=$LINES
fi

#read -p "How many columns would you like $FILE to be in? " WANTCOLS
#read -p "How many columns would you like $FILE to be in? " WANTCOLS
# For now, 2 columns desired:
WANTCOLS=2

# NEW: $(()), $[]
#WIDTH=$(( ${COLUMNS} / ${WANTCOLS} - 2 ))
WIDTH=$[ ${COLUMNS} / ${WANTCOLS} - 2 ]

# NEW: &&
$DEBUG && echo colwid=$WIDTH

$DEBUG && echo creating $FILE.tmp
fmt -w $WIDTH ${FILE}> $FILE.tmp

#LINES_FILE=`cat $FILE.tmp |wc -l`
#LINES_FILE=$(cat $FILE.tmp |wc -l)
#LINES_FILE=$(wc -l $(echo ${FILE}).tmp)  # nope!
LINES_FILE=$(wc -l < ${FILE}.tmp)

$DEBUG && echo The file ${FILE}.tmp has ${LINES_FILE} lines

$DEBUG && echo splitting into tmp##
# head -n $LINES $FILE.tmp > tmp00
# head -n $[2 * $LINES] $FILE.tmp | tail -n $LINES > tmp01
# NEW: split
split -dl $LINES $FILE.tmp tmp

$DEBUG && echo pasting together
paste tmp00 tmp01

$DEBUG && echo removing temp files
rm tmp00 tmp01 $FILE.tmp

Example: forFont[edit]

#!/bin/bash

for i in /usr/share/kbd/consolefonts/*; do
       echo 
       setfont $i
       sleep 1
done

Example: forPerformanceTest[edit]

#!/bin/bash
for i in 1 2 3
do
       time for j in $(seq 1000)
       do
               [email protected]
       done
done

Example: forRename[edit]

for i in *pp; do mv $i ${i/pp}; done

Example: forThousands[edit]

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[edit]

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

Example: whileMenu[edit]

#!/bin/bash
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

Example: whileReadStats[edit]

#!/bin/bash

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