0
#!/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.

2
  • Are you expecting to feed ${jobid[0] to grep and capture the output in jobid1? That's jobid1=$( echo "${jobid[0]}" | grep -o '[0-9]\+' ). Commented Jun 22, 2021 at 20:23
  • jdid is a string. There's no benefit in treating it like an array. Commented Jun 22, 2021 at 20:38

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
  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]\+'`

3 Comments

With bash, you don't need grep at all. [[ ${jobid[0]} =~ ([0-9]+) ]]; jobid1=${BASH_REMATCH[1]}.
With any shell (written after about 1996), this is better written jobid=$( ... ). Backticks are dead.
You seem to confuse old-style with dead; the documentation does not confirm your death certificate.
0
  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]\+'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.