tags:

views:

44

answers:

3
#!/bin/bash
usage() { echo "usage: ‘basename $0‘ project_name" }

if [ $# -ne 1 ]
then
usage
exit 1
fi

mkdir $1

What does this code do?

+3  A: 

Basically, it creates a directory. You would run the script like this:

<scriptName> /my/directory/to/create

The [$# -ne 1] is making sure that the caller provided a single argument (the directory name), and if that's not the case, it prints the usage message and exits.

In the usage function, the $0 is replaced with the name of the script.

Assuming there isn't anything else in the script file, you can accomplish the same thing by just running:

mkdir /my/directory/to/create
Andy White
`usage` would be referred to as a "function" rather than a "subroutine".
Dennis Williamson
+1  A: 

It says that if the user did not supply one command-line argument to the script it will exit (displaying a message about usage). If invoked correctly it will create a folder with the name supplied as parameter to the script.

klausbyskov
A: 

This script will create a directory if a user provides exactly one argument for the script. Somehow more readable for of this script could be written using by adding 'else' branch.

#!/bin/bash
usage() { 
    echo "usage: ‘basename $0‘ project_name"  
}

if [ $# -ne 1 ]
then
    usage
    exit 1
else 
    mkdir $1
fi
jam