requirement-driven linux shell programming
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.
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.
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:
<col>
time ./the_program_to_be_tested > result
2>&1
suppose the following code section:
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.
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:
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.
the "printf" command can do exactly what we can do in c/c++, it has rich
formats to choose from. for example:
the above code prints a table on the console as follows:
sum
avg
real
2.36
2.35
use the following command:
sed -n 1p
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:
tail -n +2 filename
will output the file contents from the second line.
list:
copy:
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:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicGcq5SN3gjMyUzNyEDO0ETMxAzLcVDM0EDMy8CX1MDN4kTNvw1ZvxmYvwVbvNmLn9GbiRXauNmLzV2Zh1Wavw1LcpDc0RHaiojIsJye.jpg)
use the following script:
author: wujing
created: 2014-05-01 四 11:48
24.3.1
( mode 8.2.6)