UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 8th, 2011, 11:48 PM
Trinimini Trinimini is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 22 Trinimini User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 m 10 sec
Reputation 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!

Reply With Quote
  #2  
Old March 9th, 2011, 11:32 AM
LKBrwn_DBA's Avatar
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 752 LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 3 h 5 m 12 sec
Reputation Power: 348
Cool

Quote:
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

__________________

Reply With Quote
  #3  
Old April 17th, 2011, 02:37 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,200 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 1 Day 11 h 47 m 52 sec
Reputation Power: 12
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Compare filenames

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap