Requirement-Driven Linux Shell Programming
Table 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)?
Recently, I am doing a little project on linux filesystem, and some testing work must be done by high-level applications written in C or other script languages. When I am running the test cases, I find it annoying to do all these works by hand, so I start to learn the linux shell programming, and I decide to write the most important concept and skills down to remind myself or to provide those who are interested in shell programming with the starting material…
I will record my progress and the useful skills I learned when the project is carrying on in the Q&A form, which I think is a clean and simple way to organize 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 shell programming, 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 basic knowledge 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 quick look 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 want to 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 $1 4: } 5: 6: for ((i = 0;i < $num_process;i++)) 7: do 8: 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 to
integers, the tool "bc" is needed to do more advanced calculations.for example:
1: while read MINUTE SECOND 2: do 3: total=`echo "$MINUTE * 60 + $SECOND" | bc` 4: sum=`echo "$total + $sum" | bc` 5: done < ctar_load_sys 6: 7: avg=`echo "scale=10;$sum / $n" | bc`
In the above code segment, two self-defined variables(MINUTE, SECOND) are read from a file named "ctar_load_sys", they are used to get the total number in seconds, and after the while terminates, the average time is calculated with the last line of 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 rich
formats 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:
sum | avg | |
real | 2.36 | 2.35 |
6 How to print the destined(specific) line of a file?
Use the following command:
sed -n 1p |
7 How to covert column to row?
Say, I have a file(nofs) which has the following contents:
nofs 30.049 57.857 115.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:
tr ‘\n‘ ‘ ‘ < nofs |
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)
tail -n +2 filename |
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 following command 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: do 10: sed -n ${line_index}p "$1" | tr ‘ ‘ ‘\n‘ | awk ‘$1‘ > /tmp/a 11: paste $result_file_name /tmp/a > /tmp/b 12: cp /tmp/b $result_file_name 13: done 14: }