mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2021-10-05, 02:19   #34
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

22·13·79 Posts
Default

Quote:
Originally Posted by paulunderwood View Post
Code:
#!/bin/bash

a="31363920 948583258055568 -21094061301960794201597 -101781123694367174730692456309 1859252514529139843616145918243322920 1347307311985305327578340438707291179185984 951738172269489471300643 -167704995415343890580296180142353038"
echo "$a" > testcase
#exec < "testcase"
read b < testcase 
c="31363920"
d="${a:0:8}"
e="${b:0:8}"
m="3"

echo "m is $m and d is $d and c is $c and b is ${b:0:10}... and a is ${a:0:10}..."
printf "\nYAFU says $c*$m is "
../yafu/yafu "expr($c*$m)" -silent
printf "\nYAFU says $d*$m is "
../yafu/yafu "expr($d*$m)" -silent
printf "\nYAFU says $e*$m is "
../yafu/yafu "expr($e*$m)" -silent
results in:

Code:
m is 3 and d is 31363920 and c is 31363920 and b is 31363920 9... and a is 31363920 9...

YAFU says 31363920*3 is 94091760

YAFU says 31363920*3 is 94091760

YAFU says 31363920*3 is 94091760
Idea lifted from https://linuxhint.com/read_file_line_by_line_bash/

(I compiled msieve for YAFU without zlib.)
Excellent! But, I'm still a bit confused as to how to read subsequent lines from testcase. In my original version, I'm trying to use:
Code:
exec <"testcase"
  while read line
    do
      larray=($line)
      ../../yafu/yafu with some elements of the array
    done
How do I acquire subsequent lines using "read b < testcase?"


Thanks.
EdH is offline   Reply With Quote
Old 2021-10-05, 02:24   #35
paulunderwood
 
paulunderwood's Avatar
 
Sep 2002
Database er0rr

2·7·281 Posts
Default

Quote:
Originally Posted by EdH View Post
Excellent! But, I'm still a bit confused as to how to read subsequent lines from testcase. In my original version, I'm trying to use:
Code:
exec <"testcase"
while read line
    do
      larray=($line)
      ../../yafu/yafu with some elements of the array
    done
How do I acquire subsequent lines using "read b < testcase?"


Thanks.
Just move the input redirection to after done like this:

Code:
while read line
    do
      larray=($line)
      ../../yafu/yafu with some elements of the array
    done < testcase

Last fiddled with by paulunderwood on 2021-10-05 at 02:25
paulunderwood is offline   Reply With Quote
Old 2021-10-05, 02:43   #36
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

22×13×79 Posts
Default

Code:
#!/bin/bash

a="31363920 948583258055568 -21094061301960794201597 -101781123694367174730692456309"
echo "$a" > testcase
echo "22$a" >> testcase
c="31363920"
m="3"

printf "\nYAFU says $c*$m is "
../../yafu/yafu "expr($c*$m)" -silent

while read line
  do
    larray=($line)
    d=${larray[0]}
    echo "d is $d"
    printf "YAFU says $d*$m is "
    ../../yafu/yafu "expr($d*$m)" -silent
  done < testcase
gives me this:
Code:
YAFU says 31363920*3 is 94091760
d is 31363920
YAFU says 31363920*3 is no variable indicator (@): interpreting batchfile lines as input expressions
-101778913424507528108228602338
eof; done processing batchfile
I'm going to have to set this aside for the night and hit it fresh tomorrow. Sorry I'm not getting this, but thanks for trying to help me out.

I have also tried reading a complete file, but YAFU still seems to act the same. If I can make sure the file is closed after, maybe I can use a two-dimension array and read the entire file into the program and work from there. But, it seems that the flag kruoli pointed out isn't reset once it's opened.

Last fiddled with by EdH on 2021-10-05 at 02:44 Reason: Remove #$@! formatting corruption in code block!
EdH is offline   Reply With Quote
Old 2021-10-05, 08:00   #37
paulunderwood
 
paulunderwood's Avatar
 
Sep 2002
Database er0rr

2·7·281 Posts
Default

Code:
a="31363920 948583258055568 -21094061301960794201597 -101781123694367174730692456309"
echo "$a" > testcase
echo "22$a" >> testcase
c="31363920"
m="3"

printf "\nYAFU says $c*$m is "
../../yafu/yafu "expr($c*$m)" -silent

readarray matrix < testcase
#file now closed

for row in "${matrix[@]}";do                                                      
    row_matrix=(${row})                                                            
    d=${row_matrix[0]}                                                         
    echo "d is $d"
    printf "YAFU says $d*$m is "
    ../../yafu/yafu "expr($d*$m)" -silent
done
This reads entire file and picks d as the first of each row.

Code:
YAFU says 31363920*3 is 94091760
d is 31363920
YAFU says 31363920*3 is 94091760
d is 2231363920
YAFU says 2231363920*3 is 6694091760

Last fiddled with by paulunderwood on 2021-10-05 at 08:02
paulunderwood is offline   Reply With Quote
Old 2021-10-05, 14:25   #38
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

22×13×79 Posts
Default

I've successfully coded a stand alone script that works well. I have not attempted to incorporate the code into a function in the main script. Perhaps a little later.

Thanks for all the help.
EdH is offline   Reply With Quote
Old 2021-10-06, 17:58   #39
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

22·13·79 Posts
Default

Well, unfortunately, in practice, it just doesn't work!

From the command line, the stand alone script works as it should. But, as a function within the bigger script, or as a call to the working script from within the bigger script, YAFU fails to understand. I'm guessing it's just due to the flag being set by something in the bigger script.

My work around is fine for now, so I'm not going to spend any more time with it until "maybe" later.


Thanks everyone.
EdH is offline   Reply With Quote
Old 2021-10-15, 18:48   #40
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

100C16 Posts
Default

Just a final note to my "adventure," since there is a conclusion:

First, YAFU was never "portable" enough for my need. I found that even changing to a parallel directory caused it to output differently. Since I intend the script I'm working on to be available to others, I had to give up on YAFU. To be fair, YAFU was not the correct choice from the start for such a task.

The good news is that Pari-gp is readily available and much easier to implement for my script. The new code is simply:
Code:
ans=$(echo "$d*$m" | gp -q -f)
It returns, in $ans, the clean value of the multiplication of the two input values, works everywhere tested and is not affected by open files.

Thanks for all the help from everyone.
EdH is offline   Reply With Quote
Old 2021-10-15, 19:39   #41
paulunderwood
 
paulunderwood's Avatar
 
Sep 2002
Database er0rr

75368 Posts
Default

Quote:
Originally Posted by EdH View Post
The good news is that Pari-gp is readily available and much easier to implement for my script. The new code is simply:
Code:
ans=$(echo "$d*$m" | gp -q -f)
It returns, in $ans, the clean value of the multiplication of the two input values, works everywhere tested and is not affected by open files.
For lengthy numbers you might find this is better as there is no truncation:

Code:
ans=$(echo "print($d*$m)" | gp -q -f)
paulunderwood is offline   Reply With Quote
Old 2021-10-15, 22:07   #42
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

100C16 Posts
Default

Quote:
Originally Posted by paulunderwood View Post
For lengthy numbers you might find this is better as there is no truncation:

Code:
ans=$(echo "print($d*$m)" | gp -q -f)
Thanks! That is important.
EdH 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 04:56.


Fri Dec 3 04:56:04 UTC 2021 up 132 days, 23:25, 0 users, load averages: 1.60, 1.26, 1.30

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.