Requirement-Driven Linux Shell Programming

本文将介绍Linux Shell编程的基本概念、实用技巧及常见问题解决方案,包括如何找到学习资源、时间函数、数学表达式使用、参数传递、浮点数计算、美观输出等,并通过实例演示了函数调用、参数传递、浮点数计算和表格输出的方法。
摘要由CSDN通过智能技术生成
<?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.

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值