Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

Search:

CA249      CA318      CA425      CA651

w2mind.computing.dcu.ie      w2mind.org

Missing
DCU student

CASE3 student Paul Bunbury is missing since Thur 2 Feb 2012.
See appeals on crime.ie and garda.ie and facebook.

He is a great coder. See DCU page and boards.ie page.
He won major coding contests in 2010 and 2011.
He is author of the brilliant "FloodItWorld".
DCU can confirm that in Jan 2012 he passed all 6 modules comfortably.


Shell functions

Shell scripts do not have objects but do have functions.
Functions are more efficient than having scripts calling other scripts.



Example

A program to calculate how much space your web pages take up, as a percent of your total file space. Defines 2 functions, then uses them.


# percent $1 $2
# returns $1 as a percentage of $2

percent()
{
 temp=`expr $1 \* 100`
 expr $temp / $2
}


# sizeof dir
# returns size of all files in dir and below

sizeof()
{
 cd $1
 du | grep '\.$' | cut -f1 
}


web=`sizeof $HOME/public_html`
echo $web

total=`sizeof $HOME`
echo $total

ans=`percent $web $total`
echo "Your web files are $ans% of your total files."

On DCU Linux, du shows file size in k.
See also GUI - Disk Usage Analyser




Call function which is defined in another file

fnsfile:

   fn() 
   { 
    echo test 
   }

prog:

   # include functions defined in fnsfile:

   		. fnsfile
   # 		source fnsfile

   # can now call them:

   fn



Environment Variables

By default, functions have access to the caller's environment variables without exporting:

fn()
{
 echo $x
}

x=3

fn
Whereas if we call a separate script, it does not by default have access to the caller's environment variables.




Piping to a function:

You can pipe to functions too (in general you can treat them just like a separate program).

Consider a program:


#  killstring (string)
# kills all instances of program named by string
# e.g. 
#  killstring textedit
# kills all your textedit processes

If "killstring" first tries to identify these processes:
  ps -Tf | grep -i $1
when you type "killstring textedit" you get something like:
humphrys 28414 28413  0 12:32:50 pts/5    0:00 grep -i textedit
humphrys 28396     1  0 12:32:19 pts/23   0:00 textedit -geometry 600x600+200-100 f1
humphrys 28352     1  0 12:31:51 pts/23   0:00 textedit -geometry 600x600+200-100 f2
humphrys 28366     1  0 12:31:51 pts/23   0:00 textedit -geometry 600x600+200-100 f3
humphrys 28413 28073  0 12:32:50 pts/5    0:00 /bin/sh killstring textedit
so we need to strip out killstring itself, and the grep itself. We also have a number of unwieldy lines of text, which we want to process one by one, to extract the process ID and kill it. We can do this by piping to a function. So killstring looks like:

fn()
{
 while read user pid restofline
 do
  kill $pid
 done
}


ps -Tf | grep -i $1 | grep -v $0 | grep -v "grep -i $1" | fn




Feeds      HumphrysFamilyTree.com

Bookmark and Share           On Internet since 1987.