LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 05-16-2021, 02:37 AM   #16
centguy
Member
 
Registered: Feb 2008
Posts: 635

Original Poster
Blog Entries: 1

Rep: Reputation: 48

If I am hard pressed I might do this:

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
  echo > /dev/null
else
  echo diff
fi
 
echo Test 2
if cmp -s A B; then
  echo > /dev/null
else
  echo diff
fi

Output :

Code:
Test 1
Test 2
diff
I need something that produce nothing (yet respect bash if-then-else) and the best I can think of is
echo > /dev/null

echo will not be good enough since it will produce an empty line that upsets other script.
 
Old 05-16-2021, 04:43 AM   #17
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,851

Rep: Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224
There were better suggestions already.
Code:
if cmp -s A A; then
  :
else
  echo diff
fi
The nop command can even take arguments
Code:
  : no diff
  : echo "no diff"
In your initial post you had a negation of the exit status
Code:
if ! cmp -s A A; then
  echo diff
fi
Another possibility
Code:
cmp -s A A || {
  echo diff
}
 
1 members found this post helpful.
Old 05-16-2021, 04:53 AM   #18
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,144

Rep: Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375
if you wish you can use a variable:
Code:
if cmp -s A B; then
  VAR=ok
else
  VAR=diff
fi
and later you can do whatever you want based on that variable.
Do not attempt to echo anything and use (process) that later, that is just not reliable. Use exit code and variables.
 
Old 05-16-2021, 08:49 PM   #19
centguy
Member
 
Registered: Feb 2008
Posts: 635

Original Poster
Blog Entries: 1

Rep: Reputation: 48
My problem is with Post #12 by shruggy:

Code:
if cmp -s A B
then :
else echo diff
fi
Now MadeInGermany says:

Code:
if cmp -s A A; then
  :
else
  echo diff
fi
There is missing ; between the two.

For that I feel very uncomfortable.

Any official source to look it up?
 
Old 05-16-2021, 08:59 PM   #20
centguy
Member
 
Registered: Feb 2008
Posts: 635

Original Poster
Blog Entries: 1

Rep: Reputation: 48
Please ignore my previous post.
I think I am just not used to


if A
then

vs

if A; then


Because of that I did not understand

if A
then :

Now that MadeInGermany has typeset it in a way that I am most familiar, I now undertand the magic of

:

which is
do nothing.

Cheers!
 
Old 05-16-2021, 09:34 PM   #21
centguy
Member
 
Registered: Feb 2008
Posts: 635

Original Poster
Blog Entries: 1

Rep: Reputation: 48
Quote:
The nop command can even take arguments
points to the right direction.

A simple google search "bash nop" gives me the much needed info:

Personally, i could not agree more with this comment.

Quote:
Also, personally, I prefer to write code that is more "human friendly". It's often possible to restructure code to be more concise or even efficient. But when that's not critical, I prefer my code to be more readable for whichever developer comes along afterwards. (I also like commenting my code.) – osullic Jan 6 at 10:08
just because I have not learned : and I am forced to negate my logic that is silly. I will rather learn things properly.
Thanks for all the tips from all posts!

Last edited by centguy; 05-16-2021 at 09:40 PM.
 
Old 05-17-2021, 01:04 AM   #22
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,144

Rep: Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375Reputation: 7375
just a minor comment:
Quote:
Originally Posted by centguy View Post
A simple google search "bash nop" gives me the much needed info: https://stackoverflow.com/questions/...tional-in-bash
As it was described on that link: : is not a noop, but a valid command which has a regular exit code too (which is always 0 - true). Actually it accepts command line arguments and for example
Code:
: $(ls /tmp > /tmp/aaa)
will execute that ls command (so it is definitely not a noop).
true is a more human readable synonym of the same command. Additionally we also have a /bin/true binary executable.
true has a counterpart, the command false, which works exactly the same way, the only difference is that it returns always a non-zero exit code (=false).

we used to construct for example an endless loop using it:
Code:
while true;
do
    # something
done

Last edited by pan64; 05-17-2021 at 01:06 AM.
 
Old 05-17-2021, 03:59 AM   #23
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,851

Rep: Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224Reputation: 1224
Code:
while true
do
    : # something
done


The : or true command can solve some portability issues.
Code:
: $((i+=1))
works in all Posix-compliant shells, in contrast to the not yet standardized
Code:
((i+=1))
Of course you can as well do
Code:
i=$((i+1))
Code:
: > filename
creates/empties "filename". While
Code:
> filename
in zsh works like
Code:
cat > filename
 
Old 05-17-2021, 08:48 AM   #24
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,657

Rep: Reputation: 2582Reputation: 2582Reputation: 2582Reputation: 2582Reputation: 2582Reputation: 2582Reputation: 2582Reputation: 2582Reputation: 2582Reputation: 2582Reputation: 2582
Quote:
Originally Posted by centguy View Post
There is missing ; between the two.
There isn't a missing semi-colon - a command can be terminated by a semi-colon or a newline (or an ampersand, which runs it in async in background).

In the examples above, the need for a colon after "then" is because the then must be followed by a command, for example, this is a syntax error: "if something; then; something; fi"

However, replacing all three semi-colons with newlines is valid - the "then" must be followed by a command, but can be separated from it by whitespace (i.e. the newline isn't a terminator in that context).

I'm not sure how well I've explained that, and unfortunately there isn't really a good explanation in the Bash manual either, but this page is relevant: https://www.gnu.org/software/bash/manual/html_node/Lists.html

 
  


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
cmp or diff? which is more accurate? work for any file? fuzzylogic25 Linux - Newbie 1 06-09-2010 01:00 AM
/bin/sh: cmp: command not found - when compile 2.6.15 again . -=Graz=- Slackware 7 05-05-2006 08:39 PM
LXer: CMP Media's Annual Embedded Systems Conference Returns to Silicon Valley, April 3-7, 2006 at San Jose's McEnery Convention Center LXer Syndicated Linux News 0 12-22-2005 02:46 PM
Help me where to find 'cmp' sohard2reg Amigo 1 03-19-2005 02:54 AM
which should I use? diff cmp comm Frybyte Linux - Newbie 2 07-19-2004 08:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:09 AM.

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