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
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.
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.
Try to do this : toolWhichGeneratesScript | bash
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.