How I can translate this shell command to golang code?

102 views
Skip to first unread message

Franco Marchesini

unread,
Jun 22, 2020, 3:54:39 PM6/22/20
to golang-nuts
Help,

how I can translate this shell command :

echo -n 123456 | dmtxwrite -s 16x48  -o image.png

to golang

 exec.Commad()

Thanks in advance
Franco

Jan Mercl

unread,
Jun 22, 2020, 4:00:29 PM6/22/20
to Franco Marchesini, golang-nuts
On Mon, Jun 22, 2020 at 5:54 PM Franco Marchesini
<franco.m...@gmail.com> wrote:

> how I can translate this shell command :
>
> echo -n 123456 | dmtxwrite -s 16x48 -o image.png

Not tested: https://play.golang.org/p/Jh9ix17TlKG

Michael Jones

unread,
Jun 22, 2020, 4:01:04 PM6/22/20
to Franco Marchesini, golang-nuts

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c6aaba94-453b-41dc-83a4-ffd4f3912494o%40googlegroups.com.


--
Michael T. Jones
michae...@gmail.com

Tyler Compton

unread,
Jun 22, 2020, 4:14:14 PM6/22/20
to Franco Marchesini, golang-nuts
I just want to highlight an easy mistake to make here. One might expect to be able to run something like this:

exec.Command("echo", "-n", "123456", "|", "dmtxwrite", "-s", "16x48", "-o", "image.png")

But, as you might have found, this doesn't work. This is because the pipe "|" is an operator implemented by shells like sh and bash. As mentioned in the overview of the os/exec package, the package never invokes your shell automatically, so operators like the pipe operator are not available by default. Jan's answer works because it invokes the shell explicitly to run your command.

Bakul Shah

unread,
Jun 22, 2020, 4:19:36 PM6/22/20
to Franco Marchesini, golang-nuts
You can avoid calling the shell by observing that you are simply passing "123456" as input, which you can do in a Go Program itself.

Franco Marchesini

unread,
Jun 22, 2020, 4:21:29 PM6/22/20
to golang-nuts
Ciao,

I had read this tutorial, but  my mistake was not having called the shell command


Il giorno lunedì 22 giugno 2020 18:01:04 UTC+2, Michael Jones ha scritto:
On Mon, Jun 22, 2020 at 8:54 AM Franco Marchesini <franco.m...@gmail.com> wrote:
Help,

how I can translate this shell command :

echo -n 123456 | dmtxwrite -s 16x48  -o image.png

to golang

 exec.Commad()

Thanks in advance
Franco

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.

Michael Jones

unread,
Jun 22, 2020, 4:26:12 PM6/22/20
to Franco Marchesini, golang-nuts
Bakul's answer is best... starting the shell, the shell pipeline, etc. is not free in execution time and memory use. The "do it all in dmtxwrite by sending input" is the fastest, lightest way of doing what you want.

To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/e84a97ec-d326-4ccc-8e31-bf25991ec897o%40googlegroups.com.

Franco Marchesini

unread,
Jun 24, 2020, 6:30:49 AM6/24/20
to golang-nuts
Yes, this is better.
Thanks

func main() { cmd := exec.Command("dmtxwrite", "-s", "16x48", "-o", "image2.png")
 cmd
.Stdin = strings.NewReader("123456")
 
if err := cmd.Run(); err != nil {
 panic
(err.Error())
 
}
}


Il giorno lunedì 22 giugno 2020 18:19:36 UTC+2, Bakul Shah ha scritto:
You can avoid calling the shell by observing that you are simply passing "123456" as input, which you can do in a Go Program itself.
On Jun 22, 2020, at 8:54 AM, Franco Marchesini <franco.m...@gmail.com> wrote:

Help,

how I can translate this shell command :

echo -n 123456 | dmtxwrite -s 16x48  -o image.png

to golang

 exec.Commad()

Thanks in advance
Franco

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages