Bash scripting help
#!/bin/bash
usage() { echo "usage: ‘basename $0‘ project_name" }
if [ $# -ne 1 ]
then
usage
exit 1
fi
mkdir $1
What does this co开发者_如何学编程de do?
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
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.
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
精彩评论