开发者

bash: how _best_ to create a script from another script

What is a generalized way to create a bash script from another script.

For instance:

$./script1 arg1 ar开发者_Go百科g2 > script2
$./script2
$arg1 arg2

I can think of a few ways like simply echoing the output but I was wondering if there was a better way or a command I didn't know about and a google search wasn't very helpful.

Thanks in advance for any and all help.


Any way of outputting from the first script will work, so echo or cat with a heredoc should be fine:

cat << EOT
these will be
the lines of
the second script
EOT


Generally, when you have one file create another, you are performing some kind of templating. While bash is capable of this, you may want to consider another tool. m4 is a popular tool used in the GNU tool chain. For even more basic templating, you can do something like the following perl script:

perl -pne BEGIN { open my $fh, $ENV{SERVER_PROPERTIES_FILE} or die $!; \
%hash = map { chomp; split /=/ } <$fh>; } s/\${(.*)}/$hash{$1} or die "missing $1"/eg' <     "${SERVER_XML_FILE}

If you don't need a second file, you can accomplish most things within a bash script using here documents and inline execution.


If you don't need the extra file

$ script1 arg1 arg2 | bash

or, if you want a file

$ script1 arg1 arg2 | tee script2 | bash
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜