C and mixed programming of scripts

zhaozj2021-02-17  68

C and mixed programming of scripts

Author: Chen Yifei

Last updated: 2003-09-09

Keywords: C, script, awk, shell, perl

Write programs on Linux, people who are a network management, more or less will have several scripts. Script language flexible variable type, powerful regular expression processing capability, plus Linux system itself pipes, redirects, and rich command line tools, allowing you to play out.

The C language has a variety of advantages, but it is undeniable that many occasions are more convenient to use scripting languages, such as the processing of the configuration file.

Let's take a look at our sample programs:

Suppose we have a program written with C, which has a profile user.conf, saves some user information, user.conf is as follows:

1)

2) Allow blank lines

3) If it is not 1 and 2, then it is effective data, the format is as follows

# User.conf: configure file for user # username agen sex countrytom 20 Male USChen 22 Female CN Each column is divided into 4 fields, with one or more blank characters (spaces or tabs) between fields, the field sequentially Yes, age, gender, country our C procedure to complete the addition, deletion, editing, querying such a simple task, using C, but also spend some effort, and if Use scripting language, it is very simple, can you adjust the script in C to complete the task? AWK is a scripting language on Linux, where it has a file with a certain format rule, such as our user.conf. About AWK's information has a lot, Oreilly has a special AWK programming book, and online can also be downloaded. You can also take a look directly. Let's take a look at how to use Shell to complete the above tasks: 1) Add a record, for example, to add a record of 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, now delete user chen's information CAT user.conf | awk '! / ^ Chen [[: blank:] / {print}'> tmp.conf; mv -f tmp.conf user .conf 3), edit a record now, want to change TOM's gender to female cat user.conf | awk '{ix ($ 0 ~ / ^ Tom [[: blank:]] /) Print $ 1 $ 2 FEMALE $ 3; Else Print} 'Through System () This function, we can call the above scripts in C to complete the task. However, SYSTEM () is still uncomfortable. It is not enough to perform scripts, but it is not possible to obtain the output data of the script, which is usually the source of data we have further processed. (In Shell and Perl, the output result of the command can be obtained by reverse quotation marks (``)). A solution is to redirect the output result to a temporary file, then read the file in C, get the data, and finally delete this file. However, this method always feels a little uncomfortable. If you can directly output the data output from the script to our buffer, it is better. I wrote a small function called my_system (), through the pipeline and redirect, realized above. The function prototype is as follows: int my_system (const char * pcmd, char * pRESULT, INT size); Output Data is saved to the buffer pointed to by PRESULT, the buffer size is Size, up to saving the SIZE-1 data. The implementation of the function is placed in this function. After the function is called, the script is called more convenient in C, we can use it to realize the query for user.conf.

4), query a record, for example, we have to get TOM's gender to implement: Cat User.conf | awk '/ ^ Tom [[: blank:]] / {Print $ 3}' script execution result is TOM's gender Male is output to the screen in our C program, call my_system (), char buf [101]; my_system ("Cat User.conf | awk '/ ^ Tom [[: blank:]] / {Print $ 3} '", BUF, 101); After the call is completed, the data in the buf is" Male ", how is it, is it convenient? The above is just a relatively simple task with a combined script, so I didn't form the script file separately. If you are good at using Perl, Shell, awk, then you can write more stronger script files to handle more complex issues, and then achieve the output of the script in other languages ​​such as MY_SYSTEM (), implementation Interesting "Mixed Programming".

转载请注明原文地址:https://www.9cbs.com/read-28810.html

New Post(0)