Reputation: 13
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.
Upvotes: 1
Views: 127
Reputation: 70911
./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.
Upvotes: 0
Reputation: 399793
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.
Upvotes: 1