zoukankan      html  css  js  c++  java
  • 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.

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

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

    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="
     %-10s %10s %10s
    "
    2:  format=" %-10s %10.3f %10.3f
    "
    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
  • 相关阅读:
    Win7 SP1 安装SQL Server 2012时提示“此计算机上的操作系统不符合 SQL Server 2012的最低要求”
    ajax jsonp跨域
    Caused by: Unable to locate parent package [json-package] for [class com.you.action.ColumnAction]
    PHP MVC自己主动RBAC自己主动生成的访问路由
    Service与Activity与交流AIDL
    SVN常见错误两项纪录
    EL表达式语言
    oracle11g ASM(修复损坏的磁盘组头asm修复2)
    如何使用iOS 8 指纹识别,代码、示例
    EXCEL Pivot table manipulate
  • 原文地址:https://www.cnblogs.com/wujingcqu/p/3577075.html
Copyright © 2011-2022 走看看