Linux shell commands and regular expressions

Source: Internet
Author: User
Tags linux shell commands

There was a problem compiling the VPP project on the FEDORA20 system today, and in the final compilation and the RPM installation package generated using Rpmbuild is incorrect, we need to make some minor changes to the spec file that generated the RPM package.

First of all, let's take a look at the spec profile of the generated RPM package from VPP, which is ${vpp_topdir}/build-root/rpm/vpp.spec, and we can open the contents of the file:

which has the following command in the%install section

1 #2# libraries
3#
4mkdir-p-m755%{buildroot}%{_libdir}
5 forfile inch$(Find%{_vpp_install_dir}/*/lib*-type f-name ' *.so.*.*.* '-print)
6Do
7install-p-M 755 $file%{buildroot}%{_libdir}
8Done
9 For file in $ (CD%{buildroot}%{_libdir} && find-type f-print | sed-e ' s/^\.\///')
TenDo
One# make Lib Symlinks
A(CD%{buildroot}%{_libdir} &&
- ln-fs $file $ (echo $file | sed-e ' S/\ (\.so\.[ 0-9]\+\).*/\1/') )
-Done

This first creates the Lib directory inside the%{buildroot} directory, and then calls the install command to copy the Lib files from all the VPP projects compiled into the Lib directory in the%{buildroot directory.

Now let's look at a For loop command for the 9~14 line, which is basically a function of creating a soft link file for all the library files inside the Lib directory inside the%{buildroot directory.

For example: If you now have a library file libvnet.so.18.0.0 in the Lib directory in the%{buildroot directory, Then the above for loop is completed and a soft link file libvnet.so.18 is created in the same directory and points to the libvnet.so.18.0.0 library file.

Now let's take a step-by-step analysis of the work of this for loop statement above the 9~14 line, and we assume that there are 2 library files in the/usr/lib64/directory, one is libvpp.so.0.0.0, and the other is libvnet.so.18.0.0.

First, let's look at a shell command following the FOR statement in line 9th:

$ (CD%{buildroot}%{_libdir} && find-type f-print | sed-e ' s/^\.\///')

Then it is equivalent to executing one of the following commands:

$ (cd/usr/lib64/&& find-type f-print | sed-e ' s/^\.\///')

This command is a combination of two commands, first performing cd/usr/lib64/switch the working directory to the/usr/lib64/directory, and then follow the Find command.

The find is then executed. The results of the-type f-print command are as follows:

./libvpp.so.0.0.0

./libvnet.so.18.0.0

The Find command here has a path in front of the lookup output result. The output from the Find command is then redirected through the pipeline to the SED command, and then the following SED command is parsed.

Sed-e ' s/^\.\///': Finds lines beginning with./In line with./Replace with empty, where ^ represents the beginning of a match, followed by two escape characters escaping "dot (.)" and "slash (/)", s action means find replace

The result of the find lookup after the SED command has been processed becomes the following:

Find. -type F-print | Sed-e ' s/^\.\///' after sed processing the Find results all removed the previous path, the final result of the command is as follows:

libvpp.so.0.0.0

libvnet.so.18.0.0

At this point, the value of the file variable in the For loop is there, and the value of the file variable is set {libvpp.so.0.0.0, libvnet.so.18.0.0}.

Let's then analyze the loop body statement in the for loop.

(CD%{buildroot}%{_libdir} && ln-fs $file $ (echo $file | sed-e ' S/\ (\.so\.[ 0-9]\+\). */\1/'))

We can replace the above command in the example we are now lifting:

(cd/usr/lib64/&& ln-fs libvpp.so.0.0.0 $ (echo libvpp.so.0.0.0 | sed-e ' S/\ (\.so\.[ 0-9]\+\). */\1/'))

(cd/usr/lib64/&& ln-fs libvnet.so.18.0.0 $ (echo libvnet.so.18.0.0 | sed-e ' S/\ (\.so\.[ 0-9]\+\). */\1/'))

We specifically analyze the SED command, where there is a regular expression we assume that the regular expression is expanded, then we get an sed command like this:

$ (echo libvpp.so.0.0.0 | sed-e ' s/(. so.[ 0-9]+). */\1/')

Note: The regular expression of the SED command above does not have an escape character.

(. So. [0-9]+). *//(...) Represents a matching substring and saves matching characters, [0-9] means matching 1 numeric characters, [0-9]+ means matching 1 or more numeric characters,. Represents any character that matches a non-line break, * indicates a match of 0 or more characters

\1//means that the matching characters that were saved when the matching substring was previously found are directly referenced here

Through the above analysis, we can draw the following shell command to run the results:

$ (echo libvpp.so.0.0.0 | sed-e ' s/(. so.[ 0-9]+). */\1/')//The resulting string is: libvpp.so.0

$ (echo libvnet.so.18.0.0 | sed-e ' S/\ (\.so\.[ 0-9]\+\). */\1/')//The resulting string is: libvnet.so.18

The LN-SF command is then invoked to create 2 soft-link files directly:

libvpp.so.0-------->libvpp.so.0.0.0

libvnet.so.18--------->libvnet.so.18.0.0

Although the above shell command created 2 soft link files, but we know that under the Linux program refers to the dynamic link library file when the general habitual use-LVPP,-lvnet, etc. to specify, The library file form that relies on this is libvpp.so and libvnet.so, not libvpp.so.0 and libvnet.so.18.

So we also need to create 2 soft link files libvpp.so and libvnet.so, also soft links to libvpp.so.0.0.0 and libvnet.so.18.0.0 respectively.

We just need to modify the for loop above and we'll modify it as follows:

For file in $ (CD%{buildroot}%{_libdir} && find-type f-print | sed-e ' s/^\.\///') do# make Lib Symlinks (CD%{ Buildroot}%{_libdir} && ln-fs $file $ (echo $file | sed-e ' S/\ (\.so\.[ 0-9]\+\). */\1/') &&
LN-SF $file $ (echo $file | sed-e ' s/\ (\.so\). */\1/')) Done

This way, after the For loop executes, 4 soft-link files are created in the/usr/lib64 directory, with the following relationships:

libvpp.so-------->libvpp.so.0.0.0

libvpp.so.0-------->libvpp.so.0.0.0

libvnet.so--------->libvnet.so.18.0.0

libvnet.so.18-------->libvnet.so.18.0.0

For a detailed description of the use of SED commands and regular expressions, please refer to links below:

Http://man.linuxde.net/sed

Http://www.infoq.com/cn/news/2011/07/regular-expressions-6-POSIX

Linux shell commands and regular expressions

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.