Write programs in linux, do network of people, more or less of several scripts.鑴氭湰璇█鐏垫椿鐨勫彉閲忕被鍨嬨?寮哄ぇ鐨勬鍒欒〃杈惧紡澶勭悊鑳藉姏锛屽啀鍔犱笂linux绯荤粺鏈韩鐨勭閬撱?閲嶅畾鍚戜互鍙婁赴瀵岀殑鍛戒护琛屽伐鍏凤紝璁╀綘缂栫▼璧锋潵娓稿垉鏈変綑銆?br />
The C language does have many advantages, but it is undeniable that many occasions, more convenient to use scripting language, such as we will illustrate the handling of the configuration file.
Take a look at our sample program tasks:
Suppose we have a program written with c, it has a configuration file user.conf, save some user information, user.conf defined as follows:
1) to comment line # at the beginning of the act, not treated
2) to allow blank lines
3), if not 1 and 2, it is valid data in the following form
# User.conf: configure file for user
銆??# username age backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp country
tom 20 male us
銆??chen 22 female cn
Each column is divided into four fields, between fields with one or more blank characters (spaces or tabs) separate the field followed by the name, age, gender, national
Our c procedures to be completed on user.conf the add, delete, edit, query
Such a simple task, together with c processing is not complicated, but also to spend something about, and if the scripting language used to do, very simply, can call the script in the c done it?
銆??Awk鏄痩inux涓婁竴绉嶈剼鏈瑷?紝瀹冪殑闀垮鍦ㄤ簬澶勭悊鏈変竴瀹氭牸寮忚鍒欑殑鏂囦欢锛屼緥濡傚挶浠殑user.conf銆?Information on awk, many, oreilly company out of a special awk programming books, the Internet also can be downloaded. You can also look directly man awk.
Let's see how combine with the shell awk to accomplish these tasks:
1) Add a record
銆??渚嬪锛岃娣诲姞 jack 18 male us 杩欐牱涓?潯璁板綍锛屽彲浠ョ畝鍗曠殑鐢ㄩ噸瀹氬悜鍔熻兘
Echo Ce "jack 18 male us">> user.conf
銆??鐜板湪锛岃繖鏉¤褰曡娣诲姞鍒?user.conf鏈熬浜嗐?
2) Delete a record
For example, the information you want to delete the user chen
cat user.conf awk '! / ^ chen: blank: + / print'> tmp.conf; mvCftmp.conf user.conf
3), editing a record
銆??鐜板湪锛屾兂鎶?tom鐨勬?鍒敼涓?female
Through the system () this function, we can call the above script in c to complete the task.
However, system () still feel uncomfortable with them, the lack of it is only for scripting, but can not get the script output data, which is usually our source of data for further processing. (In the shell and perl, you can by anti-quotation marks (``) to get the command output). One solution is to redirect output to a temporary file, then read the file in the c, to obtain data, and finally of course, delete this file. However, this method always make people feel a little uncomfortable, if the output directly to the script execution in the data input to our buffer to the better.
I wrote a small function, called my_system (), through pipes and redirection, to achieve the above ideas.鍑芥暟鍘熷瀷濡備笅锛?br />
銆??int my_system(const char* pCmd, char* pResult, int size)锛?br />Output data is saved to the buffer pointed to by pResult, the buffer size for the size, can save up to size-1 data.
Functions to achieve the final article on
With this function after calling the script in the c more convenient, we can use it to achieve user.conf inquiries.
4), query a record
銆??渚嬪锛屾垜浠鑾峰彇 tom 鐨勬?鍒?br />
銆??鍙互鐢ㄨ剼鏈繖鏍锋潵瀹炵幇锛?br />
銆??cat user.conf awk 鈥?^tom:blank:+/ print $3鈥?br />銆??鑴氭湰鐨勬墽琛岀粨鏋滄槸 tom鐨勬?鍒?male琚緭鍑哄埌灞忓箷涓?br />
銆??鍦ㄦ垜浠殑 c绋嬪簭涓紝濡傛璋冪敤 my_system()锛?br />
char buf101;
銆??my_system(鈥渃at user.conf awk 鈥?^tom:blank:+/ print $3鈥欌?,buf,101);
After the call finished, buf the data is the "male" a, how, pretty easy right?
銆??浠ヤ笂鍙槸鐢ㄧ粨鍚堣剼鏈畬鎴愪簡涓?釜姣旇緝绠?崟鐨勪换鍔★紝鎵?互鎴戞病鏈夋妸杩欎簺鑴氭湰鍗曠嫭褰㈡垚鑴氭湰鏂囦欢銆?If you are good at using perl, shell, awk, you can write a more powerful script file to handle more complex problems, then a similar my_system () method, in c / c + + and other languages to obtain the script output to achieve interesting "hybrid programming."
I hope you can 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;
相关链接:
DVR-MS to MPEG
MKV to Xbox 360
Review 0
AMD Intel on EU antitrust penalties comment
The secret of growth
Kodak V705 Dual Lens Camera Stabilization Cheap Listing Weekend
Family can offer car parking bus transfer brush 2 yuan per CARD
Internal control: harm than good, or at every step
WiMAX and TD-band battle began deemed to be an advantageous position in China
QuickTime To MPEG
Training Of New Employees Approach HR
Master Is Not Highly Educated Does Not Mean That High-income Elite
Servlet's Context and Config
Terminal And Telnet Clients evaluation
Convert for