Blue42
Blue42

Reputation: 139

What exactly is this code doing?

Could anyone tell me what this piece of code is doing?

            usage()
            {
                 echo "Usage: $0 -p ";
                 echo " p - process id ";
                 exit 1;
            }

I am a unix novice so just trying to piece together example scripts to get a grasp.

Upvotes: 0

Views: 46

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 754900

It is a shell function called usage(). It reports how the program (script) is supposed to be used and exits.

Apparently, you should type:

$ script -1234

to work on process 1234. If you misused the script, it would replace $0 with the script name:

Usage: script -p
 p - process id

It should be reporting usage to standard error:

echo "Usage: $0 -p" >&2
echo " p - process id" >&2

The semicolons are superfluous.

Upvotes: 2

fedorqui
fedorqui

Reputation: 290315

usage is a function. When called, it prints:

Usage: $0 -p
p - process id

Where $0 is set to the name of the file.

And finally exits.

This is a typical function created to show users how to use a specific command. It is called whenever the number of parameters is not correct / the parameters given are incorrect.

In this specific case, it explains that the script has to be executed with -p parameter.

Upvotes: 1

Related Questions