Exercise #3 - this has morphed from an in class exercise (the pipeline at the end of the file) to a holder of examples that we might have talked about in class or that we might not. You should assume questions related to this material is fair game for quizes or exams.
Refer to this page for additional examples of pipelines...
#what will this do ?
exec 1> >(logger –s –t $(basename $0)) 2>&1
Example : there's a program that is hung and we want to kill it using commands on a single command line. Assume the hung program is perfClient. This is more conmpletely described here : page for additional examples of pipelines...
#why are quotes not needed in the grep regexp ?
# decompose by looking in to : echo $(ps aux | grep [p]erfClient | awk '{print $2}')
kill -9 $(ps aux | grep '[p]erfClient' | awk '{print $2}') #Note: the [p] is a 'trick' to remove the unwanted second pid from the grep - which won't exist by the time the $() is done
#
#a more straight forward solution:
kill -9 $(ps aux | grep 'perfClient' | grep -v 'grep' | awk '{print $2}')
history | grep kill
we decompose a non-trivial find command. For reference, the top level lecture material on Bash is here : https://people.cs.clemson.edu/~jmarty/courses/Spring-2017/CPSC424/lectures/BashScriptingTopLevelGuide.html
Refer to this page for additional examples of pipelines...
we decompose a non-trivial find command. For reference, the top level lecture material on Bash is here : https://people.cs.clemson.edu/~jmarty/courses/Spring-2017/CPSC424/lectures/BashScriptingTopLevelGuide.html
Step 1 : download a set of data files. This creates a directory structure ./PB1 with three myFile* subdirs each with a small number of non zero files.
Step 2 run the the following (the results are shown)
time echo `find . -type f -exec stat -c %s {} \; | tr '\n' '+' | sed 's/+$//g'` | bc
183890
real 0m0.060s
user 0m0.000s
sys 0m0.004s
Step 3: decompose the command to understand what each part does.
From the page ../lectures/CommonCommandsAndFilters.html:
Let's break it down.....run just the first part:
find . -type f -exec stat -c %s {} \;
660
426
426
426
22645
426
660
182
182
22645
182
660
22645
1865
1865
1865
1865
1865
102400
The -type f is an expression that limits the files that are found to be regular files
The -exec stat.... is an action. The find command will create a subshell to run the command 'stat -c %s '
From 'man stat' we learn it provides info about one or more files. The '-c %s' indicates we only want the size of files.
Next....let's do the first pipe:
find . -type f -exec stat -c %s {} \; | tr '\n' '+'
660+426+426+426+22645+426+660+182+182+22645+182+660+22645+1865+1865+1865+1865+1865+102400+
tr is a useful filter for translating or deleting haracters from standard input...writing the result to standard output. By default, the first set of characters will be removed, the second set will be added.
Next....pipe to sed...
find . -type f -exec stat -c %s {} \; | tr '\n' '+' | sed 's/+$//g'
660+426+426+426+22645+426+660+182+182+22645+182+660+22645+1865+1865+1865+1865+1865+102400jjm@jjm-VirtualBox:~/courses/cpsc424/Spring-2017/ex3$
sed: stream editor for filtering and transforming text
In our example, sed 's/+$//g' : this is search for a plus ('+') at the end of a line, and replace with a blank (from the two consecutive forward slashes), and the g indicates do this for all lines in the file (global). So....this simply removes the final "+"
Almost there...
bc is a calculator filter.... by default it adds the data from standard in. If we issue
find . -type f -exec stat -c %s {} \; | tr '\n' '+' | sed 's/+$//g' | bc
we get a syntax error ..... Notice the find is surrounded by backticks .... this tells the shell that the contents is to be treated by the shell as a command with its
If we run the above surrounded by backticks :
`find . -type f -exec stat -c %s {} \; | tr '\n' '+' | sed 's/+$//g'` | bc
660+426+426+426+22645+426+660+182+182+22645+182+660+22645+1865+1865+1865+1865+1865+102400: command not found
A backtick tells the shell to evaluate (or execute) what is in the backticks before calling the main command .... so this is why we get the command not found error
time echo `find . -type f -exec stat -c %s {} \; | tr '\n' '+' | sed 's/+$//g'` | bc
So the echo is used to correctly pipe the results to bc.
The time will tell the shell to monitor the resources (in time) used by the command.
Step 4 Summarize how the command works . This pipeline will look at all regular files recursively, starting at the current dir, and sum the size in bytes of each file found.
Last update: 2/18/2017