Sun Qiqung accompany you to learn--how to assign the return value of a shell command to the application's variables

Source: Internet
Author: User

How to assign the return value of a shell command to an application's variable.

Bo Master recently made a router project, need to get the router now network status and parameters, using the UCI get GET,

However, with system ("UCI get"), the obtained parameters are printed out directly in the terminal, which is not available in the application.

The returned parameter. Here is my solution.

#include <stdlib.h>

#include <stdio.h>
#include <string.h>
Char buf[512];
char* Uciget (char option[])
{
FILE *stream;
memset (buf, ' n ', sizeof (BUF));
stream = popen (option, "R");
Fread (buf, sizeof (char), sizeof (BUF), stream);
Buf[strlen (BUF) -1]= ' + ';
Pclose (stream);
return buf;
}
int main (void)
{

printf ("%s\n", Uciget ("UCI get Network.wan.proto"));
printf ("%s\n", Uciget ("UCI get [email Protected][0].ssid"));
return 1;
}

Sun Qiqung accompany you to learn--how to assign the return value of a shell command to the application's variables

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.