mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2021-10-02, 16:20   #1
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

3·372 Posts
Default BASH script segment won't work in larger script

I have the following in a BASH script:
Code:
      echo "c4 is $c4 and am is $am"
      c4m=$(../../yafu/yafu "expr($c4*$am)" -silent)
      echo "c4 is $c4 and c4m is $c4m"
It doesn't work anywhere* in that 800+ line script:
Code:
c4 is -2293531595601876 and am is 3
c4 is -2293531595601876 and c4m is no variable indicator (@): interpreting batchfile lines as input expressions
eof; done processing batchfile
But, if I try the exact copy/pasted lines in a small test script (adding in the required values):
Code:
#!/bin/bash

c4=-2293531595601876
am=3
      echo "c4 is $c4 and am is $am"
      c4m=$(../../yafu/yafu "expr($c4*$am)" -silent)
      echo "c4 is $c4 and c4m is $c4m"
it works exactly as expected:
Code:
c4 is -2293531595601876 and am is 3
c4 is -2293531595601876 and c4m is -6880594786805628
I'm (pretty) sure this isn't a YAFU issue, so I'm posting here.

* I have tried these lines in two functions and in various locations in the main body of the larger script with no success. The scripts are located in the same directory.

All thoughts welcome. . .
EdH is offline   Reply With Quote
Old 2021-10-02, 19:01   #2
fivemack
(loop (#_fork))
 
fivemack's Avatar
 
Feb 2006
Cambridge, England

22·32·179 Posts
Default

Quote:
Originally Posted by EdH View Post
I have the following in a BASH script:
Code:
      echo "c4 is $c4 and am is $am"
      c4m=$(../../yafu/yafu "expr($c4*$am)" -silent)
      echo "c4 is $c4 and c4m is $c4m"
It doesn't work anywhere* in that 800+ line script:
Code:
c4 is -2293531595601876 and am is 3
c4 is -2293531595601876 and c4m is no variable indicator (@): interpreting batchfile lines as input expressions
eof; done processing batchfile
But, if I try the exact copy/pasted lines in a small test script (adding in the required values):
Code:
#!/bin/bash

c4=-2293531595601876
am=3
      echo "c4 is $c4 and am is $am"
      c4m=$(../../yafu/yafu "expr($c4*$am)" -silent)
      echo "c4 is $c4 and c4m is $c4m"
it works exactly as expected:
Code:
c4 is -2293531595601876 and am is 3
c4 is -2293531595601876 and c4m is -6880594786805628
I'm (pretty) sure this isn't a YAFU issue, so I'm posting here.

* I have tried these lines in two functions and in various locations in the main body of the larger script with no success. The scripts are located in the same directory.

All thoughts welcome. . .

What happens if you replace the '$(../../yafu/yafu' with '$(echo' ? That error message is coming from yafu not from bash (that is, yafu's output is that error message and has been captured into $c4m), so it looks as if the thing going into yafu has been somehow garbled ...

Last fiddled with by fivemack on 2021-10-02 at 19:02
fivemack is offline   Reply With Quote
Old 2021-10-02, 20:18   #3
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

100B16 Posts
Default

Quote:
Originally Posted by fivemack View Post
What happens if you replace the '$(../../yafu/yafu' with '$(echo' ? That error message is coming from yafu not from bash (that is, yafu's output is that error message and has been captured into $c4m), so it looks as if the thing going into yafu has been somehow garbled ...
I cannot detect a difference. Script that works:
Code:
c4 is 2346021463111956 and am is 3
c4 is 2346021463111956 and c4m is expr(2346021463111956*3) -silent
c4 is 2346021463111956 and c4m is 7038064389335868
and, script that doesn't work:
Code:
c4 is 2346021463111956 and am is 3
c4 is 2346021463111956 and c4m is expr(2346021463111956*3) -silent
c4 is 2346021463111956 and c4m is no variable indicator (@): interpreting batchfile lines as input expressions
eof; done processing batchfile
Code in both scripts:
Code:
am=3
      echo "c4 is $c4 and am is $am"
      c4m=$(echo "expr($c4*$am)" -silent)
      echo "c4 is $c4 and c4m is $c4m"
      c4m=$(../../yafu/yafu "expr($c4*$am)" -silent)
      echo "c4 is $c4 and c4m is $c4m"
I've also tried this with both YAFU 1 and YAFU 2 with identical results.

I fully agree with your assessment. That's why I didn't consider it a YAFU issue. But, I can't figure out what's wrong with the calling script. I'm thinking of using a separate script file to work around this, instead of embedding it in the main script.

Thanks!
EdH is offline   Reply With Quote
Old 2021-10-03, 12:20   #4
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

3·372 Posts
Default

I'm guessing it's my personal gremlin screwing with me. I wrote a separate script just to perform the task for my main program and it won't work, either. The only difference I can think of is the way the variables are assigned. In the working script, the variables are directly assigned their value. In the broken scripts, the value is acquired via parsing. The best I can come up ATM, is that there is some "invisible" artifact in the parsed values.
EdH is offline   Reply With Quote
Old 2021-10-03, 12:32   #5
M344587487
 
M344587487's Avatar
 
"Composite as Heck"
Oct 2017

2·3·5·29 Posts
Default

At first I thought you were using the expr utility and passing a number to yafu so noodled with that, but you're actually passing the string "expr(a*b)" to be evaluated by yafu (?) (sorry, unfamiliar with yafu). Either way you may need to delimit * so that it doesn't get substituted by the shell, and/or include spaces between the variables and the operator if yafu works like expr. Any difference in how you're calling the scripts that may let one substitute and the other not?
Code:
u20@u20:~/textdir$ jam="$(expr -2293531595601876 * 3)";echo $jam
expr: syntax error: unexpected argument ‘file_in_cd’

u20@u20:~/textdir$ jam="$(expr -2293531595601876 \* 3)";echo $jam
 -6880594786805628

u20@u20:~/textdir$ jam="$(expr -2293531595601876\*3)";echo $jam
-2293531595601876*3
M344587487 is offline   Reply With Quote
Old 2021-10-03, 15:28   #6
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

3·372 Posts
Default

I've tried lots of variations including \*, but mostly, they don't work with YAFU.

I'm still confused as to what the difference may be between a specific variable initialization and the variable coming from an operation. The two variables "appear" identical, but don't work the same. Oddly, I have used this in the past and it worked fine there. I need to see if I can find any of those scripts and see if I can learn anything from them.
EdH is offline   Reply With Quote
Old 2021-10-03, 16:06   #7
chris2be8
 
chris2be8's Avatar
 
Sep 2009

22·32·61 Posts
Default

What happens if you add set -x to the script? It should show you what the shell thinks the command it's executing is. Try it both in the small working script and the big one where it does not work. See the man page for bash for details (search for "SHELL BUILTIN COMMANDS" and scroll down).
chris2be8 is offline   Reply With Quote
Old 2021-10-03, 17:00   #8
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

3×372 Posts
Default

Quote:
Originally Posted by chris2be8 View Post
What happens if you add set -x to the script? It should show you what the shell thinks the command it's executing is. Try it both in the small working script and the big one where it does not work. See the man page for bash for details (search for "SHELL BUILTIN COMMANDS" and scroll down).
Thanks - I'll play with this and see what it shows.

For the time being, unless the above gets me some good info, I have come up with a solution in the following way:

I know the following:
Code:
"expr($test*$m)" | ../../yafu/yafu
will give the earlier complaint about a bathchfile, but it will also provide the answer within all the generated lines. The good part is that the answer is in the same element, if I parse the return via an array. I can harvest it from the array in a consistent manner.

I will probably run with this solution for now, unless I can easily find why the earlier script versions don't work and correct it.
EdH is offline   Reply With Quote
Old 2021-10-03, 18:07   #9
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

3·372 Posts
Default

Quote:
Originally Posted by chris2be8 View Post
What happens if you add set -x to the script? It should show you what the shell thinks the command it's executing is. Try it both in the small working script and the big one where it does not work. See the man page for bash for details (search for "SHELL BUILTIN COMMANDS" and scroll down).
Well, set -x showed that the exact same expression is sent to YAFU, but with different results:
Code:
+ ../../yafu/yafu 'expr(756756*3)' -silent
2270268
...
+ ../../yafu/yafu 'expr(756756*3)' -silent
no variable indicator (@): interpreting batchfile lines as input expressions
eof; done processing batchfile
EdH is offline   Reply With Quote
Old 2021-10-04, 15:48   #10
chris2be8
 
chris2be8's Avatar
 
Sep 2009

22×32×61 Posts
Default

My knowledge of shell scripting has nearly run out. But there must be *something* in the big script that is affecting yafu. Have you done anything with stdin or stdout in the big script? Try adding </dev/null to the yafu call. And look for anything else that could affect yafu. You could try cutting a copy of the big script down by deleting bits of it until yafu works (hopefully before it's the same as the small script!)

Sorry I've run out of ideas.
chris2be8 is offline   Reply With Quote
Old 2021-10-04, 16:25   #11
kruoli
 
kruoli's Avatar
 
"Oliver"
Sep 2017
Porta Westfalica, DE

22·191 Posts
Default

Have a look at line 855 of driver.c, it says if (isatty(fileno(stdin)) == 0) (in my current GIT checkout), and this is the only point where is_cmdline_run gets set to 2 and 2 is the only value which leads to yafu_obj.USEBATCHFILE being set to 2 (also the only occurence), the other possible value for yafu_obj.USEBATCHFILE would be 1, but we can assume that it is not 1, because you have not set the batchfile option. So we enter the if in line 230, which is the only place where prepare_batchfile gets called, and this is the only place where the message "no variable indicator (@) ..." gets printed.

If I am correct here, it will follow that isatty(fileno(stdin)) is something != 0 (likely 1) when executing directly for you and 0 when executing in your script. I checked that with my Debian WSL and could not reproduce that. Are you piping something into the script? Or are you accessing stdin at any place in the script?
kruoli is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Startup Script Gerard Information & Answers 2 2018-11-01 08:40
Simple Script to get Trial Factoring Work jfamestad PrimeNet 3 2016-11-06 20:32
bash script for extracting primenet-status gLauss Linux 0 2010-07-31 11:19
Could you help me change this into a bash script? jasong Programming 1 2007-11-29 05:59
Help with Startup Script Proggie Software 1 2004-12-19 11:54

All times are UTC. The time now is 03:42.


Mon Nov 29 03:42:25 UTC 2021 up 128 days, 22:11, 0 users, load averages: 1.65, 1.47, 1.36

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.