0

Based on the answers in StackOverflow, I used system command in a C program to invoke matrix.sh in the following possible ways:

1) system("./matrix.sh")

2) int ret=system("./matrix.sh")

3) system("cd /path/to/matrix.sh; ./matrix.sh &")

4) system("./matrix.sh>> outputfilename")

5) system("Path/to/matrisx.sh")

6) system("bash/Path/to/matrix.sh")

and a few others. But all of these give the SAME error. sh:1: Matrix.sh not found and another error (except in 2nd case) as warning: ignoring return value of 'system', declared with attribute warn_unused_result [-Wunused-result]

The paths of the invoking C program and the [input .sh script, expected output file] are different. How to write the system() command in this situation? What is the cause of this error?

Jaya Lakshmi
  • 11
  • 1
  • 2
  • 1
    Try int rc = system ("/home/username/bin/somescript.sh"); where you use an absolute path to your script. Also, make sure the shebang on the shell script is correct. – Ulises André Fierro Mar 13 '19 at 19:31
  • A [mcve], giving us enough detail to produce the problem ourselves, might include details such as what directory your C program is in, what directory you invoke the C program *from*, what command you use for that invocation, whether your `matrix.sh` is flagged executable and whether it has a valid shebang, etc. – Charles Duffy Mar 13 '19 at 20:02

1 Answers1

0

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Better to suggest `&&` rather than `;` to separate a `cd` and the command anticipated to be run in that directory, so your failure mode doesn't involve running the command in the *wrong* directory. – Charles Duffy Mar 13 '19 at 19:54
  • Good advice, @CharlesDuffy. Updated. – John Bollinger Mar 13 '19 at 19:55
  • Thank you very much for the detailed answer. I again tried the possible methods suggested by you. Unfortunately, the same error was being shown. I think the problem lies somewhere else. Thanks – Jaya Lakshmi Mar 15 '19 at 08:17