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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
05-16-2021, 02:37 AM
|
#16
|
Member
Registered: Feb 2008
Posts: 637
Original Poster
Rep:
|
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 :
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.
|
|
|
05-16-2021, 04:43 AM
|
#17
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,037
|
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.
|
05-16-2021, 04:53 AM
|
#18
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,250
|
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.
|
|
|
05-16-2021, 08:49 PM
|
#19
|
Member
Registered: Feb 2008
Posts: 637
Original Poster
Rep:
|
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?
|
|
|
05-16-2021, 08:59 PM
|
#20
|
Member
Registered: Feb 2008
Posts: 637
Original Poster
Rep:
|
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!
|
|
|
05-16-2021, 09:34 PM
|
#21
|
Member
Registered: Feb 2008
Posts: 637
Original Poster
Rep:
|
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.
|
|
|
05-17-2021, 01:04 AM
|
#22
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,250
|
just a minor comment:
Quote:
Originally Posted by centguy
|
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.
|
|
|
05-17-2021, 03:59 AM
|
#23
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,037
|
Code:
while true
do
: # something
done
The : or true command can solve some portability issues.
works in all Posix-compliant shells, in contrast to the not yet standardized
Of course you can as well do
creates/empties "filename". While
in zsh works like
|
|
|
05-17-2021, 08:48 AM
|
#24
|
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,918
|
Quote:
Originally Posted by centguy
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
|
|
|
All times are GMT -5. The time now is 04:00 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|