Bash comm ls l ls al total 12 rw rw r 1 bozo bozo 78

This preview shows page 342 - 346 out of 668 pages.

bash$comm <(ls -l) <(ls -al)total 12-rw-rw-r--1 bozo bozo78 Mar 10 12:58 File0-rw-rw-r--1 bozo bozo42 Mar 10 12:58 File2-rw-rw-r--1 bozo bozo103 Mar 10 12:58 t2.shtotal 20drwxrwxrwx2 bozo bozo4096 Mar 10 18:10 .drwx------72 bozo bozo4096 Mar 10 17:58 ..-rw-rw-r--1 bozo bozo78 Mar 10 12:58 File0-rw-rw-r--1 bozo bozo42 Mar 10 12:58 File2-rw-rw-r--1 bozo bozo103 Mar 10 12:58 t2.shUsing process substitution to compare the contents of two directories (to see which filenames are in one, butnot the other):diff <(ls $first_directory) <(ls $second_directory)Some other usages and uses of process substitution:cat <(ls -l)# Same asls -l | catChapter 22. Process Substitution336
sort -k 9 <(ls -l /bin) <(ls -l /usr/bin) <(ls -l /usr/X11R6/bin)# Lists all the files in the 3 main 'bin' directories, and sorts by filename.# Note that three (count 'em) distinct commands are fed to 'sort'.diff <(command1) <(command2)# Gives difference in command output.tar cf >(bzip2 -c > file.tar.bz2) $directory_name# Calls "tar cf /dev/fd/?? $directory_name", and "bzip2 -c > file.tar.bz2".## Because of the /dev/fd/<n> system feature,# the pipe between both commands does not need to be named.## This can be emulated.#bzip2 -c < pipe > file.tar.bz2&tar cf pipe $directory_namerm pipe#orexec 3>&1tar cf /dev/fd/4 $directory_name 4>&1 >&3 3>&- | bzip2 -c > file.tar.bz2 3>&-exec 3>&-# Thanks, St´phane ChazelasA reader sent in the following interesting example of process substitution.# Script fragment taken from SuSE distribution:while readdes what mask iface; do# Some commands ...done < <(route -n)# To test it, let's make it do something.while readdes what mask iface; doecho $des $what $mask $ifacedone < <(route -n)# Output:# Kernel IP routing table# Destination Gateway Genmask Flags Metric Ref Use Iface# 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo# As Stéphane Chazelas points out, an easier-to-understand equivalent is:route -n |while read des what mask iface; do# Variables set from output of pipe.echo $des $what $mask $ifacedone#This yields the same output as above.#However, as Ulrich Gayer points out . . .#+ this simplified equivalent uses a subshell for the while loop,#+ and therefore the variables disappear when the pipe terminates.#However, Filip Moritz comments that there is a subtle difference#+ between the above two examples, as the following shows.(Advanced Bash-Scripting GuideChapter 22. Process Substitution337
route -n | while read x; do ((y++)); doneecho $y # $y is still unsetwhile read x; do ((y++)); done < <(route -n)echo $y # $y has the number of lines of output of route -n)More generally spoken(: | x=x# seems to start a subshell like: | ( x=x )# whilex=x < <(:)# does not)# This is useful, when parsing csv and the like.# That is, in effect, what the original SuSE code fragment does.Advanced Bash-Scripting GuideChapter 22. Process Substitution338
Chapter 23. FunctionsLike "real" programming languages, Bash has functions, though in a somewhat limited implementation. Afunction is a subroutine, a code block that implements a set of operations, a "black box" that performs aspecified task. Wherever there is repetitive code, when a task repeats with only slight variations, then considerusing a function.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 668 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
Shell, scripting language, Shell script, C shell

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture