#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Compare filenames


    is there a way to check if one filename is the same as another? I've tried writing a script called samefile, which takes in 2 arguments, each being a file and compares their names

    This is what I have so far:

    if [ $1 != $2 ]
    then
    echo "The two files are not the same!"
    exit 1
    elif [ $1 = $2 ]
    echo "The two files are the same!"
    exit 1
    fi

    Clearly this does not work, so that's why I'm posting here. Please help!
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    844
    Rep Power
    387

    Cool


    Originally Posted by Trinimini
    ... Etc ...
    Clearly this does not work, so that's why I'm posting here. Please help!
    Code:
    if [ ! "$1" = "$2" ]
    then
      echo "The two file names are not the same!"
      exit 1
    else
      echo "The two file names are the same!"
      exit 0
    fi
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,200
    Rep Power
    14
    hello all
    - sure you know, file names are unique in A dir
    so $1 and $2 have different path
    that 'if' will always return 1
    you must use 'basename $1'
    - the 'if' statement for a string comp is not the best way, use 'case'
    - that 'else' AFTER an 'exit' is really useless
    - check for number of params
    - also use different exit codes
    - try something like
    Code:
         case $# in 2) ;; *) echo wrong # params; exit 1;; esac
         case `basename $1` in `basename $2`) 
                  echo $1 and $2 have the same basename; exit 0;; esac
         echo $1 and $2 have different basename
         exit 2
    working on Solaris[5-9], preferred languages french and C.

IMN logo majestic logo threadwatch logo seochat tools logo