donglangtun1850 2019-06-17 18:58
浏览 65

关于args和从position [1](不是position 0)传递args的问题

I am reading a commandline string from a config file (config.json) :

"execmd" : "c:\\windows\\system32\\cmd.exe /c runscript.cmd"

I want to pass this to exec.Command() - but this function requires 2 parameters:

exec.Comm*emphasized text*and (cmd, args...)

Where cmd is the first segment (cmd.exe) and args would then be every space deliminated value thereafter.

  1. I am not exactly sure if I need to read the config string, and then manually split it up in an array for each space deliminator? Is there any way of converting a string into args easily?

  2. How would it be possible to do something like this, where I can refer args... from an index? (the below code doesn't work, cant refer args this way)

    exec.Command (arg[0], args[1]...)

  • 写回答

2条回答 默认 最新

  • doulu8537 2019-06-17 19:00
    关注

    If the values coming it from the config file are in a format executable by shell, you're going to run into a host of problems just splitting on spaces (e.g. quoted arguments containing spaces). If you want to take in a command line that would be executable in a shell, you're going to want to have a shell execute it:

    exec.Command("cmd.exe", "/c", execmd)
    

    There is no way of "converting a string into args" because it varies from shell to shell.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度