標籤:des style blog http java color
Requirement-Driven Linux Shell ProgrammingRequirement-Driven Linux Shell ProgrammingTable of Contents
- 1. Where can I find the basic Material about Linux Shell Programming?
- 2. How to time a function/program(Get the execution time of a program)?
- 3. How to pass parameters to a function?
- 4. How to calculate float point in Shell?
- 5. How to output nicer to the console, like a table?
- 6. How to print the destined(specific) line of a file?
- 7. How to covert column to row?
- 8. How to output a certain lines of a file?(Say, all but the first line)
- 9. How to list(copy) all the executable files in a directory?
- 10. The short key for adding source block in org mode:
- 11. How to remove the duplicated newlines in a file?
- 12. How to transpose a file(to convert all the colums to rows)?
- 13. How to 向上取整 an floating point number?
- 14. How to sum row of numbers?
Recently, I am doing a little project on linux filesystem, and some testingwork must be done by high-level applications written in C or other scriptlanguages. When I am running the test cases, I find it annoying to do all theseworks by hand, so I start to learn the linux shell programming, and I decide towrite the most important concept and skills down to remind myself or to providethose who are interested in shell programming with the starting material…
I will record my progress and the useful skills I learned when the project iscarrying on in the Q&A form, which I think is a clean and simple way toorganize my thoughts and that, I hope, won‘t take me too much time…
Note:What I will cover in this post is not the step-by-step tutorial for shellprogramming, but rather the kind of problems one will meet in a real project,and I make the assumption that the readers are already equipped with basicknowledge about the shell script.
1 Where can I find the basic Material about Linux Shell Programming?
I found this tutorial useful,A Beginner‘s Handbook for Linux Shell Scripting.There are many places you can find such materials, and you can take a quicklook at it whenever you are in need.
2 How to time a function/program(Get the execution time of a program)?
Command Format: time the_program_to_be_tested
Note:the output of "time" command will be the STDERR_FILENO,in my case, I wantto redirect the output to a file, so the following command is used:
time ./the_program_to_be_tested > result 2>&1 |
3 How to pass parameters to a function?
Suppose the following code section:
1: time_xtar_load()2: {3: time ./xtar_load $14: }5: 6: for ((i = 0;i < $num_process;i++))7: do8: time_xtar_load $i &9: done
In this example, a function named time_xtar_load is defined, and the "for"statement will evoke the function many times each with a different parameter,that is, the running value of i.
4 How to calculate float point in Shell?
The default behavior of the shell to the calculation works is limited tointegers, the tool "bc" is needed to do more advanced calculations.forexample:
1: while read MINUTE SECOND2: do3: total=`echo "$MINUTE * 60 + $SECOND" | bc`4: sum=`echo "$total + $sum" | bc`5: done < ctar_load_sys6: 7: avg=`echo "scale=10;$sum / $n" | bc`
In the above code segment, two self-defined variables(MINUTE, SECOND) are read from a filenamed "ctar_load_sys", they are used to get the total number in seconds, andafter the while terminates, the average time is calculated with the last lineof code.Note that the "scale=10" is necessary to do the decimal computation.
5 How to output nicer to the console, like a table?
The "printf" command can do exactly what we can do in C/C++, it has richformats to choose from. For example:
1: header="\n %-10s %10s %10s\n"2: format=" %-10s %10.3f %10.3f\n"3: printf "$header" " " "sum" "avg"4: printf "$format" 5: real $sum $avg 6: sys $sum $avg
The above code prints a table on the console as follows:
6 How to print the destined(specific) line of a file?
Use the following command:
7 How to covert column to row?
Say, I have a file(nofs) which has the following contents:
nofs30.04957.857115.718
and I want to covert it to one row, there are many ways to do it:
1.The awk way:
cat nofs |
awk ‘{printf("%s ",$0)}‘ |
2.The tr way:
3.The echo:
echo $(<nofs) |
xargs echo < nofs |
cat nofs xargs |
All of the above methods will output like:
nofs 30.049 57.857 115.718 |
8 How to output a certain lines of a file?(Say, all but the first line)
will output the file contents from the second line.
9 How to list(copy) all the executable files in a directory?
List:
1: find . -executable -type f
Copy:
1: find . -executable -type f | xargs -i cp {} /tmp
10 The short key for adding source block in org mode:
1: <s TAB
11 How to remove the duplicated newlines in a file?
I get a file with the following format:
write rewrite read re-read randread randwrite
32149.29 35625.84 37261.05 37575.87 713.54 1606.12
57465.48 62242.62 66024.14 66969.32 1430.75 3365.54
89339.57 96388.80 104037.65 103917.83 2714.76 6479.37
128849.01 132933.47 133952.92 134381.05 5482.03 12207.46
126489.87 128470.04 129826.87 130348.56 10446.49 24424.78
127078.48 128236.06 128642.23 129169.20 19505.88 40367.71
126942.59 125676.09 128773.13 128896.08 32061.20 63187.50
127596.32 128102.95 128516.09 129485.38 51048.20 82756.15
126424.50 127839.95 128780.78 129846.62 72924.07 97219.43
125817.65 127208.07 128134.88 129057.43 92172.60 85037.41
128768.69 129389.45 130342.52 131162.48 107949.98 100109.50
127326.84 128567.19 129666.99 129796.52 117193.84 108822.87
And I just want to remove all the empty lines of the file, the followingcommand can be used:
sed ‘/^$/d‘ input_file_name > output_file_name |
or
awk ‘$1‘ input_file_name > output_file_name |
The command will output the following result:
12 How to transpose a file(to convert all the colums to rows)?
Use the following script:
1: transpose_file() 2: { 3: lines_of_file=`wc -l < "$1"` 4: echo $lines_of_file 5: result_file_name="result" 6: >$result_file_name 7: 8: for ((line_index = 1;line_index < $lines_of_file + 1;line_index++)) 9: do10: sed -n ${line_index}p "$1" | tr ‘ ‘ ‘\n‘ | awk ‘$1‘ > /tmp/a11: paste $result_file_name /tmp/a > /tmp/b12: cp /tmp/b $result_file_name13: done14: }
13 How to 向上取整 an floating point number?
Using the following command:
echo 1.0 |
awk ‘{print int($1)==$1?$1:int(int($1*10/10+1))}‘ |
14 How to sum row of numbers?
I found the awk very powerful.
Assume the data file has the following contents:
32.06 31.861 31.746 31.775 31.768 |
31.809 31.999 31.856 32.042 31.865 |
31.892 31.967 32.171 32.024 32.138 |
32.187 32.648 32.507 32.445 32.245 |
36.082 34.84 34.706 34.851 34.771 |
47.023 46.323 47.694 46.401 47.544 |
92.666 96.653 96.461 93.972 96.401 |
The following line can achieve just this:
awk ‘{for(i=1;i<=NF;i++) t+=$i; print t/NF; t=0}‘ < t-cpu-io |
31.842 |
31.9142 |
32.0384 |
32.4064 |
35.05 |
46.997 |
95.2306 |
Author: wujing
Created: 2014-07-17 四 19:55
Emacs 24.3.1 (Org mode 8.2.6)
Validate