Test (Unix) - Examples

Examples

1. To test whether a file is nonexistent or empty, type:

if test ! -s "$1" then echo $1 does not exist or is empty. fi

If the file specified by the first positional parameter to the shell procedure, $1, does not exist or is of size 0, the test command displays the message. If $1 exists and has a size greater than 0, the test command displays nothing.

Note: There must be a space between the -s function and the file name.

The quotation marks around $1 ensure that the test works properly even if the value of $1 is a null string. If the quotation marks are omitted and $1 is the empty string, the test command displays the error message

test: argument expected.

2. To do a complex comparison, type:

if then exit fi

If the shell procedure is given fewer than two positional parameters or the file specified by $1 does not exist, then the shell procedure exits. The special shell variable $# represents the number of positional parameters entered on the command line that starts this shell procedure.

Read more about this topic:  Test (Unix)

Famous quotes containing the word examples:

    In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.
    Michel de Montaigne (1533–1592)

    Histories are more full of examples of the fidelity of dogs than of friends.
    Alexander Pope (1688–1744)

    No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.
    André Breton (1896–1966)