PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

[FIXED] How to execute a command with backslashes in go

 August 18, 2022     exec, go, output     No comments   

Issue

I want to return the output of an app version using go

Why does the below fail?

emulatorCmd := exec.Command(`mdls`, `-name`, `kMDItemVersion`, `/Applications/Bot\`, `Framework\`, `Emulator.app`)
emulatorVersion, err := emulatorCmd.Output()
if err != nil {                      
   log.Fatalf("cmd.Run() failed in emulator", err)                       
   fmt.Println("Looks like you don't have the Bot Emulator installed yet, download here: https://github.com/Microsoft/BotFramework-Emulator/releases/tag/v4.5.2")
} else {
   fmt.Println("Emulator:", string(emulatorVersion))

}

Giving:

cmd.Run() failed in emulator%!(EXTRA *exec.ExitError=exit status 1)

But when I replace the command with an app that has no spaces in the name (thus, doesn't require backslashes) like

exec.Command(`mdls`, `-name`, `kMDItemVersion`, `/Applications/FaceTime.app`)

It's successful, giving:

Emulator: kMDItemVersion = "5.0"

I tried putting it all in one string, but spaces in the command need to be separated by a comma. Also tried using two '\' backslashes and now even using back ticks does not work.

Also looked at the go string literals page but didn't find something relevant to this problem: https://golang.org/ref/spec#raw_string_lit


Solution

The backslashes you need in command line are to escape the spaces to prevent them from being treated as argument separators by the shell. Since there is no shell involved executing this way in Go, you don't need them. You can just:

exec.Command(`mdls`, `-name`, `kMDItemVersion`, `/Applications/Bot Framework Emulator.app`)


Answered By - Adrian
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing