C and Shell Mixed Programming

Source: Internet
Author: User
There are more or less several scripts for programmers and network administrators in Linux. The flexible variable types and powerful regular expression processing capabilities of the script language, coupled with the pipelines, redirection, and rich command line tools of the Linux system, allow you to program easily. C language has various advantages, but it is undeniable that it is more convenient to use the script language in many cases. For example, we will illustrate how to process the configuration file. Let's take a look at the task of the example program: Suppose we have a program written in C, which has a configuration file user. conf, saves some user information, user. conf is defined as follows: 1). Comments rows starting with # are not processed. 2) Empty rows are allowed. 3) if not 1 or 2, valid data is used, the format is as follows # user. conf: configure file for user
# Username age *** country
Tom 20 male us
Chen 22 female CN each column is divided into four fields, which are separated by one or more blank characters (spaces or tabs, the fields are name, age, gender, and country. adding, deleting, editing, and querying conf is not complicated to use C, but it also takes some time. If you use the script language, it is very simple, can I call a script in C to complete the task? Awk is a scripting language in Linux. Its strength lies in processing files with certain format rules, such as our user. conf. There are a lot of materials about awk. oreilly has a special awk programming book, which can be downloaded from the Internet. You can also look at it directly with man awk. Let's first look at how to use shell combined with awk to complete the above task: 1) Add a record, for example, add a record like Jack 18 male us, you can simply use the redirection function echo ce "Jack 18 male us"> User. conf now, this record is added to user. conf ends. 2) delete a record. For example, if you want to delete the information of user Chen, cat user. conf awk '! /^ Chen: blank: +/print '> TMP. conf; mvcftmp. conf user. conf 3) edit a record. Now, if you want to change the Tom gender to female through the system () function, we can call the above script in C to complete the task. However, system () is still uncomfortable to use. Its disadvantage is that it can only execute scripts, but cannot obtain the output data of scripts. This is usually the data we process further.
Source. (In shell and Perl, the command output result can be obtained through reverse quotation marks ). One solution is to redirect the output result to a temporary file and then read the text in C.
To obtain data, and delete the file. However, this method is always a little uncomfortable. It would be better to directly import the data output in script execution to our buffer zone. I wrote a small function called my_system (), which realized the above ideas through pipelines and redirection. The function prototype is as follows: int my_system (const char * pcmd, char * presult, int size); output data is saved to the buffer zone pointed to by presult, And the buffer size is size, up to size-1 data can be saved. After the function is implemented at the end of this article, it is more convenient to call the script in C. We can use this function to query user. conf. 4) query a record. For example, we can use a script to obtain the Tom Gender: CAT user. conf awk '/^ Tom: blank: +/print $3' the execution result of the script is Tom's Gender male is output to the screen in our C program, call my_system (), char buf101;
My_system ("cat user. conf awk '/^ Tom: blank: +/print $3' ", Buf, 101); after the call, the data in the Buf is" male ". How can this problem be solved, is it convenient? The above is just a simple task completed with the script, so I did not separate these scripts into a script file. If you are good at using Perl, shell, and awk
You can write more powerful script files to solve more complex problems, and then (
In other languages, such as C/C ++, to achieve interesting "mixed programming ". Hope you have fun! # Include
# Include
# Include
# Include
# Include
Static int my_system (const char * pcmd, char * presult, intsize)
Int fd2;
Int PID;
Int count;
Int center;
Char * p = 0;
Int maxlen = size C 1;
Memset (presult, 0, size );
Printf (pipe error // n );
Return C1;
// Chile Process
Int fd22;
Printf (pipe2 error // n );
Return C1;
Close (1 );
Dup2 (fd21, 1 );
Close (fd0 );
Close (fd21 );
System (pcmd );
Read (FD20, presult, maxlen );
Presultstrlen (presult)-1 = 0;
Write (fd1, presult, strlen (presult ));
Close (FD20 );
Exit (0 );
// Parent process
Close (fd1 );
P = presult;
Center = maxlen;
While (COUNT = read (fd0, P, center )))
P + = count;
Center-= count;
Break;
Close (fd0 );
Return 0;
Int main (void)
Char result1025;
My_system (/sbin/ifconfig, result, 1025 );
Printf (the result is // N // n % S // n, result );
Return 0 ;}
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.