-3

I have a bash script in which I am receiving response of an api in string format. The response is in the following format:

foo bar test stack over flow

Now I am having following bash script to convert it to array and process further:

#!/bin/bash

result=$(curl "API URL")
resp=($result)
for i in "${resp[@]}"
do
    echo "$i" 
done

Problem:

If I run this script manually in the terminal (by making it executable) it works fine. But when I try to run it by using Golang sh command

ExecuteCommand("sh /path/to/directory/test.sh")

func ExecuteCommand(command string) error{
    cmd := exec.Command("sh", "-c",command)
    var out bytes.Buffer
    var stderr bytes.Buffer
    cmd.Stdout = &out
    cmd.Stderr = &stderr

    err := cmd.Run()

    if err != nil {
        fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
        return err
    }
    fmt.Println("Result: " + out.String())
    return nil
}

It gives me error:

test.sh: Syntax error: "(" unexpected

can someone help me what am I doing wrong ?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Amandeep kaur
  • 985
  • 3
  • 15
  • 35
  • 5
    sh != bash. Try with `/bin/bash /path/to/directory/test.sh` – Corentin Limier Jan 14 '20 at 10:31
  • 1
    Using `sh` to run a script overrides the shebang. Make it executable, and then run it just by specifying its path (`/path/to/directory/test.sh`) so that the shebang gets properly respected. – Gordon Davisson Jan 14 '20 at 10:35
  • 1
    you can also probably simply do `bash /path/to/directory/test.sh` instead of `sh /path/to/directory/test.sh` – Corentin Limier Jan 14 '20 at 10:37
  • I have updated my question for complete requirement. Apology for the incomplete details! – Amandeep kaur Jan 14 '20 at 10:38
  • 3
    @Amandeepkaur : Why do you do `sh` in `ExecuteCommand("sh /path/to/directory/test.sh")` if `sh` is already in `cmd := exec.Command("sh", "-c",command)` + why don't you try with `bash` instead of `sh` + if your file can be executed, why don't you just execute it instead of using `sh` ? – Corentin Limier Jan 14 '20 at 10:45
  • 1
    Be aware that you can also consume your API using directly Golang – Corentin Limier Jan 14 '20 at 10:46
  • @CorentinLimier can you tell me why this gives error while splitting from string ? – Amandeep kaur Jan 14 '20 at 11:10
  • 2
    @Amandeepkaur simply because your `sh` is probably pointing to `dash` or any other version that does not support bash arrays. – Corentin Limier Jan 14 '20 at 11:14
  • on my os, `bash -c 'a=("hello" "world")'` works and `dash -c 'a=("hello" "world")'` doesn't and raises `dash: 1: Syntax error: "(" unexpected` – Corentin Limier Jan 14 '20 at 11:15
  • 1
    @Amandeepkaur, because you're executing your script with sh, which isn't necessarily the same as bash (it symlinks to dash on many distros by default). You can't use bash features such as arrays in other shells. – Peter Jan 14 '20 at 11:16
  • You're welcome. You can accept @Vorsprung's answer that should work too (if your bash file is executable). Btw, I voted to reopen the question, I don't understand why it is considered as off-topic – Corentin Limier Jan 14 '20 at 12:35

2 Answers2

1

change these lines in your example

ExecuteCommand("/path/to/directory/test.sh")

func ExecuteCommand(command string) error{
    cmd := exec.Command(command)

The kernel shebang will intercept the #! line and correctly run the script

Vorsprung
  • 32,923
  • 5
  • 39
  • 63
1

U can also create a shortcut from "test.sh" file :

#!/bin/bash
$ sudo ln -s /path/to/directory/test.sh /usr/bin/testSH

If u arent root user give permission to the shortcut :

sudo chmod 777 /usr/bin/testSH

command := "testSH" 
cmd := exec.Command("sh", "-c",command).Run()
Hamed M
  • 329
  • 2
  • 10