Thank you for the code. I think I have to use csh because I have to submit my job to high performance computing (HPC) server and using the old version of samtools which the source code give the path to it. I modified your code a little bit as attached below, but I got error "while: Expression Syntax". Any ideas? Thanks
#!/usr/bin/csh
cd /share/gwascotton/GBS_data/pileup_trial/
source /usr/local/apps/samtools/samtools017a.csh
ls *.bam > /share/gwascotton/GBS_data/pileup_trial/BAM.list ;
paste BAM.list | while read BAM ;
do
samtools pileup -c -l /share/gwascotton/GBS_data/merged_files.sorted.bam.pileup -f /share/gwascotton/GBS_data/Ghirsutum_458_v1.0. "${BAM}" > /share/gwascotton/GBS_data/new/"${BAM}".pileup
done;