LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2006, 12:55 AM   #1
shashwat.gupta
LQ Newbie
 
Registered: May 2006
Posts: 18

Rep: Reputation: 0
compare files in C shell


Hi all,
I want to do compare to files in C shell script but when I try to do it by
if( cmp -s shell.tmp shell) then it starts giving me error that its an if syntax error so I guess cmp is not part of C shell ( same script is working when i wrote it for bash ).
Despite seaching internet a lot I am not able to find out how to compare two file in C shell script .
Any help will be appreciated
 
Old 05-23-2006, 02:38 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
cmp is an external prog eg man cmp. I suspect you need to be more careful with the C shell syntax; try this page: docs.freebsd.org/44doc/usd/04.csh/paper.pdf or specify bash as the first line eg:
#!/bin/bash
 
Old 05-23-2006, 03:26 AM   #3
shashwat.gupta
LQ Newbie
 
Registered: May 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Thanx chrism01

Thanx buddy
I am very Thankful to you it was really nice help
 
Old 05-23-2006, 07:56 AM   #4
shashwat.gupta
LQ Newbie
 
Registered: May 2006
Posts: 18

Original Poster
Rep: Reputation: 0
its not solving my problem

As asked by the chrism01 I have studied that particular document but its still giving me error. so i am still facing some problems.
i am putting complete shell script this shell script is working if I put the #!/bin/bash instead of #!/bin/csh
this file is completely according to as that document specify ... to be more specific if i put the $hello == 0 instead of cmp -s shell shellcopy then it works well

#!/bin/bash

cat make_version_h > shell.tmp
if (cmp -s shell shellcopy) then
echo "Matched file"
else
echo "Not matched file"
fi

any help will be quiet useful to me

I am sorry for my dumbness.actually I am very new to shell scripts
 
Old 05-23-2006, 10:07 AM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
How about this?
Code:
cmp -s shell shellcopy
test=$?

if [[ "$test" -eq "0" ]]
then 
    echo "files are equal"
elif [[ "$test" -eq "1" ]]
then
    echo "files differ"
else
    echo "something went wrong"
fi
Oh, I just noticed that there's only one thing wrong with your file: try
Code:
if (cmp -s shell shellcopy); then
 
Old 05-23-2006, 11:01 AM   #6
shashwat.gupta
LQ Newbie
 
Registered: May 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Re

Thanks for your reply
but ur method is working when I put the #!/bin/bash on top of file but if I put #!/bin/csh on file then it says it says

if: Expression Syntax.

I am not getting why this problem is coming .. actually I want to compare two files in C shell.

again thanks for your reply and Informative post
 
Old 05-23-2006, 02:02 PM   #7
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
OK, so
Code:
#! /usr/bin/csh

cmp -s shell shellcopy
set TEST = $status

if ( "$TEST" == "0" ) then 
    echo "files are equal"
else if ( "$TEST" == "1" ) then
    echo "files differ"
else
    echo "something went wrong"
endif
or
Code:
#! /usr/bin/csh

if ( { cmp -s shell shellcopy } ) then 
    echo "files are equal"
else
    echo "nope"
endif
 
Old 05-23-2006, 09:08 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Exactly; differnet shells have different syntaxes for certain stuff eg 'if' cmds are a classic example, which is why I ref'd that doc.
If it works when specifying bash shell, why not do it that way? ie Why use C-shell at all, it's pretty old.
 
Old 05-24-2006, 02:13 AM   #9
shashwat.gupta
LQ Newbie
 
Registered: May 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks every One for very Important help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I compare floating number in a shell script? abefroman Programming 34 10-12-2016 08:09 AM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 02:16 PM
How to compare records in two tables in seperate My Sql database using shell script sumitarun Programming 5 04-14-2005 09:45 AM
How do I compare 2 files? linuxhippy Slackware 6 03-26-2005 01:54 AM
compare date uusing shell programming please... izza_azhar Programming 7 01-14-2005 07:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration