【问题标题】:Passing C variable in Shell command在 Shell 命令中传递 C 变量
【发布时间】:2013-06-26 02:23:53
【问题描述】:

我遇到了一个问题。我想在 shell 命令中使用 C 变量 (dd)。

假设 abc.c 是我的 C 程序。

int main()
 {
    int block = 1313; /*any integer */
    system("dd if=device of=output-file bs=4096 count=1 skip=$((block))");
    return 0;
 }

现在,如果我在 dd 命令中使用 1313 代替 block,那么它可以正常工作。但是当我写block 时,它会在输出文件中写入零,因为block 是一个C 程序变量并在shell 命令中使用。

【问题讨论】:

  • 你没有用谷歌搜索过这个,对吧。而且你不知道 PHP 和 C 之间的区别,是吗?

标签: c shell system


【解决方案1】:

使用snprintf()

char buf[256];
const int block = 1313;
snprintf(buf, sizeof buf,
         "dd if=device of=output-file bs=4096 count=1 skip=%d", block);
system(buf);

【讨论】:

    猜你喜欢
    • 2015-08-05
    • 1970-01-01
    • 2015-01-08
    • 2022-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 2020-03-28
    • 1970-01-01
    相关资源
    最近更新 更多