Check that a bash script has exactly two arguments which are directories

20,871

Solution 1

Processing arguments

Processing arguments that are passed via a script's command line is as simple as follows. Say we had this script:

$ cat cmd.bash 
#!/bin/bash

echo "arg1: $1"
echo "arg2: $2"

Now run with no arguments:

$ ./cmd.bash 
arg1: 
arg2: 

With 1 argument:

$ ./cmd.bash hi
arg1: hi
arg2: 

With 2 arguments:

$ ./cmd.bash hi bye
arg1: hi
arg2: bye

Checking the arguments

You can then check if the arguments, 1 & 2, are valid directories or not, and then bail out or proceed as needed. So we introduce 2 checks to see if the 2 arguments are directories or not, if not then exit.

$ cat cmd.bash
#!/bin/bash

[ -d "$1" ] || exit
[ -d "$2" ] || exit
[ $# == 2 ] || exit

echo "arg1: $1"
echo "arg2: $2"

Example

Say we have these directories.

$ mkdir d1 d2

$ ls -l
total 12
-rwxrwxr-x. 1 saml saml   89 Oct 14 23:13 cmd.bash
drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d1
drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d2

If we are given anything other than 2 directories, the script will simply exit.

$ ./cmd.bash hi bye

If we're given 2 directories:

$ ./cmd.bash d1 d2
arg1: d1
arg2: d2

If we're given more than 2 arguments:

$ ./cmd.bash d1 d2 d3

I'll leave the comparison of the 2 directories to you. For learning Bash I'd direct you to the link below for a free online book on Bash.

References

Solution 2

The number of arguments is in the parameter $#.

if [ $# -ne 2 ]; then
  echo 1>&2 "Usage: $0 DIRECTORY1 DIRECTORY2"
  exit 3
fi

If you want to enforce that the arguments are both directories (as opposed to other types of files), test them with -d.

The utility diff compares two files. With the option -r, it compares directories recursively.

diff -ru -- "$1" "$2"
Share:
20,871

Related videos on Youtube

Mirko Piccolo
Author by

Mirko Piccolo

Updated on September 18, 2022

Comments

  • Mirko Piccolo
    Mirko Piccolo over 1 year

    I'm writing a bash script that compares two directories, but I'm not sure how to check conditions

    For example, if the user enters 1 arg instead of 2 (we need 2 since we're comparing two directories), it should give an error. If both arg are valid, compare the directories and output to a file.

    • terdon
      terdon over 9 years
      Welcome to the site! Please take a moment and read through our help center to understand how the site works. You have now asked 6 questions and have not accepted a single answer, do none of the answers you received answer your questions?
  • Mirko Piccolo
    Mirko Piccolo over 9 years
    thank you for the explanation, but if you enter a 3rd arg, it still prints arg 1 and arg 2. I want to write a robust script that checks all possible scenarios...
  • slm
    slm over 9 years
    @asura - see update, added check for number of arguments.
  • slm
    slm over 9 years
    @asura - if the A'ers you've received solve your issue please remember to take a moment and click the check mark next to them so that other's know your Q's been resolved.
  • Mirko Piccolo
    Mirko Piccolo over 9 years
    I added [ $# == 2 ] || exit and it doesnt seem to work. What does it mean? If # of arg is 2, exit? Also I need to check more things and I can only imagine using multiple "if"s to do this... for example, 1,if arg is not exactly 2 -> give error and exit. 2, if 2 arg but one or both are invalid -> error and exit. 3, If both, exit. 4, if 2 valid arg -> compare contents of directories and output to a file.
  • slm
    slm over 9 years
    @asura - that's an or operator b/w then, ||, so either the number of arguments is 2 or exit. You might want to invest some time picking up a book or reading some tutorials if you're attempting to learn shell scripting. It's tricky to pick it up in pieces like this. Also a book will present you w/ the material in a more structured way which will avoid these types of Q's you're asking. A simple google would've lead you to many examples. BTW all the code I put in my A'ers I typically run myself, so the above worked when I tried it.
  • slm
    slm over 9 years
    @asura - also please do not change the Q's mid-stream. If you had a different goal in mind, that should've been your Q. As I've answered it, I've shown you 1 way to do it (the novice's way). There are more efficient methods but given your rudimentary Q I've shown you a basic A.