196

How do i check for the correct number of arguments (one argument). If somebody tries to invoke the script without passing in the correct number of arguments, and checking to make sure the command line argument actually exists and is a directory.

Andrew K
  • 1,981
  • 2
  • 13
  • 6

3 Answers3

276
#!/bin/sh
if [ "$#" -ne 1 ] || ! [ -d "$1" ]; then
  echo "Usage: $0 DIRECTORY" >&2
  exit 1
fi

Translation: If number of arguments is not (numerically) equal to 1 or the first argument is not a directory, output usage to stderr and exit with a failure status code.

More friendly error reporting:

#!/bin/sh
if [ "$#" -ne 1 ]; then
  echo "Usage: $0 DIRECTORY" >&2
  exit 1
fi
if ! [ -e "$1" ]; then
  echo "$1 not found" >&2
  exit 1
fi
if ! [ -d "$1" ]; then
  echo "$1 not a directory" >&2
  exit 1
fi
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
  • 1
    @Andrew K: which line is it reporting this on? If it's the "if" line, try cutting out one of the two clauses making it either `if [ "$#" -ne 1 ] ; then` or `if ! [ -d "$1" ]; then` to see which clause is causing the trouble. – Laurence Gonsalves Dec 03 '10 at 01:24
  • I figured it out thank you. How about if the file name doesnt exist? – Andrew K Dec 03 '10 at 01:25
  • Doesn't exist == not a director as far as `-d` is concerned. If you'd like to add a separate check you can use `-e` to check for existence. – Laurence Gonsalves Dec 03 '10 at 01:27
  • if [ -e "$1" ] then echo "$1 : No such directory" exit 1 fi – Andrew K Dec 03 '10 at 01:30
  • @Andrew K: you want to invert the check. `-e` returns true if the file exists. I added more friendly error reporting to the answer. – Laurence Gonsalves Dec 03 '10 at 01:31
  • @Andrew K: somehow I hadn't noticed your "SSH" comment earlier. I just wanted to let you know that despite being called "the secure shell", ssh isn't really a "shell". ssh just connects you to another machine (like telnet, but in a much more secure manner) and then a shell is run on that machine for you. If you don't know what shell you're using it's probably bash, as it's the standard shell on pretty much every Linux distro out there (and also on the Mac). – Laurence Gonsalves Dec 14 '10 at 07:11
  • If you need to check that the number is less than or larger than a limit use [`-gt`, `-ge`, `-lt` and `-le`](http://www.tldp.org/LDP/abs/html/comparison-ops.html). – Florian Brucker Mar 10 '16 at 08:14
  • what is this doing? >&2 – JavaSheriff Oct 22 '20 at 18:47
  • @JavaSherrif in bash and related shells, the syntax &number refers to a file descriptor. File descriptor 2 is stderr. So `>&2` is redirecting (stdout) to stderr. – Laurence Gonsalves Oct 26 '20 at 04:23
28

cat script.sh

    var1=$1
    var2=$2
    if [ "$#" -eq 2 ]
    then
            if [ -d $var1 ]
            then
            echo directory ${var1} exist
            else
            echo Directory ${var1} Does not exists
            fi
            if [ -d $var2 ]
            then
            echo directory ${var2} exist
            else
            echo Directory ${var2} Does not exists
            fi
    else
    echo "Arguments are not equals to 2"
    exit 1
    fi

execute it like below -

./script.sh directory1 directory2

Output will be like -

directory1 exit
directory2 Does not exists
VIPIN KUMAR
  • 3,019
  • 1
  • 23
  • 34
17

You can check the total number of arguments which are passed in command line with "$#" Say for Example my shell script name is hello.sh

sh hello.sh hello-world
# I am passing hello-world as argument in command line which will b considered as 1 argument 
if [ $# -eq 1 ] 
then
    echo $1
else
    echo "invalid argument please pass only one argument "
fi

Output will be hello-world

chicks
  • 2,393
  • 3
  • 24
  • 40
HaSnen Tai
  • 1,353
  • 2
  • 14
  • 22