exec a bash script

hello!

I'm writing a C++ program and I use fork() and execl to call a bash script.My .cpp communicates with the script with a pipe in which the script writes data and then the .cpp reads them.I don't think my script is ever executed the way I'm doing it...

A bash script should not be executed directly with execl. It is a file that is interpreted by the bash shell. So you need to execute the bash shell, with the name of a script as first argument (and subsequent arguments after that).

If the bash shell is not installed on your system, all bets are off.

Right 98% of the time, and don't care about the other 3%.

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

A bash script should not be executed directly with execl. It is a file that is interpreted by the bash shell. So you need to execute the bash shell, with the name of a script as first argument (and subsequent arguments after that).

If the bash shell is not installed on your system, all bets are off.

Could you please give me an example of what you mean exactly?
Actually now my execl works fine and the script does what it has to do (write to files)
But when I change the output the script writes to and instead of a file I choose the pipe that .cpp and script use to communicate (the name of the pipe is passed to script as an argument of execl) the script isn't working..

Also,how can a script write to a pipe that was created in the .cpp and sent to the script through exec?

Your way of creating a pipe specifies a filename (and its full path). Just use normal File IO from the script.
The 'file' is created as some sort of special object for the communication.
Read "man 3 mkfifo" if you're confused.

Your way of creating a pipe specifies a filename (and its full path). Just use normal File IO from the script.
The 'file' is created as some sort of special object for the communication.
Read "man 3 mkfifo" if you're confused.

I just can not understand why the script cannot write to the pipe since it represents a file.Why the simple echo command works for any other regular file but not for the pipe?

Shebang. Presuming the script starts with that (they usually do), you do not need to invoke "bash script.sh"; the shebang is not interpreted by the shell, it's interpreted by the "loader", which according to this is the (kernel) handler for the execve() system call:

If your script starts with a shebang, you don't have to change it; grumpy was jumping to an erroneous conclusion. Using the version explicitly invoking bash will work, but it amounts to exactly the same thing and is not at all necessary. No one writes shell scripts without a shebang at the beginning.

Also,how can a script write to a pipe that was created in the .cpp and sent to the script through exec?

Shebang. Presuming the script starts with that (they usually do), you do not need to invoke "bash script.sh"; the shebang is not interpreted by the shell, it's interpreted by the "loader", which according to this is the (kernel) handler for the execve() system call:

Y'all should really test your premises before you give advice .

Doesn't that only apply for those exec functions taking a file as the argument ? .and not a path for the binary ?

Doesn't that only apply for those exec functions taking a file as the argument ? .and not a path for the binary ?

A path to a binary is a file path; a binary is a file. These are the same thing (note, the script must have a shebang indicating the interpreter and be marked as an executable, which shell scripts usually also are). There is not one loader* for execve and then some other loader for everything else, it is just that the "e" functions include an additional parameter (environment variables, which are also not just part of the shell, altho it is common to think of them in relation to shell access via globals).

The last line blocks, however; it won't return until another process reads from the pipe. But if you don't want to wait or need to synchronize, you can put & at the end to fork it into the background.

yes it works now.I noticed that it blocks and it's a bit strange because I have a while loop in the script which does many echos in the pipe.On the other hand I have the following while loop in the .cpp
which reads the lines of the pipe

mkfifo myfifo.fifo # you don't have to do that if it exists already
echo "hello world" > myfifo.fifo

The last line blocks, however; it won't return until another process reads from the pipe. But if you don't want to wait or need to synchronize, you can put & at the end to fork it into the background.

I tried to solve the deadlock so I added a sleep(1) in the while loop of getline in my.cpp and it runs ok.But the thing is that sometimes my script has many echo commands maybe 500 or more and
if for each echo the getline sleeps for 1 then it's very slow..
Any other suggestions?