# /* vim: set filetype=sh : */ # usage: bash our shrc ########################################################################## # oursh : a simple learning environment for the bash shell # Copyright (C) 2007, Tim Menzies, tim@menzies.us, http://menzies.us # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 3. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . ########################################################################## Here=`pwd` #### generic stuff reload() { . $Ourrc; } show() { local goal1="^$1" local com="/^$1 /,/^}/{print}" if (set | grep $goal1 | grep "=" > /tmp/debug) then set | grep $goal1 else set | gawk "$com" fi } blab() { printf "$*" >&2; } blabln() { printf "$*\n" >&2; } #### initialization stuff setUp() { setUpVars setUpDirs } lcsee() { alias ls="ls --color" } setUpVars() { # Q0: after reading http://tldp.org/LDP/abs/html/internalvariables.html # write down what "PS1", "PROMPT_COMMAND" and "PATH" do alias ls="ls -G" PS1="Our BASH!: \!$ " PROMPT_COMMAND='echo -ne "\033]0;${HOSTNAME}: `pwd`\007"' Oursh=$HOME/opt/oursh PATH="$Oursh/bin:$HOME/bin:$PATH" Safe=$Oursh/var/safe } setUpDirs() { #Q1: Aftering reading http://www.pathname.com/fhs/pub/fhs-2.3.html, # write down what /opt /var /bin /lib are used for mkdir -p $HOME/tmp mkdir -p /tmp/$USER Tmp=`mktemp -d -p /tmp/$USER` mkdir -p $Oursh/lib # for support code mkdir -p $Oursh/bin # for our executables mkdir -p $HOME/bin # for your executables mkdir -p $Safe # for stuff you want to keep around } demo1() { # Q2: After reading http://www.linux-tutorial.info/modules.php?name=MContent&pageid=20 # write down what would happen if the double quotes, below, were replaced by single quotes echo $USER echo $HOME echo "$USER lives in $HOME" } demo2() { # Q3: After reading http://www.linux-tutorial.info/modules.php?name=MContent&pageid=20 # describe how the following uses "pipes" to find all files containing the letter "a". ls | grep a | wc } demo3() { # Q4: After reading http://www.linux-tutorial.info/modules.php?name=MContent&pageid=20 # catch the output of ls | wc. # write down what would happen if the double quotes, below, were replaced by single quotes echo $USER echo $HOME } demo4() { # Q5: After reading http://www.linux-tutorial.info/modules.php?name=MContent&pageid=20 # code demo4 to captures the output of demo2 into a string, then prints the string. # Hint: back ticks true } demo5() { # Q6: modify the following so that "yes" is echoed if there is a file in this directory # containing the word apple if ls | grep a > /dev/null; then echo yes else echo no fi } demo6() { # Q7: after reading http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/ # write down what this code does wibble > /dev/null 2>&1 } demo7() { # Q8: after reading http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/ # explain what is happenning here: for((I=1;I<=10;I++)); do blabln $I demo5 done | wc } demo8() { # Q9: after reading http://www.mktemp.org/mktemp/manual.html # explain the "mktemp -d -p" command mktemp -d -p /tmp } demo9() { # Q10: modify the following so that you change to the $Tmp directory, # generate some temp files, then trap the final report in $Safe for((i=1;i<=10;i++)); do demo5 | wc > $i.out done wc *.out } demo10() { # Q11: after reading http://linux.byexamples.com/archives/category/common/tee/ # modify this demo with the "tee" command to catch the output of the "cut". # Explain the final command line demo13 | cut -d, -f 4 | sort | uniq -c } demo11() { # Q12: type "man tr" at a unix prompt. Write "demo11" that turns all characters # in a file to upper case true } demo12() { # Q13: after reading http://www.grymoire.com/Unix/Sed.html#uh-1 # write demo12 that turns "day" into "night" true } demo13() { # This is a "here" document. It is like "cat" but the contents of the file, # less leading blankspace, is written to the screen cat<<-EOF 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 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 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 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 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 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 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 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 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 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 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 EOF } demo14() { # Q14: using "cut -d, ", isolate columns 1 and 4 20 from the output of demo13, # using "sort -t,", sort those two columns descending by column1 and # ascending by column 20 then descending by column 4. Hint, the following is wrong demo13 | cut -d, -f 1 | sort -t, } demo15() { # Q15: same as demo14 but only print the "Biuck" results : hint grep true } demo16() { # Q16: same as demo14 but only print lines without "Biuck" . hint man grep true } demo17() { #Q17: write a new version of demo5 where the "grep" runs on a user supplied # paramters, called like (e.g.) demo17 b true } demo18() { #Q18: google "bash set +x" and find out how to trace a bash program. # Hand in a trace of demo5 true } demo19() { #Q20: show the output of this demo. explain the code, with comments. add a flag to only print the last # N lines (hint: use "tail") local ignore="someImpossibleSYMbol" local sortby=2 while [ `echo $1 | grep "-"` ]; do case $1 in -h|--help) echo "this is the help text";; -v|--version) echo "demo19worker version 0.0001";; -s|--sortby) sortby=$2;; -i|--ignore) ignore=$2;; *) blabln "'"$1"' unknown\n usage demo19workder [options]"; return 1;; esac shift 2 done ls -lsa | grep -v "$ignore" | sort -n -k $sortby,$sortby } #### start up setUp blabln "OurSh version v0.1 (c)2007 tim@menzies.us under GPLv3" echo "Tmp=$Tmp" blabln "Reach out and grep someone.\n"