1
#!/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 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

usage would be referred to as a "function" rather than a "subroutine".
1

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.