1

I tried to execute script within C program. I tried:

system("/home/olaudix/weather.sh") 

system("sh /home/olaudix/weather.sh")

execvp("/home/olaudix/weather.sh")

but all of them throw

syntax error: "(" unexpected at line 1.

Scripts starts with function getData() { but it runs fine when executed in terminal.

3
  • you could try: sh -c Commented Jan 26, 2018 at 16:36
  • 1
    Line 1? You're missing a #! line. Also, if you want to be compatible with sh, it should be getData() { (without function). Commented Jan 26, 2018 at 16:36
  • What I need to make in C is "shell" program that can execute scripts and programs after user gives relative or absolute path to said scripts. I can't edit the scripts themselves and I think that if it works in terminal i have to make it work in my program. Commented Jan 26, 2018 at 16:56

2 Answers 2

4

The shebang (i.e.: #!) at the very beginning of the scripts is missing. You need it in order to specify the corresponding interpreter (bash in this case), i.e.:

#!/bin/bash
Sign up to request clarification or add additional context in comments.

2 Comments

What I need to make in C is "shell" program that can execute scripts and programs after user gives relative or absolute path to said scripts. I can't edit the scripts themselves and I think that if it works in terminal i have to make it work in my program.
Then, you can call bash with the script to run as an argument as part of system() (i.e.: bash -c "command-to-run").
0

It is also possible to source the shell script in the system command. Below is an example

$ cat 48465591.c
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char* argv[])
{
system(". ./myshellscript.sh"); // Note the '.' which stands for sourcing
return 0;
}
$ cat myshellscript.sh
printhello()
{
echo "Hello";
}

Comments

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.