繁体   English   中英

调用shell脚本后返回主C程序

[英]Return to main C program after calling shell script

我在Raspberry-Pi上运行了一个C程序,该程序不断从AVR读取串行信息。 我想写一个使用简单的shell脚本接收到的命令的历史记录:

#!/bin/bash

echo "Command $2 received from address $1" 

date_time=$(date)                       #read current date from terminal
echo " $date_time Address: $1 Cmd: $2" >> file.txt      #echo address & command to file, redirect stdio

exit 0

当在终端中使用适当的参数调用该脚本时,该脚本可以完美运行,但是,我希望每次从串行线路读取命令时都执行该脚本。 C程序当前读取命令和地址并将其打印到标准io,因此我想做的是在此之后调用脚本,以将数据写入文件。

当像这样在我的代码中调用脚本时:

    char* args[3];
char* program="ioExport.sh";
    args[1]=addr;
    args[2]=cmd;
    args[3]=NULL;
    execv(program,args);

它退出主程序。 调用脚本后如何返回主程序? 还是在将脚本作为后台进程运行时有办法做到这一点? 我知道使用标准C文件I / O进行操作要简单得多,但是如果可以使用Shell,我想这样做:)

问候,哈科

exec函数家族用新进程替换了该进程。 它们仅在出现错误的情况下返回。

您似乎想要的是system函数,该函数为您创建一个shell进程,然后在自变量中执行命令。

顺便说一句,您反正将argument数组设置为错误,请记住数组索引从零开始 另外, argument数组中的第一个条目应为程序名称,这意味着您需要将argument[0]设置为program ,并且当然要将数组大小更改为4。


但是,正如trojanfoe所指出的那样,除非收到消息时还需要运行一些其他特殊命令,否则根本不需要调用Shell脚本。如果您只想将一些日志记录信息打印到文件中,则可以可以很容易地从程序内部完成:

/* First get the date and time string */
time_t now = time(NULL);
char *timestring = ctime(&now);

/* It contains a trailing newline, remove that by */
/* overwriting the newline with the string terminator */
timestring[strlen(timestring) - 1] = '\0';

/* Open the file for appending (i.e. writing at the end) */
FILE *file = fopen("file.txt", "a");

/* And write your message to your file */
fprintf(file, "%s Address: %s Cmd: %s\n", timestring, addr, cmd);

/* Close the file */
fclose(file);

将其放在单独的函数中,可在需要时调用。 并请注意,如果您想向文件中写入更多内容,则可以在程序开始时将其打开,直到退出该程序才将其关闭。

尤其如此频繁地调用shell脚本会削弱程序的性能,因此请使用内置的日志记录功能。 您可能需要更改logData()的语义,因为我不知道您的“命令”和“地址”采用什么形式。

logger.h:

#pragma once

#include <stdbool.h>

extern bool logOpen(const char *filename);
extern void logData(const char *command, const char *address);
extern void logClose();

logger.c:

#include "logger.h"
#include <stdio.h>
#include <sys/time.h>

static FILE *logfp = NULL;

bool logOpen(const char *filename) {
    logfp = fopen(filename, "w+");
    return logfp != NULL;
}

void logData(const char *command, const char *address) {
    struct timeval tv;
    struct tm tm;

    if (!logfp)
        return;

    gettimeofday(&tv, 0);
    localtime_r(&tv.tv_sec, &tm);

    fprintf(logfp, "%02d:%02d:%02d.%03u: command='%s' address='%s'\n",
        tm.tm_hour, tm.tm_min, tm.tm_sec, (unsigned)(tv.tv_usec / 1000),
        command, address);

}

void logClose() {
    if (logfp) {
        fclose(logfp);
        logfp = NULL;
    }
}

main.c中:

#include "logger.h"

int main(int argc, const char **argv) {

    logOpen("file.log");

    logData("Command 1", "Address 1");
    logData("Command 2", "Address 2");
    logData("Command 3", "Address 3");
    logData("Command 4", "Address 4");

    logClose();
    return 0;
}

编译和测试:

$ clang -o test main.c logger.c
$ ./test
$ more file.log
13:40:08.399: command='Command 1' address='Address 1'
13:40:08.399: command='Command 2' address='Address 2'
13:40:08.399: command='Command 3' address='Address 3'
13:40:08.399: command='Command 4' address='Address 4'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM