howto run a bash compressed script?

2019-05-17 16:21发布

问题:

There is a way to run compressed bash script with 'arguments' directly on the fly without decompressing it in a file and then running the decompressed file ?

for example: i need to execute the setup-mysql gzip compressed script with some given arguments: "-n", "wordpress", "locahost", without decompressing the script first and then executing.

What i'm looking for is a replacement of the word MAGIC... in my command below:

gzip -d --stdout /usr/share/doc/wordpress/examples/setup-mysql.gz | MAGIC... -n wordpress localhost

回答1:

Try this:

gzip -d --stdout file.gz | bash -s -- "-n wordpress localhost"

A little explanation: with bash -s you're telling bash to process stdin as commands. The double dash means that everything that follows will be passed as arguments (a single dash seems to be equivalent, check man bash).

If you didn't have any arguments to pass you could just do

gzip -d --stdout file.gz | bash

Another option is:

gzip -d --stdout file.gz | bash /dev/stdin "arguments"


标签: bash gzip