System() 参数 C

System() parameters C

我的 system() 函数有问题。

我想创建一个 运行 命令 x 次的小程序。

下面的代码是启动一个命令:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    char command[100];
    strcpy(command, "(time lance REF_CLIENT_FOUR002542_C FE-ERPCP-REF_CLIENT_FOUR.zip F > result.txt) 2> time.txt");
    system(command);
    return 0;
}

当我直接在我的 shell 中写入命令时,我的命令正在运行,但是当我使用这个程序时,我得到这个错误:

./lance: [[: introuvable
./lance: [[: introuvable
./lance: [[: introuvable
./lance: [[: introuvable
./lance: erreur de syntaxe ligne 34: `(' inattendue

这个错误意味着:

./lance: [[: not found
./lance: syntax error at line 34 : '(' unexpected

我想系统会尝试像 time ./lance ... 那样执行我的命令,但我想 运行 这就像命令而不是程序,所以时间长矛 ...

我已经试过运行这个没有循环的程序,还是一样。

我也试过一个更简单的命令,比如ls -l;行得通。

如果有人能帮助我,我将不胜感激!

编辑:我的目的是 运行 脚本多次获得有关执行时间的数据,这就是我编写此程序的原因。

Edit2:这是我脚本的一部分 "lance" :

if [[ $# -ne 3 ]] # 1st not found
 then
   echo "bla"
   exit 1
fi

if [[ ! -f  ]] # 2nd not found
then
   echo "bla"
   exit 1
fi

if [[ ! -f  ]] # 3rd not found
then
  echo "bla"
  exit 1
fi

if [[ "" != "F" &&  "" != "Z" ]] #4th not found
then
   echo "bla"
   exit 1
fi

if [[ -d TRAVAIL ]] # This is the line 34
then
  mv TRAVAIL TRAVAIL_$(date +%d%m%y%H%M%S)
fi

Edit3:感谢 alk 帮助我找出我的代码有什么问题,我在脚本的第一行添加了#!/bin/bash,现在可以正常工作了。

使用 system() 时不需要 fork()!后者是一个更高级的调用,它会派生一个 shell 并向 运行 命令询问。

./lance: syntax error at line 34 : '(' unexpected

在脚本 lance 的第 34 行有一个意外的 ( 字符。

调试脚本放这个

set -x

作为脚本中的第一条语句。