This is an extension of the question I asked . Passing directory from command line to shell script
I have a script which writes another script using a heredoc. I need to be able to write unexpanded variables in the heredoc, so I use single quotes ('EOF'
). However, I need one variable to be expanded. Given the script below, how can I write the value of $sourcedir
inside the heredoc?
#!/bin/bash
sourcedir="$1"
cd $sourcedir
find "$PWD" -maxdepth 2 -name \*_R1*.fastq.gz > list1
fastq_list=$sourcedir/list1 echo `cat $fastq_list` num_files=$(wc -l <
$sourcedir/list1) echo $num_files
cat > run_array_job.sh<<'EOF'
#!/bin/bash -l
#$ -j y
#$ -cwd -S /bin/sh
#$ -l h_vmem=10G
#$ -pe smp 12
if [ -z "${SGE_TASK_ID}" ]; then echo "Need to set SGE_TASK_ID" exit 1 fi
BASEDIR=$sourcedir
echo "BASEDIR" echo $BASEDIR
BASEFILES=$( ls *_R1.fastq.gz)
BASEFILES_ARRAY=(${BASEFILES})
BASEFILE=${BASEFILES_ARRAY[(${SGE_TASK_ID} - 1)]}
echo $BASEFILE
...................
...................
EOF
qsub -t 1-${num_files} run_array_job.sh
I am running this script using
bash script.sh /home/dir/data
I am able to pass /home/dir/data as $1 to sourcedir but it also needs to be passed to BASEDIR , in array script which is submitted to cluster using qsub.
BASEDIR=$1
. You need to escape all$
's in your embedded script. If you don't get what I mean I can write an answer. – Weijun Zhou Mar 12 '19 at 19:05$
just to keep the value of one variable will be complicated. – terdon Mar 12 '19 at 19:08$sourcedir
in the array script. – terdon Mar 12 '19 at 19:08cat > wrapperscript.sh << EOF
, then putrun_array_job.sh $1
in the heredoc for wrapperscript.sh. – Weijun Zhou Mar 12 '19 at 19:10EOF
quoted so that you don't need to escape the$
s forrun_array_job.sh
, but leave out the quote when you write heredoc forwrapperscript.sh
– Weijun Zhou Mar 12 '19 at 19:12BASEDIR=$1
suggestion,it did not work – Ron Mar 12 '19 at 19:17<<'EOF'
, if you use<<EOF
it will work but you need to escape all$
's. This is also why terdon edited your question to clarify. – Weijun Zhou Mar 12 '19 at 19:18$'s
– Ron Mar 12 '19 at 19:20