Positional Parameters 0 and 1 through 9

How we do we send a script some arguments?

The list of argument or argument buffers provided system wide are numbered from 1 upwards.

The first argument on the command line is seen as the first parameter, the second argument (if we had two arguments) as the second parameter etcetera.

We can have up to 9 arguments on the command line, well that's not quite true, we can have a lot more than 9 arguments but we will see how to deal with more than 9 in a moment.

Within our script we can use a parameter marker and access it with prefacing it with the $ sign, for example, if we run our script using one argument as follows:

./eatout.sh italian
	
echo $1
            

will output:

italian
            

If we told it to:

echo $2
            

It would echo nothing on the command line because we only called the script with a single argument.

So we've got up to 9 positional arguments that we can use in a script. We've got a special argument $0. We've used this before on the command line, if you:

echo $0 
            

It tells you what your current shell is, in my case it's /bin/bash.

Now edit a file called eatout.sh and enter the script as follows:

#!/bin/bash
DATE=$(date +"%d %b %Y %H:%M")
TYPE=$1
echo "Queried on $DATE"
grep $TYPE restaurants.txt |sort -t, +3n
exit 0
            

In order to save the first positional argument, I've saved it to a variable called TYPE. Part of the reason why I've assigned $1 to a variable, is that $1 can then be reset and will loose the contents of $1.

At this point I grep the relevant restaurant type from " restaurants.txt" and sort the output numerically by piping it through the sort command.

Remember that we must make the script executable before we can run it:

chmod +x eatout.sh
./eatout.sh steakhouse
            

The output looks as follows:

riaan@debian:~> ~/ham$ ./eatout.sh steakhouse
Queried on 01 Dec 2003 21:30
steakhouse,Nelsons Eye,6361017,8
steakhouse,Butchers Grill,6741326,7
            

That shows us only the restaurants that we're interested in and it will sort them numerically according to which restaurants we like best (determined from the rating), and which ones we like least.

So we've seen that we can send any number of positional arguments to a script.

As an exercise, show only restaurants where the rating is greater than 5 (we don't want to go to bad restaurants)

./eatout.sh italian 5 
            

It should only show italian eating places that have a rating of greater than 5.

Hint, the rating is now stored as a positional parameter and is the second argument this will be positional parameter $2.

See the following exercises where you are going to edit your file to use $2 and look for all the restaurants having a rating of greater than 5. There are many ways to skin this cat, so I'll leave it to you to find just one of these.

Exercises:

  1. Write a script to display only restaurants in the category having a rating greater than 5. Sort the list from the best restaurant to the worst.

  2. Alter your script above to display the output in a nicely formatted way, using the echo commands.

Challenge sequence:

  1. Use the ncurses libraries to format the output.