Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failing if diff is found (c-shell)

Tags:

shell

unix

diff

csh

Please resist the urge to tell me not to use c-shell.

I'm writing a c-shell script and I need to run a diff between two files (generated in the script). How do I run diff and return its status (if it fails, return 1, else 0)?

like image 846
Amir Rachum Avatar asked Aug 21 '26 05:08

Amir Rachum


1 Answers

In C shell you can use the variable $status to get the exit status of the command.

% echo 'hi' > foo
% echo 'ho' > bar
% diff foo foo
% echo $status
0
% diff foo bar > /dev/null
% echo $status
1

In a script you can do something like:

set f1=foo
set f2=bar
diff $f1 $f2 > /dev/null                   
if ($status == 0) then
        echo 'no diff'
else
        echo 'diff'
endif
like image 73
codaddict Avatar answered Aug 22 '26 19:08

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!