Subcontractors – bc , xargs , find
David Morgan
© David Morgan 2011- 14
bc – math help for the shell
interactive or programatic can accept its commands from stdin can accept an entire bc program’s worth
© David Morgan 2011-14
1 bc – math help for shell
bc, commanded interactively by user does what shell cannot e.g. high-precision math ( & much more ) bc, commanded non-interactively by shell
shell, by extension, does high-precision
can embed bc code wholesale as here document
© David Morgan 2011-14
bc – shell script or bc interpreter script
© David Morgan 2011-14
2 bc – math help for shell
bc code, not bash shell code
background: http://jeremykun.com/tag/simpsons-rule http://www.zweigmedia.com/RealWorld/integral/integral.html © David Morgan correct! 2011-14
bc – interactive math help for shell
Wicked Cool Shell Scripts , Dave Taylor, p29
© David Morgan 2011-14
3 xargs
produce list of vi’s backup files
long list ‘em with xargs
remove ‘em with xargs gone
© David Morgan 2011-14
xargs filename too long to type command to avoid typing it
what’s in the file? …oops cat needs filename as argument, not input xargs composes correct cat syntax by pasting its input onto cat’s command line produces: cat /root/class… etc etc
produces: cat /etc/resolv.conf /root/class… etc etc
xargs composes incorrect cp syntax by pasting its input onto cp’s command line produces: cp . /root/class… etc etc cp differs from cat, needs insertion from xargs not pasting
control argument placement with xargs’ –I (xargs stuff goes where the special© char David appears, _ in this case) Morgan 2011-14
4 xargs and efficiency
sleeptime totals 15 sec run sleeps simultaneously took 15 sec
took 5 sec
significant for compute-heavy commands distributes them across multi-cores – simultaneous execution © David Morgan 2011-14
xargs and efficiency
commonly used with find to process files functionally equivalent – find . -name "*" -exec rm {} \; – find . -name "*" | xargs rm computationally different – -exec -rm creates one process per file – xargs rm creates one process per group of files
© David Morgan 2011-14
5 xargs and efficiency
12 times longer to delete 50 files
© David Morgan 2011-14
xargs
spaces in filenames
space and newline are delimiters to xargs
find output-delimits with 0a
but can use 00 instead of 0a
and xargs can input-delimit on 00 instead of 20 (space)
© David Morgan 2011-14
6 find
searches for files in a directory tree described by an expression expression consists of elements – options – tests – actions each element returns boolean result find evaluates as many elements of its expression as needed to know expression’s outcome © David Morgan 2011-14
Most common use for
if
but the operation details are more complex than that © David Morgan 2011-14
7 find example
expression
find . -maxdepth 1 -size +1000000c -print
an optiona test an action
find files 1) in the current directory (no subdirectory search) 2) bigger than a million bytes © David 3) and print their names Morgan 2011-14
Some example elements
find . options* tests actions
maxdepth name print mount atime+n ls etc size +n exec executable ok type etc empty false etc © David * find’s options, not shell command options Morgan 2011-14
8 Some example elements
find . options tests actions
what it returns: true always true or false true or false
what it does: influence nothing their particular find behavior action
© David Morgan 2011-14
Operational logic
"[evaluates] the given expression from left to right... until the outcome is known (the left hand side is false for and operations, true for or ), at which point find moves on to the next file name.“ - “find” man page
© David Morgan 2011-14
9 Operational logic
because -name “A*” is false for B* files
because printing happens before -name “A*” evaluation
from the 2 nd -print (2 nd print doesn’t happen for B* files) © David Morgan 2011-14
exec action – arbitrary response for qualifying files
needs to be terminated with ; uses {} as placeholder for current file need to escape these from shell
a “finder” script command:
find . -type f –exec grep –l “$1” {} \;
print names of all files in current directory containing a given string © David Morgan 2011-14
10