博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Requirement-Driven Linux Shell Programming
阅读量:5328 次
发布时间:2019-06-14

本文共 5647 字,大约阅读时间需要 18 分钟。

<?xml version="1.0" encoding="utf-8"?> Requirement-Driven Linux Shell Programming

Requirement-Driven Linux Shell Programming

Table of Contents

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 $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 to integers, the tool "bc" is needed to do more advanced calculations.for example:

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 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: 

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:

https://images0.cnblogs.com/blog/598435/201405/011148135806761.jpg

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: }

Author: wujing

Created: 2014-05-01 四 11:48

24.3.1 ( mode 8.2.6)

转载于:https://www.cnblogs.com/wujingcqu/p/3702666.html

你可能感兴趣的文章
windos系统定时执行批处理文件(bat文件)
查看>>
thinkphp如何实现伪静态
查看>>
BZOJ 2243: [SDOI2011]染色( 树链剖分 )
查看>>
BZOJ 1925: [Sdoi2010]地精部落( dp )
查看>>
c++中的string常用函数用法总结!
查看>>
界面交互之支付宝生活圈pk微信朋友圈
查看>>
[DLX精确覆盖+打表] hdu 2518 Dominoes
查看>>
SuperMap iServerJava 6R扩展领域开发及压力测试---判断点在那个面内(1)
查看>>
Week03-面向对象入门
查看>>
一个控制台程序,模拟机器人对话
查看>>
web.xml 中加载顺序
查看>>
pycharm激活地址
查看>>
hdu 1207 四柱汉诺塔
查看>>
Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(上篇——纯前端多页面)
查看>>
display:none与visible:hidden的区别
查看>>
我的PHP学习之路
查看>>
【题解】luogu p2340 奶牛会展
查看>>
对PostgreSQL的 SPI_prepare 的理解。
查看>>
解决响应式布局下兼容性的问题
查看>>
京东静态网页练习记录
查看>>