shell - Checking for the correct number of arguments -
how check correct number of arguments (one argument). if tries invoke script without passing in correct number of arguments, , checking make sure command line argument exists , directory.
#!/bin/sh if [ "$#" -ne 1 ] || ! [ -d "$1" ]; echo "usage: $0 directory" >&2 exit 1 fi translation: if number of arguments not (numerically) equal 1 or first argument not directory, output usage stderr , exit failure status code.
more friendly error reporting:
#!/bin/sh if [ "$#" -ne 1 ]; echo "usage: $0 directory" >&2 exit 1 fi if ! [ -e "$1" ]; echo "$1 not found" >&2 exit 1 fi if ! [ -d "$1" ]; echo "$1 not directory" >&2 exit 1 fi
Comments
Post a Comment