Using golang variable in bash script Command

286 views
Skip to first unread message

PK

unread,
Sep 19, 2022, 5:50:36 AM9/19/22
to golang-nuts
HI everyone, can anyone help me with this:
I want to list all the files having some user's entered string as substring in their name.
So I wrote these commands:

scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Enter the substring name")
scanner.Scan()
search:=scanner.Text()
cmd1,err:=exec.Command("bash", "-c", "find . -name '*$search*'")


But I think the variable search is not getting used inside exec command.

Can anyone please let me know, how I can use the golang variable in bash command this?

Thanks

Jan Mercl

unread,
Sep 19, 2022, 6:01:12 AM9/19/22
to PK, golang-nuts
On Mon, Sep 19, 2022 at 11:50 AM PK <princ...@gmail.com> wrote:

> search:=scanner.Text()
> cmd1,err:=exec.Command("bash", "-c", "find . -name '*$search*'")

Try something like this, not tested:

        cmd, err := exec.Command("bash", "-c", fmt.Sprintf("find . -name %s", search))

PK

unread,
Sep 19, 2022, 6:04:32 AM9/19/22
to golang-nuts
then we don't need to add these commands 

scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Enter the substring name")
scanner.Scan()
search:=scanner.Text()

 right???

we only need to keep this one

 cmd, err := exec.Command("bash", "-c", fmt.Sprintf("find . -name %s", search))

Brian Candler

unread,
Sep 19, 2022, 8:25:31 AM9/19/22
to golang-nuts
On Monday, 19 September 2022 at 10:50:36 UTC+1 princ...@gmail.com wrote:
search:=scanner.Text()
cmd1,err:=exec.Command("bash", "-c", "find . -name '*$search*'")

You first "search" is a go local variable.
Your second "$search" is seen by the shell as a reference to an environment variable.

So if you want to do it that way, you need to set it in the environment, e.g. with os.Setenv.

Amnon

unread,
Sep 19, 2022, 8:52:39 AM9/19/22
to golang-nuts
What happens if the user enters the string "'; rm -fr ~;'" ?

Jan Mercl

unread,
Sep 19, 2022, 8:55:42 AM9/19/22
to Amnon, golang-nuts
On Mon, Sep 19, 2022 at 2:52 PM Amnon <amn...@gmail.com> wrote:

> What happens if the user enters the string "'; rm -fr ~;'" ?

https://twitter.com/codepo8/status/1373224835866882048

Tamás Gulácsi

unread,
Sep 19, 2022, 10:56:21 AM9/19/22
to golang-nuts
No, please, no!

Do NOT use bash with string interpolation if possible!
Call find directly:
cmd, err := exec.Command("find", ".", "-name", search)

TECHAX

unread,
Sep 19, 2022, 12:08:24 PM9/19/22
to Tamás Gulácsi, golang-nuts
Thank you for the response.

--
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/fb987200-6678-443f-a0ab-884b5f77c324n%40googlegroups.com.

Robert Solomon

unread,
Sep 20, 2022, 9:13:03 AM9/20/22
to golang-nuts
I've done this for Linux and windows, as I regularly use both.  But I used a different strategy.  I used the os functions to retrieve a slice of strings that I can then filter and sort as I desire.  And I use the command line to get the target string.

also in that repo is ds and rex that do this. dsrtr and dsrtre search all subdirectories down from the current dir for what you describe.
And I also sort the returned list by time w/ the most recent first.
--rob solomon

Rich

unread,
Sep 23, 2022, 5:06:57 PM9/23/22
to golang-nuts
I do a lot of 'scripting' like things with Go - I found that using github.com/bitfield/script makes it a TON easier to run commands and you can pipe a command into another command, etc. Worth checking it out. 
Reply all
Reply to author
Forward
0 new messages