# Execute bash script within OCaml \[SOLVED\]

**URL:** <https://discuss.ocaml.org/t/execute-bash-script-within-ocaml-solved/1081>\
**Category:** Learning\
**Tags:** script, bash\
**Created:** [November 7, 2017, 2:33pm UTC](https://discuss.ocaml.org/t/execute-bash-script-within-ocaml-solved/1081 "2017-11-07T14:33:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathandoyle](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jonathandoyle](https://discuss.ocaml.org/u/jonathandoyle)\
**Post date:** [November 7, 2017, 2:33pm UTC](https://discuss.ocaml.org/t/execute-bash-script-within-ocaml-solved/1081/1 "2017-11-07T14:33:24Z")

</div>

Is it possible to execute bash scripts within Ocaml code ?

I thought of using Sys.command “bash my\_script” but on my example this returns a 255 error (means an “out-of-range” error, not sure how to interpret it) and does not work.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [November 7, 2017, 2:55pm UTC](https://discuss.ocaml.org/t/execute-bash-script-within-ocaml-solved/1081/2 "2017-11-07T14:55:59Z")

</div>

> [@jonathandoyle](#):
>
> I thought of using Sys.command “bash my\_script” but on my example this returns a 255 error (means an “out-of-range” error, not sure how to interpret it) and does not work.

This should be the way to do it.

E.g. here `Sys.command "bash"` in `ocaml` will take me to a shell prompt and back when I’m finished.

Does `my_script` maybe call `exit` with a value out of 0-255 ?

---

<div class="post-metadata">

**Author:** ![jonathandoyle](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jonathandoyle](https://discuss.ocaml.org/u/jonathandoyle)\
**Post date:** [November 7, 2017, 3:03pm UTC](https://discuss.ocaml.org/t/execute-bash-script-within-ocaml-solved/1081/3 "2017-11-07T15:03:07Z")

</div>

Note that the script seemingly executes without any problems if I launch it outside OCaml. It is rather short, and simply reads a PHP¨ file and writes the output on a file.

Here goes the script’s content :

#! /bin/bash

/Applications/MAMP/bin/php/php5.6.7/bin/php-cgi -f   
/Users/jonathandoyle/Documents/Sites/Rachel/public\_html/iewtopic.php   
f=42 p=5554 \> /Users/jonathandoyle/Documents/Sites/Rachel/public\_html/temp.txt;

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [November 7, 2017, 3:06pm UTC](https://discuss.ocaml.org/t/execute-bash-script-within-ocaml-solved/1081/4 "2017-11-07T15:06:08Z")

</div>

Just to make sure what is the result of:

```auto
bash my_script
echo $?

```

---

<div class="post-metadata">

**Author:** ![jonathandoyle](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jonathandoyle](https://discuss.ocaml.org/u/jonathandoyle)\
**Post date:** [November 7, 2017, 3:16pm UTC](https://discuss.ocaml.org/t/execute-bash-script-within-ocaml-solved/1081/5 "2017-11-07T15:16:31Z")

</div>

> [@dbuenzli](#):
>
> Just to make sure what is the result of:
> 
> ```auto
> bash my_script
> echo $?
> 
> ```

The result is 0 when the PHP executes without errors and 255  
when it contains errors.

I’ve just checked that OCaml does the correct thing when there is no error in the PHP script, but Iwant to be able to do things even when there are errors in the PHP.  
The problem with OCaml is that in the error case, it will not  
print the error message on the temp.txt log file as the bash script  
outside OCaml would.

---

<div class="post-metadata">

**Author:** ![jonathandoyle](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jonathandoyle](https://discuss.ocaml.org/u/jonathandoyle)\
**Post date:** [November 7, 2017, 3:19pm UTC](https://discuss.ocaml.org/t/execute-bash-script-within-ocaml-solved/1081/6 "2017-11-07T15:19:32Z")

</div>

To be clear, what I said in my last post wasn’t clear at all to me at the beginning, and there is now an easy fix : test if Sys.command is zero to know if there is an error or not.

But I’m still curious to know why OCaml acts slightly differently from bash in this case.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [November 7, 2017, 3:26pm UTC](https://discuss.ocaml.org/t/execute-bash-script-within-ocaml-solved/1081/7 "2017-11-07T15:26:52Z")

</div>

> [@jonathandoyle](#):
>
> Still, I’m curious about understanding why OCaml acts slightly differently from bash in this case.

Is it also the case if you invoke the script via `bash my_script` rather than `./my_script` ? Also maybe try `bash -i` but I doubt this will change anything. Are you sure the errors get written on `stdout` and not `stderr` ?

In any case I don’t think you’ll find something fishy in OCaml here, `Sys.command` is just a wrapper around `system(3)`.

---

<div class="post-metadata">

**Author:** ![jonathandoyle](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jonathandoyle](https://discuss.ocaml.org/u/jonathandoyle)\
**Post date:** [November 7, 2017, 3:35pm UTC](https://discuss.ocaml.org/t/execute-bash-script-within-ocaml-solved/1081/8 "2017-11-07T15:35:37Z")

</div>

> [@dbuenzli](#):
>
> In any case I don’t think you’ll find something fishy in OCaml here, `Sys.command` is just a wrapper around `system(3)`.
> 
> Is it also the case if you invoke the script via `bash my_script` rather than `./my_script` ?

You’re right, `bash my_script` acts differently from `./myscript` in this situation. Case closed, OCaml proved innocent !  
Thanks for all your help.

---

<div class="post-metadata">

**Author:** ![perry](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/perry/32/518_2.png) [@perry](https://discuss.ocaml.org/u/perry)\
**Post date:** [November 7, 2017, 4:43pm UTC](https://discuss.ocaml.org/t/execute-bash-script-within-ocaml-solved/1081/9 "2017-11-07T16:43:29Z")

</div>

By the way, just a general pair of notes:

1. if your shell script lists its command interpreter explicitly in its first line via the `#!` mechanism and is set executable you need not specify the shell that is used to execute it. The kernel does all that nice work for you. Just pretend it’s like any other executable file.
2. When you specify things to execute (say `bash` or your script), it is good style to give full path.
