简体   繁体   中英

Program runs fine from the terminal, but does not work from a shell script

I am on Linux Mint.

I have program called samtools, and it is stored in a folder on my desktop. I've added path to the executable into $PATH variable. In other words my local ~/.bashrc file has a line:

export PATH="~/Desktop/samtools/samtools-1.1:$PATH"

Executable file named samtools is in this folder.

So when I try to launch it from a command line like a simple command, by just typing "samtools" it works. It also works when I type direct path to the executable in the command line.

However when I try to launch it from a shell script, it does not launch and says either No such file or directory or not found.

Actually, I am trying to use another software which uses some shell scripts to preprocess some data. The error I am getting looks like this:

Indexing...
./RD_capture//process_one_capture.sh: 17: ./RD_capture//process_one_capture.sh: samtools: not found
 Sorting...
 ./RD_capture//process_one_capture.sh: 20: ./RD_capture//process_one_capture.sh: samtools: not found
 Piling up...
./RD_capture//process_one_capture.sh: 23: ./RD_capture//process_one_capture.sh: samtools: not found

Shell code looks like this:

echo "Indexing..."
samtools index $INPUTDIR/$sample.bam
#Then we sort them
echo "Sorting..."
samtools sort $INPUTDIR/$sample.bam $TMPDIR/$sample.sorted
#Finally we pile them up
echo "Piling up..."
samtools mpileup $TMPDIR/$sample.sorted.bam | cut -f 1-4 > $OUTPUT/$(basename $sample .bam).pile

Can anyone help me to solve this problem?

It looks like Linux is not able to find samtools .

To solve this use: Declare this in the beginning (change pwd with the path that leads to executable from pwd )

export set CURRENT_DIR=`pwd`

and while calling samtools use

$CURRENT_DIR/samtools

Note 1: you are responsible to tell the exact path to shell script as shell scripts execute from /usr/bin/sh . try echoing the path on the console with the command, if this does not help, as follows:

echo `pwd`

Note 2: the use of back quote above that is located on left hand side top corner of your keyboard.

Note 3: the export set is used to store global variables in a shell script so that commands that appear after this command can use this variable and update it when ever required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM