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
  • 相关阅读:
    sql 笔记之一
    js的Location
    VS无法使用.XXXX附加到程序
    C#遍历对象的方法
    解决tomcat启动时中文乱码问题。
    动态sql foreach 循环报错问题
    java跳过https证书直接请求工具类
    安装svn报2503错误处理方法
    oracle常用的一些查询命令
    Oracle11g安装过程中忘记进行口令配置
  • 原文地址:https://www.cnblogs.com/wujingcqu/p/3577075.html
Copyright © 2011-2022 走看看