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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl: Command works in terminal, but not in shell script
# 1  
Old 05-02-2013
perl: Command works in terminal, but not in shell script

Hi, the following command works in the terminal no problem.
Code:
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......
Code:
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.
Code:
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
Code:
 $match = 0;

.
I've attached my code. Probably a syntax error?
# 2  
Old 05-02-2013
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"
# 3  
Old 05-02-2013
'run' with "./shellscript" -- no success

'run' with "./shellscript" -- no success :/
# 4  
Old 05-02-2013
I ask again what this 'run' thing is.

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

Work with us, please. Smilie
# 5  
Old 05-02-2013
the 'run' function

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.
# 6  
Old 05-02-2013
Quote:
Originally Posted by jdilts
Code:
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"

There's a small error in your perl code. A semi-colon is missing after the while block
Code:
while ( condition ) { blah }; if ($match > ......

# 7  
Old 05-02-2013
colon after while block ... still produces the same error

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
Code:
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
Code:
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....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string works on command-line but fails when run from shell script

I wish to replace "\\n" with a single white space. The below does the job on command-line: $ echo '/fin/app/scripts\\n/fin/app/01/sql' | sed -e 's#\\\\n# #g'; /fin/app/scripts /fin/app/01/sql However, when i have the same code to a shell script it is not able to get me the same output:... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

Need to understand how the line in perl program works as shell commend

I have a file with two line, one is header, the other actual value: TYPCD|ETID2|ETID|LEG ID|PTYP|PTYP SUB|TRD STATUS|CXL REASON|CACT|CACTNM|ENCD|ENC D NM|TRDR|ASDT|TRDT|MTDT|STDT|LS|SECID|SECID TYP|SECNM|PAR|STCC|MARKET PRICE|DIS MARKET PRICE|MARKET PRICE CURRENCY|SRC OF SETTLEMENT... (2 Replies)
Discussion started by: digioleg54
2 Replies

3. Shell Programming and Scripting

Perl error in batch command but works one at a time

In the below perl executes if one file is processed perfect. However, when multiple files are processed in batch which is preferred I get the below error that I can not seem to fix it as the '' necessary for the command to execute, but seem to only work for one -arg option. Thank you :). ... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. UNIX for Dummies Questions & Answers

Shell script not working but command works in command prompt

Hi everyone I have a problem with my script If I try directly this command /usr/bin/nice -n 19 mysqldump -u root --password="******" wiki_schneider -c | nice -n 19 gzip -9 > /point_de_montage/$(date '+%Y%m%d')-wiki-db.sql.gz It works But if I simply add this command in a script and... (8 Replies)
Discussion started by: picemma
8 Replies

5. Shell Programming and Scripting

SED command works in terminal, but not Applescript

The following command works perfectly in Terminal, but not in Applescript. (Returns "unknown token" error for square brackets.) (new to site. sorry.) I have an Applescript that is designed to find and remove any square-bracketed text, including the square brackets. I ran the following code from... (1 Reply)
Discussion started by: Phillip Acosta
1 Replies

6. Shell Programming and Scripting

help with shell script: cp command not working, but mv command works...

Hello. I would like to ask your help regarding the cp command. We are using a cp command to create a back-up copy of our file but to no avail. It's just not working. We already checked the file and directory permissions and all seems correct. We have a script (ftp.script) which calls on... (1 Reply)
Discussion started by: udelalv
1 Replies

7. Shell Programming and Scripting

how the typeset command works in shell script

typeset -l section section=${2:-.} what does these 2 lines meaning? (1 Reply)
Discussion started by: venkatababu
1 Replies

8. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

9. Shell Programming and Scripting

#!/bin/bash and #1bin/sh command not found error on mac osx terminal/shell script

i am having a weird error on mac os x running some shell scripts. i am a complete newbie at this and this question concerns 2 scripts. one of which a friend of mine wrote (videochecker.sh) a couple weeks ago and it's been running fine on another machine. then last week i wrote capture.sh and it... (2 Replies)
Discussion started by: danpaluska
2 Replies

10. Shell Programming and Scripting

perl - how come this script works?

#!/usr/bin/perl open (DATA, file.txt); @array = <DATA>; close (DATA); open (DATA, ">$file.txt"); for (@array) { s/text/replace text/; push(@contents,$_); } seek(DATA, 0, 0); print DATA (@contents); close(DATA); could someone please explain how this works. i've been... (3 Replies)
Discussion started by: mjays
3 Replies
Login or Register to Ask a Question