1

I've a problem with the system() function.

I want to create a little program which run a command x times.

The following code is to launch a command :

#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;
}

My command is working when I write it directly in my shell, but when I use this program I get this error:

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

This error means:

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

I guess that system try to execute my command like time ./lance ..., but I want to run this like a command not a program so time lance ...

I've already tried to run this program without the loop, still the same.

I've also tried a simpler command like ls -l; that's working.

If anyone can help me, I'll be very thankful!

Edit : My purpose is to run a script many time to have data about execution's time, that's why I wrote this program.

Edit2: This is a part of my script "lance" :

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

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

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

if [[ "${3}" != "F" &&  "${3}" != "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 : Thanks for alk who help me to find what was wrong is my code, I've added #!/bin/bash in the 1st line of my script and now it's working.

8
  • 1
    What happens if you type (time lance REF_CLIENT_FOUR002542_C FE-ERPCP-REF_CLIENT_FOUR.zip F > result.txt) 2> time.txt from the command line ? Commented May 4, 2016 at 9:11
  • Implementing your use-case in C seems a little bit like taking a sledgehammer to crack a nut. You want to have a look at some basic shell scripting. Commented May 4, 2016 at 9:14
  • Have you tried running your command the way that system() does? I.e. /bin/sh -c "(time lance ...)" - that may expose some assumptions you've made about $SHELL. Commented May 4, 2016 at 9:16
  • If you're using system(), you don't need fork(). The system() function essentially does a fork for you. Commented May 4, 2016 at 9:17
  • "but I want to run this like a command not a program so time lance ..." please excuse but I somewhat do under understand what you want to express. Commented May 4, 2016 at 9:17

2 Answers 2

1

You don't need to fork() when using system()! The latter is a much more high-level call, it will fork a shell and ask that to run the command.

Sign up to request clarification or add additional context in comments.

2 Comments

Without forking the program behaves differently, as the calls to system() would run in sequence, not in parallel.
@ArthurDessez: "I removed fork()" So you also want to remove the wait()` as it most likely does not do what you expect. Please RTFM here: man7.org/linux/man-pages/man2/wait.2.html
0

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

In script lance on line 34 there is an unexpected ( character.

To debug the script put this

set -x

as first statement into the script.

3 Comments

I've updated my code. The thing that I don't understand is when I run the script directly on the line command it's working but not with my program. I've use your tips to debug and every ./lance not found is during if at the beginning (I add this part of code in the first post)
@ArthurDessez: The error ./lance: [[: introuvable does not mean "lance not found", but in file ./lance some [[ was "not found".
@ArthurDessez: Make sure your script defines which shell it needs to run in. If you want the script to run using bash, put this #!/bin/bash as 1st line in the script. system() uses /bin/sh, the login you might run your test from perhaps uses a different one. Also you want to check if /bin/sh is a link, and to what.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.