0

I have the following bash script:

#!/bin/bash
$db=DB
$H=6973
$cov=38
for i in $(find . -type f -name "*.*.las");
do
  cat <<EOF
  #qsub <<EOF
#!/bin/bash -l

#PBS -N DASqv
#PBS -l walltime=48:00:00
#PBS -j oe
#PBS -l mem=30G
#PBS -l ncpus=1
#PBS -M [email protected]
##PBS -m bea

cd \$PBS_O_WORKDIR

source activate thegenemyers

DAStrim=$(DASqv -v -H$H -c$cov $db $i | grep Recommend - | sed "s|Recommend ||g" - | sed "s|'||g" -)

DAStrim $db $i 

EOF

done

Unfortunately, I got this error:

sh Dascrubber_pbs.sh

DASqv: -H '' argument is not an integer
  #qsub <<EOF
#!/bin/bash -l

#PBS -N DASqv
#PBS -l walltime=48:00:00
#PBS -j oe
#PBS -l mem=30G
#PBS -l ncpus=1
#PBS -M [email protected]
##PBS -m bea

cd $PBS_O_WORKDIR

source activate thegenemyers

DAStrim=

DAStrim  ./DB.309.las

I would have expected to see e.g. DAStrim=$(DASqv -v -H6973 -c38 DB ./DB.82.las | grep Recommend - | sed "s|Recommend ||g" - | sed "s|'||g" -).

What did I miss?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
user977828
  • 921
  • 4
  • 16
  • 30
  • 5
    `$H=6973` isn't an assignment in bash (and related shells): use `H=6973` – steeldriver Jan 30 '18 at 00:41
  • As well as what @steeldriver said about variable assignments, you can also improve your use of `sed`. there's no need for `grep | sed | sed` here - one sed command can do it all `DAStrim=$(DASqv -v -H$H -c$cov $db $i | sed -n -e "/Recommend / { s/Recommend //; s/'//g ; p }"`. More importantly, see [https://unix.stackexchange.com/questions/321697/why-is-looping-over-finds-output-bad-practice](https://unix.stackexchange.com/questions/321697/why-is-looping-over-finds-output-bad-practice) – cas Jan 30 '18 at 01:05
  • In addition, when you are running the script as `sh Dascrubber_pbs.sh`, you are not necessarily executing it as a bash script. What language is assumed here, depends on how sh is defined (where it is pointing to). – user1934428 Jan 30 '18 at 05:57

1 Answers1

2

There are two issues:

  1. As steeldriver pointed out, assignments in shells (other than C shell derivatives) look like
    variable=value
    not
    $variable=value
  2. If a here-document’s delimiter string is not quoted, the contents of the here-document are interpreted (expanded).  So you shouldn’t be expecting to see DAStrim=$(DASqv -v -H6973 ...), you should be expecting to see DAStrim=whatever_the_output_of_DASqv_is.  If, for some reason, you wanted to see the command, you should quote the EOF, or any of its characters (so you could use 'EOF', "EOF", \EOF, or assorted other variations), or escape the $.  For example,
    $ WORKDIR=/tmp
     
    $ i=foobar.las
     
    $ cat << EOF
    cd \$WORKDIR
    ls -l "$i"
    today=$(date)
    EOF
    cd $WORKDIR                             (output)
    ls -l "foobar.las"
    today=Tue, Jan 30, 2018  1:27:42 AM
     
    $ cat << 'EOF'
    cd \$WORKDIR                            (same input as above)
    ls -l "$i"
    today=$(date)
    EOF
    cd \$WORKDIR                            (output)
    ls -l "$i"
    today=$(date)
     
    $