why won't if [ ! cmp -s A B ] work?
I think in bash,
this works. Code:
if ! cmp -s A B; this won't work Code:
if [ ! cmp -s A B ]; then |
The second example has the word "then" twice.
Ed |
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. |
Sorry,
the script content is Code:
if [ ! cmp -s A B ]; Quote:
|
The square bracket interprets its argument as an expression. The script should not put square brackets around a command.
Do "man bash" and search for "test expr". Ed |
Couple of examples. If this is what you are looking for.
Code:
echo "Mary had a little lamb" > A |
Thank you folks!
I thought cmp returns True or False but it gives a chunck of text if there is a difference. |
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:
Quote:
Regarding Code:
cmp A B | grep text Quote:
|
cmp -s doesn't print anything, it only returns an exit code:
Quote:
|
The problem is essentially solved.
A bit of a long story. I looked up internet and found this works Code:
if cmp -s A B; then Code:
It says Code:
./simple: line 3: syntax error near unexpected token `else' If Fortran, this works: Code:
if (a == b) then Code:
if( strcmp("a","b") == 0 ) { The trouble starts when I tried to take "the negative" of if cmp -s A B; then A general question. Is everybody forced to take a negative just to avoid an empty statement? |
i think I have to learn the proper use of $?
as in |
Quote:
Code:
cmp -s A B || echo diff Code:
if cmp -s A B Code:
if ! cmp -s A B |
Code:
if( strcmp("a","b") == 0 ) { 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 Code:
Test 1 |
Quote:
It is still valid for your c example too (where you omit the then, but cannot omit the {}). |
All times are GMT -5. The time now is 01:17 AM. |