How to resolve the algorithm Here document step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Here document step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

A   here document   (or "heredoc")   is a way of specifying a text block, preserving the line breaks, indentation and other whitespace within the text. Depending on the language being used, a   here document   is constructed using a command followed by "<<" (or some other symbol) followed by a token string. The text block will then start on the next line, and will be followed by the chosen token at the beginning of the following line, which is used to mark the end of the text block.

Demonstrate the use of   here documents   within the language.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Here document step by step in the UNIX Shell programming language

Source code in the unix programming language

#!/bin/sh
cat << ANARBITRARYTOKEN
The river was deep but I swam it, Janet.
The future is ours so let's plan it, Janet.
So please don't tell me to can it, Janet.
I've one thing to say and that's ...
Dammit. Janet, I love you.
ANARBITRARYTOKEN


cat << EOF
Here documents do parameter and command substitution:
 * Your HOME is $HOME
 * 2 + 2 is `expr 2 + 2`
 * Backslash quotes a literal \$, \` or \\
EOF


if true; then
    cat <<- EOF
    The <<- variant deletes any tabs from start of each line.
    EOF
fi


cat << 'TOKEN'
If TOKEN has any quoted characters (like 'TOKEN', "TOKEN" or \TOKEN),
then all $ ` \ in the here document are literal characters.

$PATH \$PATH `shutdown now`
TOKEN


echo '
In any unix shell, you specify a text block and can use all whitespace like
  (spaces) &    (tab) in it, by using single quotes. 
As mentioned above, a Unix "here document" is a type of redirection.
In a literal Bash here document, the starting token must be
quoted, but the end token must not, so they are not the same, which does not
conform to the task definition.
'


#!/bin/csh -f
cat << ANARBITRARYTOKEN
 * Your HOME is $HOME
 * 2 + 2 is `@ n = 2 + 2; echo \$n`
ANARBITRARYTOKEN

cat << 'ANARBITRARYTOKEN'
$PATH \$PATH `shutdown now`
'ANARBITRARYTOKEN'


  

You may also check:How to resolve the algorithm Loops/Wrong ranges step by step in the C# programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Scheme programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the Arturo programming language
You may also check:How to resolve the algorithm One of n lines in a file step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Palindrome dates step by step in the REXX programming language