Supposing that an absolute pathname for the script you want to execute is /path/to/matrix.sh
, and that it is executable by name from the command line, a variation that will work is
system("/path/to/matrix.sh");
To avoid the second warning you should capture the return value as well, and verify that it does not indicate an error (by being -1).
As for your attempts:
1) system("./matrix.sh")
This depends on matrix.sh
being present in the current working directory (which may or may not be the one containing the C program). That's what the "./" part means.
2) int ret=system("./matrix.sh")
Same as the previous, only here you catch the error code. The shell will likely have emitted an error message, but that's not observable by the program. The program can use the value of ret
to determine whether the script was successfully launched and if so, what its exit code was.
3) system("cd /path/to/matrix.sh; ./matrix.sh &")
The "cd /path/to/matrix.sh" part is wrong because matrix.sh
is not a directory. Additionally, it's unclear why you are using the &
operator to execute the script in the background. This variation ought to work, however:
system("cd /path/to && ./matrix.sh");
That might make sense to do if the script (unwisely) depends on using relative paths to locate other files installed near it, and it will fail without trying to execute the (possibly wrong) script if it does not succeed in changing the working directory.
4) system("./matrix.sh>> outputfilename")
This has the same issue as (1) and (2). Redirecting the standard output does not resolve it. You could probably suppress the error message by redirecting standard error instead of standard output ("./matrix.sh 2> /dev/null"), but that by itself would not solve the main problem.
5) system("Path/to/matrisx.sh")
That's almost it, but (1) file and directory names are case-sensitive to bash
, and Path
is not the same as path
, but more importantly, (2) without a leading /
character, "Path/to/matrisx.sh" is still interpreted relative to the current working directory.
6) system("bash/Path/to/matrix.sh")
That could almost work, too, if the path were correct, but you need at least one space character between "bash" and the path to the script.
system("bash /path/to/matrix.sh");
That variation does introduce an extra level of shell, however, which is a needless inefficiency unless the script you're naming is not directly executable.