shell - How to validate a file format -


i wrote shell script. got error message line 14 , 18, not see wrong. script supposed analyze each file of *.fq. if first 3 characters of given file not match "hwi" script reply "invalide format", if match script give me number of "hwi" lines in file.

!/bin/sh f in *.fq a="$(head -n1 $f)" c="$(expr substr $a 2 3)" echo $c nr="$(grep -w "hwi" $f | wc -l) if [ "$c"="hwi" ] ;   echo $f |  "the number of read of $f $nr" else   echo"invalide format" fi done 

can explain errors are?

there several syntax issues in script, can use tool http://www.shellcheck.net/ detect mistakes. after indenting code, here more correct (not perfect) version of wrote:

for f in *.fq       a=$(head -n1 "$f")     c=$(expr substr "$a" 2 3)     echo "$c"     nr=$(grep -w "hwi" "$f" | wc -l)     if [ "$c" == "hwi" ];         echo "the number of read of $f $nr"     else          echo "invalide format"     fi done 
  • no need quotes " " around $(..)
  • no need pipe $f echo (not sure trying do)
  • put quotes " " wrapping variables avoid word splitting

i did not run code since don't know want do, @ least syntax should better.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -