Bash troubleshooting: Not a valid identifier

12,586

Solution 1

That must be so:

for i in `</home/regionstextfile`
do 
  tabix /sequences/human_variation/snps/genotypes.vcf.gz $i | vcftools --window-pi 10000 >> /home/Testgenomesdata/genomesregions.txt
done

When you use a variable (e.g. assign value to it or export it, or do anything but with the variable itself) you write its name without $; when you use a value of a variable you write $.

EDIT:

When region names contains spaces but each region is in a separate line, you need while:

cat /home/regionstextfile | while read i
do 
  tabix /sequences/human_variation/snps/genotypes.vcf.gz "$i" | vcftools --window-pi 10000 >> /home/Testgenomesdata/genomesregions.txt
done

Solution 2

The same thing without cat :

while read i
do
  tabix /sequences/human_variation/snps/genotypes.vcf.gz "$i" | vcftools --window-pi 10000 >> /home/Testgenomesdata/genomesregions.txt
done < /home/regionstextfile

Remark <file.txt could not work unless IFS=''

OLDIFS="$IFS"
IFS=''
for i in `</home/regionstextfile`
do
  tabix /sequences/human_variation/snps/genotypes.vcf.gz $i | vcftools --window-pi 10000 >> /home/Testgenomesdata/genomesregions.txt
done
IFS="$OLDIFS"
Share:
12,586
user964689
Author by

user964689

Updated on June 05, 2022

Comments

  • user964689
    user964689 almost 2 years

    Beginner here trying to get a pipeline working in bash. If somebody can see why when I run the following I get:

    -bash: `$i': not a valid identifier,
    

    that would be really helpful. Also if there are other mistakes please let me know

    for $i in /home/regionstextfile; do tabix /sequences/human_variation/snps/genotypes.vcf.gz $i | vcftools --window-pi 10000 >> /home/Testgenomesdata/genomesregions.txt; done
    

    The idea is for each line in regionstextfile (which contains genome coordinates) run a program called tabix in the vcf.bz file, then with the output run vcftools with the specified options, then put all the outputs into the genomesregions.txt file.

  • jedwards
    jedwards almost 12 years
    As long as the lines in the textfile doesn't contain whitespace, this should work.
  • user964689
    user964689 almost 12 years
    Thanks for the fast reply, testing it now