30

Consider:

#!/bin/bash

/u01/app/oracle/middleware/oracle_common/common/bin/wlst.sh <<!END
connect('user','pw');
p=redeploy('application');
p.printStatus();
exit();
!END

The above script is there to help me to perform a redeploy for weblogic application. The area I don't understand is wlst.sh <<!END as well as the last !END.

What's the function of both !END statements?

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Isaac
  • 411
  • 4
  • 7

1 Answers1

50

It's a Here Document -- the lines between <<!END and !END are fed to the stdin of wlst.sh

It could also be expressed as:

echo "connect('user','pw');
p=redeploy('application');
p.printStatus();
exit();" | /u01/app/oracle/middleware/oracle_common/common/bin/wlst.sh

but without the potential effects of running the command in a subshell.

It could also also be expressed as:

echo "connect('user','pw');
p=redeploy('application');
p.printStatus();
exit();" > someFile
/u01/app/oracle/middleware/oracle_common/common/bin/wlst.sh < someFile
rm someFile
glenn jackman
  • 25,463
  • 6
  • 46
  • 69
  • 27
    Just to make it clear, it doesn't have to be "!END" - it can be any string. Whatever string is following **<<** will mark the end of the Here Document when it's encountered a second time (I usually use just END). – Baard Kopperud Mar 05 '20 at 14:03
  • 15
    Might be good to mention that while history expansion isn't typically enabled for scripts (non-interactive shells), `<<!END` would be subject to history expansion were it to be enabled. It's better to use a string that doesn't use a `!`. – JoL Mar 05 '20 at 17:35
  • 1
    Where I work, `EOHD` (for "End Of Here Document") is commonly used. – Bob Jarvis - Слава Україні Mar 06 '20 at 03:30
  • 2
    I'll use things like `END_USAGE` or `END_HTML` to give the heredoc a bit more context. – glenn jackman Mar 06 '20 at 15:17
  • 6
    I find this discussion about the choice of the terminating string fascinating, given that I don't think I've ever seen anything except `EOF` used unless there were nested or interleaved here-documents involved. – Austin Hemmelgarn Mar 06 '20 at 18:24
  • I always use `ZZ`, and I didn't come up with that by myself. – reinierpost Mar 07 '20 at 00:29
  • @AustinHemmelgarn I think the usage of EOF, which stands for "End Of File", is miss leading when used as a delimiter for a Here-Document as the file which includes the Here-Document usually doesn't end at the EOF. Also the Here-Document by itself is not a file either. – bey0nd Mar 24 '20 at 07:06