Requirement-driven Linux Shell Programming

Source: Internet
Author: User

<title>Requirement-driven Linux Shell Programming</title> Requirement-driven Linux Shell programmingtable 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 do I pass parameters to a function?
    • 4. How to calculate a float point in Shell?
    • 5. How to output nicer to the console, like a table?
    • 6. How to print the destined (specific) line of a file?
    • 7. How do I covert column to row?
    • 8. How to output a certain lines of a file? (Say, all and the first line)
    • 9. How to List (copy) all the executable files in a directory?
    • The short key for adding source block in org mode:
    • How do I remove the duplicated newlines in a file?
    • What is the transpose a file (to-Convert all the colums to rows)?
    • How do I take a floating point number up?

Recently, I am doing a little project on Linux filesystem, and some testingwork must is done by high-level applications WR Itten in C or other scriptlanguages. When I am running the test cases, I-find it annoying to does all theseworks by hand, so I start to learn the Linux Shell Pro Gramming, and I decide towrite the most important concept and skills down to remind myself or to providethose who is inte Rested in shell programming with the starting material ...

I'll record my progress and the useful skills I learned when the project iscarrying on the Q&a form, which I thin K is a clean and simple to toorganize my thoughts and that, I hope, won ' t take me too much time ...

Note:what I'll cover in this post isn't the step-by-step tutorial for shellprogramming, but rather the kind of problems One would meet in a real project,and I make the assumption that the readers is already equipped with Basicknowledge about The shell script.

1Where can I find the basic Material about Linux Shell programming?

I found this tutorial useful,A beginner ' s Handbook for Linux Shell Scripting. There is many places you can find such materials, and you can take a quicklook at it whenever is in need.

2How 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 would be is the stderr_fileno,in my case, I wantto redirect the output to a file, so the fol lowing command is used:

Time./the_program_to_be_tested > Result 2>&1
3How to pass parameters to a function?

Suppose the following code section:

1:Time_xtar_load()2:{3:    Time./Xtar_load $4:}5:6: for ((I= 0;I<$num _process;I++))7: Do8:    Time_xtar_load $i&9:Done

In this example, a function named Time_xtar_load are defined, and the "for" statement would evoke the function many times EAC H with a different parameter,that is, the running value of I.

4How to calculate a float point in Shell?

The default behavior of the shell to the calculation works was limited tointegers, the tool "BC" is needed to do more advan CED calculations.forexample:

1: whileRead MINUTE SECOND2: Do3:    Total=' Echo"$MINUTE * + $SECOND" |BC '4:    Sum=' Echo"$total + $sum" |BC '5:Done<Ctar_load_sys6:7:Avg=' Echo"scale=10; $sum/$n" |BC '

In the above code segment, both self-defined variables (MINUTE, SECOND) is read from a filenamed "Ctar_load_sys", they is Used to get the total number in seconds, andafter the while terminates, the average time was calculated with the last Lineo F Code. Note that the ' scale=10 ' is necessary to do the decimal computation.

5How to output nicer to the console, like a table?

The "printf" command can do exactly and what we can do in C + +, it has richformats to choose from. For example:

Header="\ n%-10s%10s%10s\n"format="%-10s%10.3f%10.3f\n"  "$header" " " "Sum" "AVG"  "$format"real $sum $avg    sys  $sum $avg

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

Sum Avg
Real 2.36 2.35
6How to print the destined (specific) line of a file?

Use the following command:

Sed-n 1p
7How to covert column to row?

Say, I had a file (Nofs) which has the following contents:

nofs30.04957.857115.718

and I want to covert it to one row, there is many ways to do it:

1.The awk:

Cat Nofs awk ' {printf ("%s", $ A)} '

2.The TR-To:

Tr ' \ n ' < Nofs

3.The Echo:

echo $ (<NOFS)
Xargs Echo < Nofs
Cat Nofs Xargs

All of the above methods would output like:

Nofs 30.049 57.857 115.718
8How to output a certain lines of a file? (Say, all and the first line)
Tail-n +2 filename

Would output the file contents from the second line.

9How to List (copy) all the executable files in a directory?

List:

Find. -executable-type F

Copy:

Find. -executable-type F | Xargs-i CP {}/tmp
TenThe short key for adding source block in org mode:
<s TAB
OneHow to remove the duplicated newlines in a file?

I get a file with the following format:

Write rewrite read re-read Randread randwrite

32149.29 35625.84 37261.05 37575.87 713.54 1606.12

57465.48 62242.62 66024.14 66969.32 1430.75 3365.54

89339.57 96388.80 104037.65 103917.83 2714.76 6479.37

128849.01 132933.47 133952.92 134381.05 5482.03 12207.46

126489.87 128470.04 129826.87 130348.56 10446.49 24424.78

127078.48 128236.06 128642.23 129169.20 19505.88 40367.71

126942.59 125676.09 128773.13 128896.08 32061.20 63187.50

127596.32 128102.95 128516.09 129485.38 51048.20 82756.15

126424.50 127839.95 128780.78 129846.62 72924.07 97219.43

125817.65 127208.07 128134.88 129057.43 92172.60 85037.41

128768.69 129389.45 130342.52 131162.48 107949.98 100109.50

127326.84 128567.19 129666.99 129796.52 117193.84 108822.87

and I just want to remove all the empty lines of the file, the Followingcommand can be used:

Sed '/^$/d ' input_file_name > Output_file_name

Or

awk ' $ ' input_file_name > Output_file_name

The command would output the following result:

AHow to transpose a file (to convert all the colums to rows)?

Use the following script:

  Transpose_file (){lines_of_file= ' wc-l < ' $ 'echo $lines _of_file result_file _name= "Result"> $result _file_namefor ((Line_index = 1;line_index < $lines _of_file + 1 ; line_index++))dosed-n ${line_index}p "$" | Tr ' \ n ' | awk ' $ ' >/tmp/apaste $result _file_name/tmp/a >/tmp/bcp/tmp/b $result _file_name
    done}
-How do I take a floating point number up?

Using the following command:

Echo 1.0 awk ' {print int ($) ==$1?$1:int (int ($1*10/10+1))} '

Author:wujing

CREATED:2014-07-02 three 09:21

Emacs 24.3.1 (ORG mode 8.2.6)

Validate

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.