Bash: how to _best_ create a script from another script

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

For instance:

$./script1 arg1 arg2 > script2 $./script2 $arg1 arg2 

I can come up with several ways to simply repeat the output, but I was wondering if there is a better way or command that I did not know about, and a google search did not help much.

Thanks in advance for any help.

+4
source share
3 answers

Any way to output from the first script will work, so echo or cat with heredoc should be fine:

 cat << EOT these will be the lines of the second script EOT 
+3
source

If you do not need an additional file

 $ script1 arg1 arg2 | bash 

or if you want a file

 $ script1 arg1 arg2 | tee script2 | bash 
+1
source

As a rule, when you have one file, another is created, you execute some kind of template. While bash is capable of this, you can consider another tool. m4 is a popular tool used in the GNU toolchain. For simpler templates, 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 do most of the things in a bash script using the docs and inline execution here.

+1
source

Source: https://habr.com/ru/post/1300562/


All Articles