Requirement-Driven Linux Shell Programming
2014-03-02 17:48
239 查看
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.
2 How to time a function/program(Get the execution time of a program)?
3 How to use math expressions in Shell?
4 How to pass parameters to a function?
5 How to calculate float point in Shell?
6 How to output nicer to the console, like a table?
There are many places you can find such materials, and you can take a quick
look at it whenever you are in need.
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:
Note:the double quote is necessary, such expressions can be used in many
places ,like:
The above statement will first check whether the total command line parameters
received is only one, if there is exactly one parameter, the (($# == 1))
returns TRUE so that the "usage" function will not be executed due to the use
of "||". This is an efficient way to test the initial condition and exit with
error usage message when launching the script.
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.
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.
formats to choose from. For example:
The above code prints a table on the console as follows:
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.
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 use math expressions in Shell?
4 How to pass parameters to a function?
5 How to calculate float point in Shell?
6 How to output nicer to the console, like a table?
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.
How to time a function/program(Get the execution time of a program)?
Command Format: time the_program_to_be_testedNote: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 |
How to use math expressions in Shell?
Command Format:((math expression))Note:the double quote is necessary, such expressions can be used in many
places ,like:
1: (($# == 1)) || usage
The above statement will first check whether the total command line parameters
received is only one, if there is exactly one parameter, the (($# == 1))
returns TRUE so that the "usage" function will not be executed due to the use
of "||". This is an efficient way to test the initial condition and exit with
error usage message when launching the script.
How to pass parameters to a function?
Suppose the following code section:1: time_ctar_load() 2: { 3: time ./ctar_load $1 4: } 5: 6: for ((i = 0;i < $num_process;i++)) 7: do 8: time_ctar_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.
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.for
example:
1: while read MINUTE SECOND 2: do 3: ((n = $n + 1)) # account the total number of records 4: total=`echo "$MINUTE * 60 + $SECOND" | bc` 5: sum=`echo "$total + $sum" | bc` 6: done < ctar_load_real 7:
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.
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
The above code prints a table on the console as follows:
sum | avg | |
real | 2.35 | 3.25 |
相关文章推荐
- Requirement-Driven Linux Shell Programming
- Requirement-Driven Linux Shell Programming
- Requirement-Driven Linux Shell Programming
- Requirement-Driven Linux Shell Programming
- 《Shell Programming in Unix, Linux and OS X, 4th Edition 》 笔记之Can I Quote You On That?
- 《Shell Programming in Unix, Linux and OS X, 4th Edition 》 笔记之Decisions,Decisions
- Linux Shell programming
- Beginning Linux Programming----shell脚本程序实例
- An Introduction to Bash Shell Programming in Linux
- Novice learning resources for linux shell programming and vim
- 5 Shell Scripts for Linux Newbies to Learn Shell Programming – Part II
- Linux shell Programming
- Understanding Unix/Linux Programming 笔记:chapter 9:可编程的shell、shell变量和环境; chapter 10:I/O重定向和管道
- 5 Shell Scripts for Linux Newbies to Learn Shell Programming – Part II
- 《Shell Programming in Unix, Linux and OS X, 4th Edition 》 笔记之Passing Arguments
- Linux: 提示 Tips for shell programming
- [linux and unix shell programming] and [the unix environment programming]
- 《A Practical Guide to Linux Commands, Editors, and Shell Programming》之命令行解析
- Introduction to linux and shell programming
- Mathematical Aspect of Linux Shell Programming – Part IV