2

I am in trouble reading a string from a file into another script as a string. Here is my detail:

I have 2 script file: one is 'step0.sh' and the other is 'step1.lsf'

'step0.sh' file contains only two lines

DIR="/sc/arion/work/kimm61/vepSIMUL/clinvar"
IN="clinvar.vcf"

and 'step1.lsf' contains those lines:


#!/bin/bash
#BSUB -J step1_1
#BSUB -P acc_Itan_lab
#BSUB -q private
#BSUB -n 41
#BSUB -R rusage[mem=20000]
#BSUB -R span[hosts=1]
#BSUB -W 120:00
#BSUB -o echo $DIR/step1out/%J.stdout
#BSUB -eo echo $DIR/step1out/%J.stderr
#BSUB -L /bin/bash


cd /sc/arion/work/kimm61/vepSIMUL/clinvar/
mkdir step1out


source activate phen
ml bcftools/1.10.2
module load vep/100

source /sc/arion/work/kimm61/vepSIMUL/clinvar/step0.sh
directory=echo "$DIR"
input=echo "$IN"
WRKDIR=echo "$DIR"/step1out
ref_fasta=/sc/arion/projects/Itan_lab/vep_data/fasta_ref/Homo_sapiens.GRCh38.dna.primary_assembly.fa.gz

myinput=echo "$DIR"/"$IN"
myoutput=vep1_1.vcf

(What I want)

  1. for the directory from 'step1.lsf', I would like to read 'DIR' from 'step0.sh'
  2. for the input from 'step1.lsf', I would like to read 'IN' from 'step0.sh'
  3. for the standard output(-o) and the standard err output(-eo), I would like to read'DIR' from 'step0.sh'
Minju Kim
  • 21
  • 4
  • 2
    Since the lines of `step1.lsf` appear to be syntactically correct POSIX variable assignments, you could simply source that file into you script. See for example [exporting environment variable using .sh file](https://askubuntu.com/questions/917340/exporting-environment-variable-using-sh-file) – steeldriver Jul 13 '22 at 19:46
  • Which distro and version of Linux are you using? – user68186 Jul 13 '22 at 20:08
  • What's not working? You're already sourcing the file as far as I can see? Also, please include the scripts as text and not as images. Thanks. – Artur Meinild Jul 14 '22 at 07:45
  • Please edit your question and include the code you are showing in the images as code-formatted text. Then delete the images. – vanadium Jul 14 '22 at 10:02

1 Answers1

2

The proper and most simple way is to source the settings file in the beginning of the script. Then use the variables like as they were defined in the script. For Dash/Sh you must use the command ., for Bash you can use either source or ..

cat step1.lsf
#!/bin/bash

. step0.sh

echo "$DIR"
echo "$IN"

If for some reason this is not that you want, you could parse the values of the variables from the settings file, capture the output by a command substitution $() and assign them to variables within the script. For example.

cat step1.lsf
#!/bin/bash

DIR="$(sed -nr 's/^DIR="(.*)"$/\1/p' step0.sh)"
IN="$(sed -nr 's/^IN="(.*)"$/\1/p' step0.sh)"

echo "$DIR"
echo "$IN"

Actually the problem in your script are the assignments like this:

directory=echo "$DIR"

Here you need to use command substitution to make it work.

directory="$(echo "$DIR")"

But in this case (where you are not processing the value in some special way) you can just assign the value of the variable $DIR to the other variable.

directory="$DIR"
pa4080
  • 29,351
  • 10
  • 85
  • 161