Perl/Bash string execution

GWestphal

Golden Member
Jul 22, 2009
1,120
0
76
I'm having an issue with perl or maybe bash parsing out a command when I'm just trying to pass it as a string into another file.

system("echo \"for i in \`seq 0 99\`; do\" >> Neuron$Num.sh");
 

Aluvus

Platinum Member
Apr 27, 2006
2,913
1
0
1. It is very helpful to describe the actual output you are getting, rather than just saying you're having an issue. An issue could mean that the text has weird whitespace, or the output is in Arabic, or that your computer is on fire.

2. After Perl's parsing, the shell sees (I believe):

Code:
echo "for i in `seq 0 99`; do" >> Neuron{value of $Num to Perl}.sh

Based on my quick testing, I expect the shell is then executing your seq command in place. Evidently bash is executing the section in backticks because it is inside double quotes, in the same way that it would perform variable interpolation inside double quotes. After playing with it a little, I expect this will solve that problem (not tested):

Code:
system("echo 'for i in `seq 0 99`; do' >> Neuron$Num.sh");

Disclaimer: I am not a bash programmer, and in fact think shell scripting is awful.

3. Why not just do this with Perl and avoid the pain of executing a shell script inside a Perl script?

Code:
open OUTPUT, ">> Neuron$Num.sh";
print OUTPUT 'for i in `seq 0 99`; do';

(I'm assuming you have a good reason for writing a .sh file, and are adding a newline later.)
 

GWestphal

Golden Member
Jul 22, 2009
1,120
0
76
What I'm doing is using the shell script to create two files, Neuron$Num.sh and TTFNS$Num.hoc. These are simulations with different parameters that will be farmed out one to a core to a supercomputer cluster. I need the bash script to do looping of the program a certain number of times because you can only submit so many jobs to the queue, so if I loop in the bash it only counts as one job where as if I loop from perl it counts as 100 jobs. It's dumb in my opinion since the queue I'm using is currently unused, but that's how they have it set up.

So that code is being used to "build" the .sh file line by line with the appriopriate .hoc file specified inside it.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
Consider the Perl .. operator. If I run this:

perl -e 'print join(" ",1..100)'

Then I get a list of numbers from 1 to 100. That is what seq is producing, so why not just embed the string?

system("echo \"for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100; do\" >> Neuron$Num.sh");

Or more simply:

system("echo \"for i in ".join(" ",1..100)."; do\" >> Neuron$Num.sh");