# exec.Cmd Stdin pipe vs file

**URL:** <https://forum.golangbridge.org/t/exec-cmd-stdin-pipe-vs-file/34443>\
**Category:** Getting Help\
**Created:** [February 9, 2024, 7:29pm UTC](https://forum.golangbridge.org/t/exec-cmd-stdin-pipe-vs-file/34443 "2024-02-09T19:29:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![j-forster](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/j-forster/32/9304_2.png) [@j-forster](https://forum.golangbridge.org/u/j-forster)\
**Post date:** [February 9, 2024, 7:29pm UTC](https://forum.golangbridge.org/t/exec-cmd-stdin-pipe-vs-file/34443/1 "2024-02-09T19:29:33Z")

</div>

Hello Gophers,

I noticed some unexpected behavior when feeding a buffer to an `exec.Command`.

There is a tool to create image thumbnails, called “vipsthumbnail”, that I am using in a pipeline architecture like this:

```bash
vipsthumbnail stdin -o thumbnail.webp < original.jpeg

```

It creates the thumbnail as expected.

The go equivalent also works the same way:

```auto
original, err := os.Open("original.jpeg")
if err != nil {
	log.Fatal(err)
}

cmd := exec.Command("vipsthumbnail", "stdin", "-o", "thumbnail.webp")
cmd.Stdin = original
if err := cmd.Run(); err != nil {
	log.Fatal(err)
}

```

However, using a buffer does not work:

```auto
original, err := os.ReadFile("original.jpeg")
if err != nil {
	log.Fatal(err)
}

cmd := exec.Command("vipsthumbnail", "stdin", "-o", "thumbnail.webp")
cmd.Stdin = bytes.NewReader(original)
if err := cmd.Run(); err != nil {
	log.Fatal(err)
}

```

Using a buffer, the vipsthumbnail tool fails with some generic _“unable to read from stdin”_ message.

From the Go docs we get:

> If Stdin is an \*os.File, the process’s standard input is connected directly to that file.
> 
> Otherwise, during the execution of the command a separate goroutine reads from Stdin and delivers that data to the command over a pipe.

Is this an implementation error in the vipsthumbnail software not beeing able to handle pipes properly? How does it make a difference for that software at all? Is there anything I can do in my Go code?

Thanks for all the help!

---

<div class="post-metadata">

**Author:** ![GonzaSaya](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/gonzasaya/32/4535_2.png) [@GonzaSaya](https://forum.golangbridge.org/u/GonzaSaya)\
**Post date:** [February 9, 2024, 8:29pm UTC](https://forum.golangbridge.org/t/exec-cmd-stdin-pipe-vs-file/34443/2 "2024-02-09T20:29:43Z")

</div>

Did you try with the golang package?

> **[GitHub - davidbyttow/govips: A lightning fast image processing and resizing...](https://github.com/davidbyttow/govips)**
>
> A lightning fast image processing and resizing library for Go - GitHub - davidbyttow/govips: A lightning fast image processing and resizing library for Go

---

<div class="post-metadata">

**Author:** ![j-forster](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/j-forster/32/9304_2.png) [@j-forster](https://forum.golangbridge.org/u/j-forster)\
**Post date:** [February 9, 2024, 9:23pm UTC](https://forum.golangbridge.org/t/exec-cmd-stdin-pipe-vs-file/34443/3 "2024-02-09T21:23:52Z")

</div>

I tried to use vipsthumbnail as an example for better understanding the problem.

This issue is about the differences of passing a buffer vs using a file handle for the `exec.Command`. In my understanding, using a buffer should not affect the behavior of the command call.

---

<div class="post-metadata">

**Author:** ![Zachary\_Young](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/zachary_young/32/14379_2.png) [@Zachary\_Young](https://forum.golangbridge.org/u/Zachary_Young)\
**Post date:** [February 11, 2024, 10:22pm UTC](https://forum.golangbridge.org/t/exec-cmd-stdin-pipe-vs-file/34443/4 "2024-02-11T22:22:06Z")

</div>

My initial thought was that this has something to do with vipsthumbnail itself. I don’t have vipsthumbnail, so I tried to apply your thinking/coding to grep, and it looks like cmd.Exec does what you would expect.

I get the following:

```none
With file: "Hello, 世界\n"
With bytes reader: "Hello, 世界\n"

```

when I run this, which I tried to keep with the intent of your code/question:

```go
func main() {
	f, _ := os.Create("input.txt")
	f.WriteString("Hello, 世界")
	f.Close()

	withFile()
	withStdin()
}

func run(r io.Reader) []byte {
	cmd := exec.Command("grep", "-i", "hello")
	cmd.Stdin = r
	out, err := cmd.Output()
	if err != nil {
		panic(err)
	}
	return out
}

func withFile() {
	f, _ := os.Open("input.txt")
	fmt.Printf("With file: %q\n", run(f))
}

func withStdin() {
	b, _ := os.ReadFile("input.txt")
	fmt.Printf("With bytes reader: %q\n", run(bytes.NewReader(b)))
}

```

Have you tried any other command-line program?

---

<div class="post-metadata">

**Author:** ![j-forster](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/j-forster/32/9304_2.png) [@j-forster](https://forum.golangbridge.org/u/j-forster)\
**Post date:** [February 12, 2024, 8:50am UTC](https://forum.golangbridge.org/t/exec-cmd-stdin-pipe-vs-file/34443/5 "2024-02-12T08:50:51Z")

</div>

Thanks for testing this! I was expecting it to work as you said.

With some more testing, I can confirm that it is a problem with vipsthumbnail, and it also fails on windows only.

This **works** on both Windows and Linux:

```auto
vipsthumbnail stdin -o thumbnail.webp < "image.jpg"

```

This **fails** on windows (on Windows use `type` instead of `cat`) cmd:

```auto
cat "image.jpg" | vipsthumbnail stdin -o thumbnail.webp

```

The same **fails** in Windows PowerShell:

```auto
Get-Content -Path "image.jpg" -AsByteStream | vipsthumbnail stdin -o thumbnail.webp

```

Thank you for testing @Zachary_Young.

As this is clearly a `vipsthumbnail` issue, I will close this topic now.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [May 12, 2024, 8:51am UTC](https://forum.golangbridge.org/t/exec-cmd-stdin-pipe-vs-file/34443/6 "2024-05-12T08:51:40Z")

</div>

Closed on May 12, 2024, 8:51am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
