1 # /* vim: set filetype=sh : */
  2 # usage: bash our shrc
  3 ##########################################################################
  4 #    oursh : a simple learning environment for the bash shell
  5 #    Copyright (C) 2007, Tim Menzies, tim@menzies.us, http://menzies.us
  6 #
  7 #    This program is free software: you can redistribute it and/or modify
  8 #    it under the terms of the GNU General Public License as published by
  9 #    the Free Software Foundation, version 3.
 10 #
 11 #    This program is distributed in the hope that it will be useful,
 12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 #    GNU General Public License for more details.
 15 #
 16 #    You should have received a copy of the GNU General Public License
 17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 18 ##########################################################################
 19
 20 Here=`pwd`
 21
 22 #### generic stuff
 23 reload() { . $Ourrc;  }
 24 show() {
 25     local goal1="^$1"
 26     local com="/^$1 /,/^}/{print}"
 27     if   (set | grep $goal1 | grep "=" > /tmp/debug)
 28     then set  | grep $goal1
 29     else set  | gawk "$com"
 30     fi
 31 }
 32
 33 blab()   { printf "$*"    >&2; }
 34 blabln() { printf "$*\n" >&2; }
 35
 36 #### initialization stuff
 37 setUp() {
 38     setUpVars
 39     setUpDirs
 40 }
 41 lcsee() {
 42     alias ls="ls --color"
 43 }
 44 setUpVars() {
 45     # Q0: after reading http://tldp.org/LDP/abs/html/internalvariables.html
 46     #     write down what "PS1", "PROMPT_COMMAND" and "PATH" do
 47     alias ls="ls -G"
 48     PS1="Our BASH!: \!$ "
 49     PROMPT_COMMAND='echo -ne "\033]0;${HOSTNAME}: `pwd`\007"'
 50     Oursh=$HOME/opt/oursh
 51     PATH="$Oursh/bin:$HOME/bin:$PATH"
 52     Safe=$Oursh/var/safe
 53 }
 54 setUpDirs() {
 55     #Q1: Aftering reading http://www.pathname.com/fhs/pub/fhs-2.3.html,
 56     #    write down what /opt /var /bin /lib are used for
 57     mkdir -p $HOME/tmp
 58     mkdir -p /tmp/$USER
 59     Tmp=`mktemp -d -p /tmp/$USER`
 60     mkdir -p $Oursh/lib # for support code
 61     mkdir -p $Oursh/bin # for our executables
 62     mkdir -p $HOME/bin  # for your executables
 63     mkdir -p $Safe      # for stuff you want to keep around
 64 }
 65 demo1() {
 66     # Q2: After reading http://www.linux-tutorial.info/modules.php?name=MContent&pageid=20
 67     #     write down what would happen if the double quotes, below, were replaced by single quotes
 68     echo $USER
 69     echo $HOME 
 70     echo "$USER lives in $HOME"
 71 }
 72 demo2() {
 73     # Q3: After reading http://www.linux-tutorial.info/modules.php?name=MContent&pageid=20
 74     #     describe how the following uses "pipes" to find all files containing the letter "a".
 75     ls | grep a | wc
 76 }
 77 demo3() {
 78     # Q4: After reading http://www.linux-tutorial.info/modules.php?name=MContent&pageid=20
 79     #     catch the output of ls | wc.
 80     #     write down what would happen if the double quotes, below, were replaced by single quotes
 81     echo $USER
 82     echo $HOME 
 83 }
 84 demo4() {
 85     # Q5: After reading http://www.linux-tutorial.info/modules.php?name=MContent&pageid=20
 86     #     code demo4 to captures the output of demo2 into a string, then prints the string.
 87     #     Hint: back ticks
 88     true
 89 }
 90 demo5() {
 91     # Q6: modify the following so that "yes" is echoed if there is a file in this directory
 92     #     containing the word apple
 93     if ls | grep a > /dev/null; then
 94         echo yes
 95     else echo no
 96     fi
 97 }
 98 demo6() {
 99     # Q7: after reading http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/
100     #     write down what this code does
101     wibble > /dev/null 2>&1
102 }
103 demo7() {
104     # Q8: after reading http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/
105     #     explain what is happenning here:
106     for((I=1;I<=10;I++)); do
107         blabln $I
108         demo5
109     done  | wc
110 }
111 demo8() {
112     # Q9: after reading http://www.mktemp.org/mktemp/manual.html
113     #     explain the "mktemp -d -p" command
114     mktemp -d -p /tmp
115 }
116 demo9() {
117     # Q10: modify the following so that you change to the $Tmp directory,
118     #      generate some temp files, then trap the final report in $Safe
119     for((i=1;i<=10;i++)); do 
120             demo5 | wc > $i.out
121     done  
122     wc *.out
123 }
124 demo10() {
125     # Q11: after reading http://linux.byexamples.com/archives/category/common/tee/
126     #      modify this demo with the "tee" command to catch the output of the "cut".
127     #      Explain the final command line  
128     demo13 | cut -d, -f 4 | sort | uniq -c
129 }
130 demo11() {
131     # Q12: type "man tr" at a unix prompt. Write "demo11" that turns all characters
132     #      in a file to upper case
133     true
134 }
135 demo12() {
136     # Q13: after reading http://www.grymoire.com/Unix/Sed.html#uh-1
137     #      write demo12 that turns "day" into "night"
138     true
139 }
140 demo13() {
141     # This is a "here" document. It is like "cat" but the contents of the file,
142     #  less leading blankspace, is written to the screen
143     cat<<-EOF 
144     Cadillac,Large,16,25,1,1,8,4.9,200,4100,1510,0,18,6,206,114,73,43,35,18,3620,1,34.7
145     Acura,Small,25,31,0,1,4,1.8,140,6300,2890,1,13.2,5,177,102,68,37,26.5,11,2705,0,15.9
146     Buick,Large,16,25,1,0,6,5.7,180,4000,1320,0,23,6,216,116,78,45,30.5,21,4105,1,23.7
147     Acura,Midsize,18,25,2,1,6,3.2,200,5500,2335,1,18,5,195,115,71,38,30,15,3560,0,33.9
148     Audi,Midsize,19,26,2,1,6,2.8,172,5500,2535,1,21.1,6,193,106,70,37,31,17,3405,0,37.7
149     Buick,Large,19,28,1,1,6,3.8,170,4800,1570,0,18,6,200,111,74,42,30.5,17,3470,1,20.8
150     BMW,Midsize,22,30,1,0,4,3.5,208,5700,2545,1,21.1,4,186,109,69,39,27,13,3640,0,30
151     Buick,Midsize,22,31,1,1,4,2.2,110,5200,2565,0,16.4,6,189,105,69,41,28,16,2880,1,15.7
152     Buick,Midsize,19,27,1,1,6,3.8,170,4800,1690,0,18.8,5,198,108,73,41,26.5,14,3495,1,26.3
153     Audi,Compact,20,26,1,1,6,2.8,172,5500,2280,1,16.9,5,180,102,67,37,28,14,3375,0,29.1
154     Cadillac,Midsize,16,25,2,1,8,4.6,295,6000,1985,0,20,5,204,111,74,44,31,14,3935,1,40.1
155 EOF
156 }  
157 demo14() {
158     # Q14: using "cut -d, ", isolate columns 1 and 4 20  from the output of demo13,
159     #      using "sort -t,", sort those two columns  descending by column1 and
160     #      ascending by column 20 then descending by column 4. Hint, the following is wrong
161     demo13 | cut -d, -f 1 | sort -t,
162 }
163 demo15() {
164     # Q15: same as demo14 but only print the "Biuck" results : hint grep
165     true
166 }
167 demo16() {
168     # Q16: same as demo14 but only print lines without "Biuck" . hint man grep
169     true
170 }
171 demo17() {
172         #Q17: write a new version of demo5 where the "grep" runs on a user supplied
173         #     paramters, called like (e.g.) demo17 b
174         true
175 }
176 demo18() {
177         #Q18: google "bash set +x" and find out how to trace a bash program.
178         #     Hand in a trace of demo5
179         true
180 }
181 demo19() {
182     #Q20: show the output of this demo. explain the code, with comments. add a flag to only print the last
183     #      N lines (hint: use "tail")
184
185     local ignore="someImpossibleSYMbol"
186     local sortby=2
187     while [ `echo $1 | grep "-"` ]; do
188        case $1 in
189             -h|--help)    echo "this is the help text";;
190             -v|--version) echo "demo19worker version 0.0001";;
191             -s|--sortby)  sortby=$2;;
192             -i|--ignore)  ignore=$2;;
193             *)            blabln "'"$1"' unknown\n usage demo19workder [options]"; 
194                           return 1;;
195        esac
196        shift 2
197     done
198     ls -lsa | grep -v "$ignore" | sort   -n -k $sortby,$sortby
199 }
200
201 #### start up
202 setUp
203 blabln "OurSh version v0.1 (c)2007 tim@menzies.us under GPLv3"
204 echo "Tmp=$Tmp"
205 blabln "Reach out and grep someone.\n"