gpt4 book ai didi

c - 来自linux shell脚本的函数不在c程序中执行

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:19:28 25 4
gpt4 key购买 nike

我在 stackoverflow 和其他网站上看到过类似的帖子,但我找不到解决问题的方法。

我有以下 consoleout.sh 文件:

#!/bin/sh

#this way works in c:
#echo "Hello World!"

#but in function does not work:
a(){
echo "Hello World!"
}

以下 C 代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
system(". consoleout.sh");

system("a");
return 0;
}

没有 system("./consoleout.sh"),它工作正常。

最佳答案

system() 调用一个 shell 并等待它终止。对 system() 的另一个调用将创建一个从头开始的不同 shell。

要运行您的 shell 函数,您需要从定义它的 shell 中执行:

#include <stdio.h>
#include <stdlib.h>

int main()
{
return system( ". consoleout.sh; a" );
}

关于c - 来自linux shell脚本的函数不在c程序中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46645013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com