1

I got the following error

awk: cmd. line:1: {if(NR%4==2) print length(.)}
awk: cmd. line:1:                           ^ syntax error

While running sh s.sh .

#!/bin/bash
#usage: sh afterqc_pbs.sh /work/waterhouse_team/All_RawData/Banana_Each_Cell_Raw/Banana_Illumina/QUT_WT_GN366_4 

for r1 in $(find $1 -name "*R1*.gz");
do
  base=${r1%_R1*}
  echo $base
  output=$(basename $(echo $r1 | sed 's/R1//g'))
  r2=$(echo $r1 | sed 's/R1/R2/g')
  echo $r1
  echo $r2 


  #cat <<EOF
  qsub <<EOF
#!/bin/bash -l

#PBS -N $output
#PBS -l walltime=48:00:00
#PBS -j oe
#PBS -l mem=50G
#PBS -l ncpus=1
#PBS -M m.lorenc@qut.edu.au
##PBS -m bea

cd \$PBS_O_WORKDIR

echo "Average\n"
zcat $r1 | awk '{if(NR%4==2) {count++; bases += length} } END{print bases/count}'

echo "Distribution\n"
zcat $r1 | awk '{if(NR%4==2) print length($1)}' | sort -n | uniq -c


EOF

done

How is it possible that awk does not picks up . rather than the length?

Thank you in advance.

Will Bickford
  • 5,381
  • 2
  • 30
  • 45
user977828
  • 7,259
  • 16
  • 66
  • 117
  • As a start, try writing your heredoc output to a file instead of immediately submitting it to `qsub`. Just reviewing the generated source may help. Likely issue is quoting after `$1` is substituted. – Will Bickford Sep 06 '18 at 01:53
  • You might want to paste your script at http://shellcheck.net/ and fix the errors it reports. A complete refactoring would be an improvement, but that's a start. – tripleee Sep 06 '18 at 04:15

1 Answers1

2

The issue is your use of qsub <<EOF. When you use << (called a "here document" or "heredoc"), any variables inside get expanded. The single quotes around them don't count, as the heredoc takes priority. If you want to change this, change <<EOF to <<"EOF". https://www.gnu.org/software/bash/manual/bashref.html#Here-Documents has more details.