Im writing a script to be run through qsub in a sge cluster.
I have several executables that I need to call in the script and all of them are in the same location (the same as the script itself). So Im wondering How can I get the location of the script Im executing. I have an automatic build system and it is just not right to hard code this location. The code should be something like
#!/bin/bash  
#$ -S /bin/bash  
#$ -V  
#$ -cwd
#$ -N "Job"  
exec="/path/to/this/script/"execNameJob  
$exec
I tryed a couple of things and they didn't work and people marked as duplicate. None of the previous solutions worked. Im running my script using qsub -o console.out /tmp/qsubScript/qsubScript.sh. What I want to get is the /tmp/qsubScript/ path in the script
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
echo " Directory SOURCE: $DIR"
echo " BaseDir 0: " $0
echo " SGE_ROOT: $SGE_ROOT"
echo " SGE_JOB_SPOOL_DIR: $SGE_JOB_SPOOL_DIR"
the output is:
Directory SOURCE: /opt/gridengine/default/spool/compute-2-8/job_scripts
 BaseDir 0:  /opt/gridengine/default/spool/compute-2-8/job_scripts/9534
 SGE_ROOT: /opt/gridengine
 SGE_JOB_SPOOL_DIR: /opt/gridengine/default/spool/compute-2-8/active_jobs/9534.1  
The main problem is that the sge implementation copy my script into a job_script, so I loose the reference to the original script.
 
    