0

I want to pass directory as a command line argument to the shell script.

#!/bin/bash 

sourcedir=/home/dir/data

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=/home/dir/data

 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

This array script for SGE cluster is script.sh and I am running it using

 bash script.sh

However I want to pass the directory which is /home/dir/data from the command line such as below (instead of using it twice in the script)

 bash script.sh /home/dir/data

I am not able to do that.

9
  • You should find the answer in the duplicate. I just want to point out a couple of things: i) your fastq_list=$sourcedir/list1 echo ... line will give you a syntax error, is that really what you want? ii) don't use foo=$( ls *_R1.fastq.gz), use basefiles=( *_R1.fastq.gz ) instead. This will give you an array directly. iii) as a general rule, avoid using ALLCAPS variable names in shell scripts since by convention, environment variables are in caps. iv) It looks like you might be interested in our sister site: [bionformatics.se]. – terdon Mar 12 '19 at 17:47
  • I am not sure if its duplicate.My question is different than the one mentioned.I tried the answers but had issue.Will $1 be passed to the shell script or the run_array_job.sh script which is inside it? I need to pass it to both of them. – Ron Mar 12 '19 at 18:03
  • Just change sourcedir=/home/dir/data to sourcedir="$1" and launch your script with bash script.sh /home/dir/data. – terdon Mar 12 '19 at 18:03
  • It worked and its accessible in the outer script.How can I also pass it to the array script? – Ron Mar 12 '19 at 18:09
  • I don't know since your script isn't very clear. Since this is a different issue, please ask a new question but clarify exactly what needs to be passed to what. – terdon Mar 12 '19 at 18:11