Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String method return a new line when followed by Exec #161

Open
ifraixedes opened this issue Dec 14, 2022 · 3 comments
Open

String method return a new line when followed by Exec #161

ifraixedes opened this issue Dec 14, 2022 · 3 comments

Comments

@ifraixedes
Copy link

Calling String followed by Exec returns a string ending with a new line (i.e. \n).

Example:

out, err := script.Exec("echo hello").String()
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%q\n", out)

It prints: "hello\n"

Is this intended or the \n shouldn't be there?

If I'm not mistaken the issue is that os/exec.Command returns it. Execute this example

package main

  import (
    "bytes"
    "fmt"
    "log"
    "os/exec"
  )

  func main() {
    out := &bytes.Buffer{}

    cmd := exec.Command("echo", "hello")

    cmd.Stdout = out
    err := cmd.Start()
    if err != nil {
      log.Fatal(err)
    }

    cmd.Wait()

    fmt.Printf("%q\n", out.String())
  }

And you see that it prints: "hello\n"

@bitfield
Copy link
Owner

If you run the command echo hello in your terminal, you'll see that it prints exactly hello\n.

@ifraixedes
Copy link
Author

ifraixedes commented Dec 15, 2022

Right.
The thing is that I ran into this issue when I was trying to do this shell

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

And how I'm doing with script is

arch, err := script.Exec("dpkg --print-architecture").String()
if err != nil {
	log.Fatal(err)
}

versionName, err := script.Exec("lsb_release -cs").String()
if err != nil {
	log.Fatal(err)
}

_, err := exec.Echo(fmt.Sprintf("deb [arch=%s signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian  %s stable", arch, versionName)).Exec("sudo tee /etc/apt/sources.list.d/docker.list").String()
if err != nil {
	log.Fatal(err)
}

Because Exec("dpkg --print-architecture") and Exec("lsb_release -cs") have a newline at the end, it doesn't work as expected and I have to trim the \n for each result.

@rmasci
Copy link

rmasci commented Mar 21, 2023

If you're looking to add a newline -- use printf instead of echo:

script.Exec(`printf "Hello There\n\n\n\n"`).Stdout() 

If you're looking to remove the newline from the end of the string you can do it like this:

versionName, err := script.Exec("lsb_release -cs").String()
if err != nil {
	log.Fatal(err)
}
versionName=strings.TrimSpace(versionName) 

That will remove all whitespace from both sides of the string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants