mersenneforum.org  

Go Back   mersenneforum.org > Great Internet Mersenne Prime Search > Software

Reply
 
Thread Tools
Old 2009-03-25, 01:49   #12
Kosmaj
 
Kosmaj's Avatar
 
Nov 2003

E2616 Posts
Default

`foo` is the stdout of running function "foo". It's consumed by "if".
It's the same like
z=`foo` # nothing is being printed, z='aa'

To get the "aaUnequal" output, execute foo once more:
foo
if [ `foo` = 'aa' ]; then
...



Quote:
Originally Posted by CRGreathouse View Post
It's not actually relevant anymore -- I redesigned the script -- but here's a testcase.
Code:
foo () {
	echo -n 'aa'
}
if [ `foo` = 'aa' ]; then
	echo Equal
else
	echo Unequal
fi
exit 0
It displays "Equal" instead of "aaUnequal".
Kosmaj is offline   Reply With Quote
Old 2009-03-25, 02:50   #13
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

135338 Posts
Default

Quote:
Originally Posted by Kosmaj View Post
1) To catch the return value from a function, refer to $? immidiately after calling it.
Great, that simplifies a lot of things.

Quote:
Originally Posted by Kosmaj View Post
2) In bash you use single "=" to compare strings. That's why your code works. You can also use "==" but there is something tricky about it, it has different meaning in single brackets and in double brackets (will find you a reference later). It's the best to avoid it for the time being.
I know about single and double brackets (I use both, correctly, in my script). I'm not sure why you call out my use of = for string comparison: my task clearly calls for string comparison and I use the string comparison operator... is it that hard to believe I'd use it correctly?

Because I didn't know how to access the return value I was using the backtick hack (passing the whole comparison rather than the result of the comparison), but that won't be an issue anymore.

Quote:
Originally Posted by Kosmaj View Post
3) Binary valules in echo can confuse bash, that's why it's better to use "od" and do string comparisons. Even better, execute "file" and perform string comparisons on its output.
Honestly I still don't understand how to use od. And I'm not sure what you mean by string comparisons, since I'm doing string comparisons now.
CRGreathouse is offline   Reply With Quote
Old 2009-03-25, 07:46   #14
Kosmaj
 
Kosmaj's Avatar
 
Nov 2003

2·1,811 Posts
Default

(2) was directed to Alex in post #9, not to you.

(3) Try "od -aN4 a.png", it returns "P N G" with spaces. You can remove spaces with bash (or sed) and get "PNG". Ditto for jpg (look for JFIF), with "-aN10". You will have to select a suitable max number of bytes to find the magic number. But that's exactly what "file" does:
z=`file $input_file`

and check $z for all doc types you want to handle. Type "man file" for details. Magic numbers are kept in another file that you can modify if you want to add new file types. No need to rediscover the wheel. :-)

Last fiddled with by Kosmaj on 2009-03-25 at 07:50
Kosmaj is offline   Reply With Quote
Old 2009-03-26, 02:56   #15
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by Kosmaj View Post
(2) was directed to Alex in post #9, not to you.
Ah! No wonder I was confused. I pick up programming languages much faster than human subtleties (doubly so on forums where there's no intonation).

Quote:
Originally Posted by Kosmaj View Post
(3) Try "od -aN4 a.png", it returns "P N G" with spaces. You can remove spaces with bash (or sed) and get "PNG". Ditto for jpg (look for JFIF), with "-aN10". You will have to select a suitable max number of bytes to find the magic number.
Hmm, okay. I can see how that would be handy.

Quote:
Originally Posted by Kosmaj View Post
But that's exactly what "file" does:
z=`file $input_file`

and check $z for all doc types you want to handle. Type "man file" for details. Magic numbers are kept in another file that you can modify if you want to add new file types. No need to rediscover the wheel.
I found file easy enough to understand (sed and od still have me a bit confused). But actually my project is far overdesigned for what I actually need -- the main purpose was just to write something nontrivial as an exercise. I think I've basically done that, though I may play with my toy a bit more.

Any suggestions on where to go from here? Any other fun toys I should learn? I've been using regular expressions longer than linux, so don't say grep. But as you might imagine, most things are still mysterious to me. I have two decades of experience on DOS/Windows but only one month of experience on Ubuntu...


Edit: I don't think I told you that I appreciated your efforts to explain things to me. Thanks: it all makes sense now!

Last fiddled with by CRGreathouse on 2009-03-26 at 02:57
CRGreathouse is offline   Reply With Quote
Old 2009-03-26, 07:17   #16
jrk
 
jrk's Avatar
 
May 2008

3×5×73 Posts
Default

See if you can make a Mersenne prime-prover using only Bash built-in commands.
jrk is offline   Reply With Quote
Old 2009-03-26, 08:42   #17
akruppa
 
akruppa's Avatar
 
"Nancy"
Aug 2002
Alexandria

2,467 Posts
Default

Quote:
Originally Posted by Kosmaj View Post
2) In bash you use single "=" to compare strings. That's why your code works. You can also use "==" but there is something tricky about it, it has different meaning in single brackets and in double brackets (will find you a reference later). It's the best to avoid it for the time being.
Right... the man page lists "==" as the comparison operator with "=" a synonym for Posix compliance, but the "test" command understands only "=".

Note to self:
Quote:
an introductory text like Kosmaj posted would probably be worthwhile.
Alex
akruppa is offline   Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripts thread bsquared YAFU 4 2012-10-21 19:45
the scripts thread Mini-Geek Conjectures 'R Us 52 2012-05-29 21:43
aliquot escape firejuggler Aliquot Sequences 26 2012-01-19 08:15
High-altitude driver escape fivemack Aliquot Sequences 1 2011-04-24 09:34
DPGraph 2D/3D scripts nibble4bits Lounge 0 2008-01-16 17:05

All times are UTC. The time now is 09:34.


Tue Jul 27 09:34:39 UTC 2021 up 4 days, 4:03, 0 users, load averages: 1.70, 1.81, 1.81

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.