Faculty of Engineering and Mathematical Sciences 
Not logged in (login)


This forum is provided to promote discussion amongst students enrolled in Open Source Tools and Scripting (CITS4407).
RSS cloud
Jump to:

About exercise 4

1 of 390 articles shown, currently no other people reading this forum.
From: Christopher M.
Date: Wed 29th Apr 2020, 5:03am


"Kebing Zhao" <22*6*7*[email protected]*u*e*t*u*a*e*u*a*> wrote:

> I watched the lecture recording last week, I have some questions about exercise 4. 
> Firstly, I don't understand clearly what the "<<" in "cat << The_End" means, I know 
> ">", ">>", but "<<" confuses me. Besides, I am also confused about "$1", sometimes I 
> saw "$0" somewhere. I don't understand what "$1" or "$0 represents ( or even "$2", 
> "$3") Could u help me out?

Hello Kebing,  OK there's two questions here, one about "here document" (using << ), and another about accessing command-
line arguments  ($1, $2, ...)

You're familiar with input redirection:

   command [optional-arguments] < input-filename

where the input for a command is read from a file on disk.  Now, if you wanted to place that command sequence in a 
shellscript, and give that script to someone else, you also have to give them input-filename to provide the input for the 
command.  So they'd need to copy around and keep together two files, which may be problematic over time.

So, instead of requiring a separate script and an input file (or, imagine, possibly multiple input files), we can use "here 
documents" to place the fixed/static input into the shellscript.  I think the name comes from "here is the input".
There's actually multiple ways to do this:

- we could simply echo a single line of text input with    echo here are six words of input | command

- or even have multiple lines of input with:
( echo line one
  echo line two
  echo line three
) | command

but it all becomes a bit unreadable, and we have to be very careful with characters that have special meaning to the shell, 
such as    | < > >> $ ( ) ;  and the control-flow keywords,  etc.

Instead we use "here documents", sometimes named "hereis redirection", where we just provide a large block of unmodified 
text to a command. For example:

line one
line two
line three

where the 3 lines of text are provided as input to the command.
The word END_OF_TEXT_INDICATOR can be anything, and (obviously) shouldn't be something in the input lines.
So now the shellscript provides the commands to execute and the fixed input to those commands.

There are actually many forms of "here documents", as described in detail:


The answer to your question about command-line is much easier.
We know that a shellscript is a text file containing commands.  Often we wish to provide different inputs or command-line 
arguments to those commands, or even make the shellscript conditionally execute its commands.
We can provide command-line arguments to the script to make it perform differently.
Within the script, its command-line arguments are accessed like all other environment variables, by preceding their name 
with a dollar-sign.

Consider this shellscript (suggest you cut-and-paste it into a file, make the file executable):

  echo command-name - $0
  echo first - $1
  echo second - $2
  echo third - $3
  echo all - $*

Now, execute the script, providing some command-line arguments:

  prompt>  ./trythis aaa bbb ccc ddd eee
  command-name - ./trythis
  first - aaa
  second - bbb
  third - ccc
  all - aaa bbb ccc ddd eee

Notice that the shellscript can access its own name, with $0. This is useful because the script can now display error 
messages, or its progress, by indicating where the message came from.

Other command-line arguments, even all of them, can be accessed in the script, performs to provide the names of files or 
fixed text for commands within the script.

Hope this helps.

Related articles

About exercise 4 (all 3) RSS
├─ original   Mon 27th Apr 2020, 4:21pm, Kebing Z.
├─ THIS   Wed 29th Apr 2020, 5:03am, Christopher M.
└─ reply 2   Sat 2nd May 2020, 6:22pm, Kebing Z.  O.P.
This Page

Program written by: [email protected]
Feedback welcome
Last modified:  2:34pm Sep 18 2020