简体   繁体   中英

Printing the word echo in the output of cat file

I am trying to generate a file using cat in bash where inside the bash, i already ran a script, that i saved into a variable then will be used inside the cat. To ran the script and save the output to a variable, I used the following:

declare RESULT=$(./script.pl 5 5 5)

Next, I show an excerpt of the cat file where the variable RESULT is being used.

  cat> input.in<<EOF 
  bla bla
  echo $RESULT 
  EOF

What I obtain after running the bash, is the correct output of the variable RESULT. However, there is a word echo in the beginning of the file (as shown below). This is problematic because I am trying to automate a code, and adding the word echo ruins the code.

echo K_POINTS crystal    
     64
     0.00000000  0.00000000  0.00000000  1.562500e-02 
     0.00000000  0.00000000  0.25000000  1.562500e-02 
     0.00000000  0.00000000  0.50000000  1.562500e-02

Can you tell me please what is the problem? Thanks a lot!

Here documents don't run the contents as commands, they just expand values like $RESULT .

cat> input.in<<EOF 
bla bla
$RESULT 
EOF

If you wanted to actually get the output of a command, you can use $(mycommand myarg) but obviously this is pointless for echo

Maybe I'm not reading this correctly (apologies if so), but it looks like you are expecting the heredoc to be run as a script. It is not. It is just created and cat'ed to input.in, and the string 'echo' is included in that output.

Try removing the string 'echo' from the heredoc (I assume that your perl output starts with the token K_POINTS).

ie:

declare RESULT=$(./script.pl 5 5 5)

cat> input.in<<EOF 
  bla bla
  $RESULT 
  EOF

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM