Alex A
Alex A

Reputation: 1

Bash unable to assign variable

#!/usr/bin/env bash
#SBATCH --partition=standard
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=20
#SBATCH --mem=100G
USEAGE="metascript.sh <wd> <wd1>"
source ~/anaconda2/etc/profile.d/conda.sh
conda activate assembly
wd=$1
wd1=$2
cd $wd
cd $wd1 
for f in SRR*/ ; do
  [[ -e $f ]] || continue
  SRR=${f::-1}
  cd ../..
  jdid=$(sbatch -J FirstQC_$SRR ./pipelines/preprocessingbowtietrinity/FirstFastqc.sh $wd $wd1 $SRR)
  #echo ${jdid[0]}|grep -o '[0-9]\+'
  jobid=${jdid[0]}
  jobid1=${jobid[0]}|grep -o '[0-9]\+'
  #echo $jobid1

Hi all just having issues with my bash scripting, so I can print the line ${jdid[0]}|grep -o '[0-9]+' however when I assign it to a variable it is unable to return anything.

Upvotes: 0

Views: 75

Answers (3)

damienfrancois
damienfrancois

Reputation: 59072

If the idea is to extract just the job ID from the output of sbatch, you can also use sbatch's --parsable argument. See here in the documentation.

jdid=$(sbatch --parsable -J FirstQC_$SRR ./pipelines/preprocessingbowtietrinity/FirstFastqc.sh $wd $wd1 $SRR)

and jdij will only contain the job ID if the cluster is not part of a federation.

Upvotes: 1

CrazyTux
CrazyTux

Reputation: 222

  1. If the issue is printing the line ${jdid[0]}|grep -o '[0-9]+' as your question.
  2. Just put the line in double quotation marks and it will work out.
  3. Here is a little test i made:
jobid1="{jobid[0]}|grep -o '[0-9]\+'"
echo $jobid1 
  • the out put is {jobid[0]}|grep -o '[0-9]\+'

Upvotes: 0

Armali
Armali

Reputation: 19375

  jobid1=${jobid[0]}|grep -o '[0-9]\+'

I can print the line ${jdid[0]}|grep -o '[0-9]+' however when I assign it to a variable it is unable to return anything.

In order to assign the output of a pipeline to a variable or insert it into a command line for any other purpose, you have Command Substitution at hand:

  jobid1=`echo ${jobid[0]}|grep -o '[0-9]\+'`

Of course, with bash this is better written as:

  jobid1=`<<<${jobid[0]} grep -o '[0-9]\+'`

Upvotes: 0

Related Questions