您的位置:首页 > 运维架构 > Shell

Requirement-Driven Linux Shell Programming

2014-03-04 20:38 337 查看
<?xml version="1.0" encoding="iso-8859-1"?>

Requirement-Driven Linux Shell Programming

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.

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?
7 How to pass parameters to a function?
8 How to calculate float point in Shell?
9 How to output nicer to the console, like a table?

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

4 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.

5 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:    ((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.

6 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

The above code prints a table on the console as follows:

 sumavg
real2.353.25
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.

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

8 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.

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

 sumavg
real2.362.35
Date:
Author: wujing
Org version 7.9.3f with
Emacs version 24
Validate XHTML 1.0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell linux