Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Always post the actual commands being entered and exact error messages. (i.e. copy and paste both of them.)
"[" is an alias for "test", which deals with expressions.
"if" does not deal with expressions, it deals with commands.
"!" happens to be valid syntax both before test expressions and before a command.
"cmp" is a command/executable, not an expression.
Couple of examples. If this is what you are looking for.
Code:
echo "Mary had a little lamb" > A
echo "Mary had a little" > B
cmp A B
A B differ: byte 18, line 1
#######
if cmp A B | grep "differ"; then
echo "They are different"
else
echo "They are the same"
fi
A B differ: byte 18, line 1
They are different
if cmp A A | grep "differ"; then
echo "They are different"
else
echo "They are the same"
fi
They are the same
########
if [[ "$(cmp A B)" =~ "differ" ]]; then
echo "They are different"
else
echo "They are the same"
fi
They are different
if [[ "$(cmp A A)" =~ "differ" ]]; then
echo "They are different"
else
echo "They are the same"
fi
They are the same
#######
[[ "$(cmp A B)" == *"differ"* ]] && echo "Yes" || echo "No"
Yes
[[ "$(cmp A A)" == *"differ"* ]] && echo "Yes" || echo "No"
No
The general approach is:
if <commandA>; then <commandB> else <commandC> fi
<commandA> can be anything, if will only examine its exit code. So test, [, cmp or anything which can return an exit code should work.
! will just reverse the condition, exchange the than/else parts.
Quote:
Originally Posted by centguy
I think in bash,
this works.
Code:
if ! cmp -s A B;
then
echo A and B are different
fi
This one should work if A and B are two files. Would be nice to know what was your problem with it.
Quote:
Originally Posted by centguy
Strangely enough
this won't work
Code:
if [ ! cmp -s A B ]; then
then
echo A and B are different
fi
it says too many arguments.
This is just wrong. The command [ (or test) has a well defined syntax and in general it accepts 2 or 3 arguments (excluding !). 4 arguments is just useless. The test command does not operate on exit codes, but expressions, therefore you can use the text produced by commands - that is the $(command) syntax.
Regarding
Code:
cmp A B | grep text
This will work the same way, the line will return an exit code (in this case produced by grep) and actually we don't really care about the exit code of cmp (which would be good enough). Additionally (in general) we must not rely on the text printed by cmp, that may cause strange things, but the exit code of cmp.
Quote:
Originally Posted by centguy
I thought cmp returns True or False but it gives a chunck of text if there is a difference.
That is [almost] true. cmp returns with an exit code, 0 means ok (or true) - files are the same, anything else means not ok (False) - files are different. if can handle it perfectly. You do not need to analyze the report of cmp to know the answer. see man page of cmp.
if( strcmp("a","b") == 0 ) {
} else
{
printf("a is not equal to b\n");
}
With respect to c or FORTRAN bash does have some unusual syntax rules. However, being forced or silly to use a negative condition when both c and FORTAN have A!=B constructs to then use if-then-else seems just sloppy coding. However, what ever you are used to and what is more readable to you is probably more important.
shruggy has already posted the examples to make bash look similar to the code you are used to using.
Appreciate shruggy and michaelk and teckk comments:
I summarize the discussion so that those who wants to play with it can simply cut and paste:
Script:
Code:
echo "Mary had a little lamb" > A
echo "Mary had not a little lamb" > B
echo "Test 1"
if cmp -s A A
then :
else
echo diff
fi
echo "Test 2"
if cmp -s A B
then :
else
echo diff
fi
echo "Test 3"
cmp -s A A && echo same || echo diff
echo "Test 4"
cmp -s A B && echo same || echo diff
./simple: line 3: syntax error near unexpected token `else'
./simple: line 3: `else
which is rather stupid.
A general question. Is everybody forced to take a negative just to avoid an empty statement?
No, it is not that stupid. You have to put something between the two keywords then and else.
It is still valid for your c example too (where you omit the then, but cannot omit the {}).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.