Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a group of commands to a shell script in bash

Tags:

bash

I'm trying to write a script that takes a group of commands on the command line (similar to how you can do with the "time" command), have it execute that group of commands and then based on the error code returned do something (if your curious, I'm having it call back to a speech daemon on my desktop to echo back the command and if it failed or succeeded - using this for some long running build processes)

With the unix time command if I have a list of commands I can run

> time (do_build && deploy_build)

With my command (which i call tellme) when I try this I get

> tellme (do_build && deploy_build)
bash: syntax error near unexpected token `do_build'

It doesn't seem to like the group of commands as parameters even though the exact same thing works fine for the time command.

Any ideas?

like image 892
Andrew Avatar asked Dec 31 '25 18:12

Andrew


2 Answers

I'm guessing you'll have to do something like

tellme 'do_build && deploy_build'

And inside tellme

eval "$*"
like image 115
glenn jackman Avatar answered Jan 02 '26 09:01

glenn jackman


time is actually executing its (do_build && deploy_build) parameter and waiting for it.

$ time (sleep 1; echo hello world;);
mundo

real    0m1.098s
user    0m0.030s
sys     0m0.000s

$ echo asd (sleep 1; echo hello world;);
bash: syntax error near unexpected token `('

$ (sleep 1; echo hello world;);
mundo

You can't use subshells wherever you want.

In your case you need to use something like

$ (sleep 1; echo hello world) && tellme && echo done;
like image 22
KurzedMetal Avatar answered Jan 02 '26 09:01

KurzedMetal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!