0

I have a CLI which generates a bash script. How can I evaluate it immediatly without redirecting to a .sh file?

example is to change the following:

~: toolWhichGeneratesScript > tmp.sh
~: chmod +x tmp.sh
~: ./tmp.sh

to something like:

~: toolWhichGeneratesScript | evaluate
2
  • Thanks all for the very FAST answers. Embarrassed that I didn't think of that! Whose answer should I except??? Commented Sep 15, 2016 at 17:38
  • I would accept the one suggested by @thatotherguy because it's the most flexible solution, unless your toolWhichGeneratesScript might create a very long script, in which case the other ones are safer. Commented Sep 16, 2016 at 6:12

3 Answers 3

2

You can pass in commands to run with bash -c (or sh -c):

bash -c "$(toolWhichGeneratesScript)"
   -c        If the -c option is present, then  commands
             are read from the first non-option argument
             command_string.   If  there  are  arguments
             after the command_string, they are assigned
             to the positional parameters, starting with
             $0.

Unlike piping to the shell, this leaves stdin free for you to interact with prompts and programs the script runs.

Sign up to request clarification or add additional context in comments.

Comments

1

The shell reads its script from standard input:

toolWhichGeneratesScript | sh

(In fact, an interactive shell does the same; it's standard input just happens to be a terminal.)

Note that you need to know which shell to use; if your tool outputs bash extensions, then you have to pipe it to bash. Also, if the generated script itself needs to read from standard input, you have a bit of a problem.

Comments

0

Try to do this : toolWhichGeneratesScript | bash

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.