1

I have a bash script that I want to run in cluster. But I run into following errors:

/var/spool/slurmd/job24250582/slurm_script: line 5: i_hard: command not found

/var/spool/slurmd/job24250582/slurm_script: line 6: i_soft: command not found

/var/spool/slurmd/job24250582/slurm_script: line 7: i_neutral: command not found

What I want to do is read all the files in the directory. And then depending upon whether that file has word 'hard','soft',or 'neutral', I want to run specific commands on the cluster.

#!/bin/bash

#generating training data

i_hard = 0
i_soft = 0
i_neutral = 0

for entry in /Users/documents/popGen/sample/slim_script/*
do
    if [[ $entry == *"hard"* ]]; then
        echo "It's there!"
        /home/popGen/build/./slim $entry > /home/popGen/sweeps/msOut/hard_$i.msOut.gz
        i_hard = $((i_hard+1))
    fi

    if [[ $entry == *"soft"* ]]; then
        echo "It's there!"
        /home/popGen/build/./slim $entry > /home/popGen/sweeps/msOut/soft_$i.msOut.gz
        i_soft = $((i_soft+1))
    fi
    if [[ $entry == *"neutral"* ]]; then
       echo "It's there!"
       /home/popGen/build/./slim $entry > /home/popGen/sweeps/msOut/neutral_$i.msOut.gz
       i_neutral= $((i_neutral+1))
   fi

done

This code should generate files but I get the error mentioned above. Can someone explain why I am getting this error?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Peter
  • 97
  • 1
  • 9

2 Answers2

2

In bash, variable assignment does not allow spaces between the name and the value. i_hard = 0 is interpreted as the command i_hard, called with two arguments: = and 0. Use the following instead:

i_hard=0
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
1

As far as I know, there should be no spaces around the assignment operator, like this:

variable=6
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • I didn't know that. Thank you. Should I remove spaces in $entry == "neutral" in the if statement for example? – Peter Jun 15 '19 at 07:01
  • @Peter No, those spaces are required. The shell syntax rules vary quite a bit between different contexts, and inside `[[ ]]` or `[ ]`, you need spaces between each "word" in the test expression. – Gordon Davisson Jun 15 '19 at 08:07