perl: Command works in terminal, but not in shell script

Hi, the following command works in the terminal no problem.

samtools view -h rawlib.bam | perl -ne '{ @line = split( /\s+/ ); $match = 0; while( $line[5] =~ /(\d+)M/g ) { $match = $match + $1 } if( $match >= 80 || $_ =~ /^\@/ ) { print $_ } }' | java -Xmx12G -jar /opt/picard/picard-tools-current/SortSam.jar I=/dev/stdin O=rawlib.gt80.bam VERBOSITY=WARNING QUIET=TRUE SO=coordinate; samtools index rawlib.gt80.bam

But when I plop it in a shell script and change the file names to given execution variables......

run "samtools view -h ${ANALYSIS_DIR}/${BARCODE_ID}_rawlib.bam | perl -ne '{ @line = split( /\s+/ ); $match = 0; while( $line[5] =~ /(\d+)M/g ) { $match = $match + $1 } if( $match >= 80 || $_ =~ /^\@/ ) { print $_ } }' | java -Xmx12G -jar /opt/picard/picard-tools-current/SortSam.jar I=/dev/stdin O=${ANALYSIS_DIR}/${BARCODE_ID}_rawlib.bam VERBOSITY=WARNING QUIET=TRUE SO=coordinate";samtools index ${ANALYSIS_DIR}/${BARCODE_ID}_rawlib.bam

I get an error.

running: samtools view -h /results/analysis/output/Home/user_LIS-27-06mar2013-40p12rpt-swp_4-12.2_207/AIB13-NA17281_rawlib.bam | perl -ne '{ @line = split( /\s+/ );  = 0; while( runplugin/runlevel	""[5] =~ /(\d+)M/g ) {  =  + -j } if(  >= 80 || /results/analysis/output/Home/user_LIS-27-06mar2013-40p12rpt-swp_4-12.2_207/AIB13-NA17281_oldrawlib.bam =~ /^\@/ ) { print /results/analysis/output/Home/user_LIS-27-06mar2013-40p12rpt-swp_4-12.2_207/AIB13-NA17281_oldrawlib.bam } }' | java -Xmx12G -jar /opt/picard/picard-tools-current/SortSam.jar I=/dev/stdin O=/results/analysis/output/Home/user_LIS-27-06mar2013-40p12rpt-swp_4-12.2_207/AIB13-NA17281_rawlib.bam VERBOSITY=WARNING QUIET=TRUE SO=coordinate
String found where operator expected at -e line 1, near "runlevel """
	(Do you need to predeclare runlevel?)
Bareword found where operator expected at -e line 1, near "06mar2013"
	(Missing operator before mar2013?)
Bareword found where operator expected at -e line 1, near "40p12rpt"
	(Missing operator before p12rpt?)
Bareword found where operator expected at -e line 1, near "06mar2013"
	(Missing operator before mar2013?)
Bareword found where operator expected at -e line 1, near "40p12rpt"
	(Missing operator before p12rpt?)
syntax error at -e line 1, near "; ="
syntax error at -e line 1, near "runlevel """
syntax error at -e line 1, near "( >="
syntax error at -e line 1, near "} }"
Execution of -e aborted due to compilation errors.

It looks as though the error starts occurring at

 $match = 0;

.
I've attached my code. Probably a syntax error?

The difference I see is the 'run' command. You are launching it with something else, perhaps not a shell. I do not know what exactly, but am curious.

I would put it in a shell script, then do run "./shell_script"

'run' with "./shellscript" -- no success :confused:

I ask again what this 'run' thing is.

I also ask, in what way "run ./script" didn't work.

Work with us, please. :slight_smile:

Sorry, I will try to be a little more descriptive.

I'm using a shell script template provide by life technologies; meaning the 'run' function is not of my creation. From what I can tell, it's purpose is to print a given command and then evaluate it.

'run ./script" didn't work.' - it returned the same error as previous.

There's a small error in your perl code. A semi-colon is missing after the while block

while ( condition ) { blah }; if ($match > ......

Thanks balajesuri, but even with the fix, I still got the same error.

What I found interesting was the command echoed from the 'run' statement. They aren't identical (aside from the file names).

script command

samtools view -h rawlib.bam | perl -ne '{ @line = split( /\s+/ ); $match = 0; while( $line[5] =~ /(\d+)M/g ) { $match= $match + $1 } if( $match >= 80 || $_ =~ /^\@/ ) { print $_ } }' | java -Xmx12G -jar /opt/picard/picard-tools-current/SortSam.jar I=/dev/stdin O=rawlib.gt80.bam VERBOSITY=WARNING QUIET=TRUE SO=coordinate; samtools index rawlib.gt80.bam

echoed command

samtools view -h /results/analysis/output/Home/user_LIS-27-06mar2013-40p12rpt-swp_4-12.2_207/AIB13-NA17281_rawlib.bam | perl -ne '{ @line = split( /\s+/ );  = 0; while( runplugin/runlevel	""[5] =~ /(\d+)M/g ) {  =  + -j }if(  >= 80 || /results/analysis/output/Home/user_LIS-27-06mar2013-40p12rpt-swp_4-12.2_207/AIB13-NA17281_oldrawlib.bam =~ /^\@/ ) { print /results/analysis/output/Home/user_LIS-27-06mar2013-40p12rpt-swp_4-12.2_207/AIB13-NA17281_oldrawlib.bam } }' | java -Xmx12G -jar /opt/picard/picard-tools-current/SortSam.jar I=/dev/stdin O=/results/analysis/output/Home/user_LIS-27-06mar2013-40p12rpt-swp_4-12.2_207/AIB13-NA17281_rawlib.bam VERBOSITY=WARNING QUIET=TRUE SO=coordinate

It looks like the variable $match isn't being created? I'm just trying to shed some light on what might be the problem....

@jdilts: Could you please post the output of samtools view -h filename and what you intend to do with the output. May be there is a simpler way to arrive at the solution.

The output is a really, really large txt file. (I've attached a really small example)
That line of code was written by another bioinformaticist who is more familiar with the file types than I am. He gave it to me to run in the terminal (which worked), but when I tried to use it in a bash script, it hasn't prevailed. I'm currently getting support from him. In conclusion, I don't know I lot of what that code does. I put it out here on the forum to see if it was a quick syntax fix, since I'm not real familiar with bash. Thanks for all your help!

Edit: I mean I know what it produces, but I don't know exactly how it produces.