There are more or less several scripts for programmers and network administrators in Linux. Flexible variable types and powerful regular expressions in script language
Processing capabilities, coupled with the pipelines, redirection, and rich command line tools of the Linux system, allow you to program easily. C Language
However, there are various advantages, but it is undeniable that in many cases, it is more convenient to use the script language. For example, we will illustrate how to process the configuration file.
Let's take a look at the tasks of our sample program:
Suppose we have a program written in C, which has a configuration file user. conf, which stores some user information. The definition of user. conf is as follows:
1) Comments rows starting with "#" without processing.
2) Empty rows are allowed.
3) if it is not 1 or 2, it is the valid data, format
As follows:
# User. conf: configure file for user
# Username age sex country
Tom 20 male us
Chen 22 female CN
Each column is divided into four fields with one or more blank fields.
Characters (separated by spaces or tabs). The fields are names and years in sequence.
Age, Gender, and country. Our C program needs to complete
Add, delete, edit, and query.
It is not complicated to use C to process such a simple task, but it also takes some time. However, if you use the script language, it is very simple.
Single, 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. About aw
K has a lot of materials. oreilly has a special awk programming book, which can also be downloaded online. You can also directly man awk
Look.
Let's first look at how to use shell combined with awk to complete the above tasks:
1) Add a record
For example, to add a record like Jack 18 male us, you can simply use the redirection function.
Echo-E "Jack 18 male us"> User. conf
Now, this record is added to the end of user. conf.
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; MV-f tmp. conf user. conf
3) edit a record
Now, I want to change Tom's gender to female.
Cat user. conf | awk '{if ($0 ~ /^ Tom [[: blank:] +/) Print $1 $2 female $3; else print }'
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 often the source of further data processing. (In shell and Perl, the command output result can be obtained through reverse quotation marks ). I
The solution is to redirect the output result to a temporary file, read the file in C, get the data, and delete
File. However, this method is always a little uncomfortable. If we can directly import the data output in script execution to our buffer zone
That's better.
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 );
The output data is saved to the buffer to which the presult points. The buffer size is size. Up to size-1 data can be saved. Function
At the end of this article.
With this function, it is more convenient to call the script in C. We can use it to query user. conf.
4) query a record
For example, we want to obtain the Tom gender.
You can use the script to achieve this:
Cat user. conf | awk '/^ Tom [[: blank:] +/{print $3 }'
The script execution result is that Tom's Gender male is output to the screen.
In our C program, this calls my_system (),
Char Buf [101];
My_system ("cat user. conf | awk '/^ Tom [[: blank:] +/{print $3}'", Buf, 101 );
After the call, the data in the Buf is "male". How 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
With Perl, shell, and awk, you can write more powerful script files to deal with more complex problems, and then use ()
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, int size)
{
Int FD [2];
Int PID;
Int count;
Int left;
Char * p = 0;
Int maxlen = size-1;
Memset (presult, 0, size );
If (pipe (FD ))
{
Printf ("pipe errorn ");
Return-1;
}
If (pid = fork () = 0)
{// Chile Process
Int fd2 [2];
If (pipe (fd2 ))
{
Printf ("pipe2 errorn ");
Return-1;
}
Close (1 );
Dup2 (fd2 [1], 1 );
Close (FD [0]);
Close (fd2 [1]);
System (pcmd );
Read (fd2 [0], presult, maxlen );
Presult [strlen (presult)-1] = 0;
Write (FD [1], presult, strlen (presult ));
Close (fd2 [0]);
Exit (0 );
}
// Parent process
Close (FD [1]);
P = presult;
Left = maxlen;
While (COUNT = read (FD [0], p, left ))){
P + = count;
Left-= count;
If (Left = 0)
Break;
}
Close (FD [0]);
Return 0;
}
Int main (void)
{
Char result [1025];
My_system ("/sbin/ifconfig", result, 1025 );
Printf ("the result isnn % Sn", result );
Return 0;
}
From: http://www.233.com/Linux/Instructs/051207/165934648.html