linux - Change directory to path of parent/calling script in bash -


i have dozens of scripts, in different directories. (exported/expanded talend jobs)

at moment each job has 1 or 2 scripts, starting same lines, important one:

cd ***path-to-script*** 

and several lines set java path , start job.

i want create script, ran these scripts. e.g.:

/scripts/talend.sh 

and in talend scripts, first line run /scripts/talend.sh, examples of these scripts ran from:

/talend-job1_0.1/talend-job1_0.1/talend-job1/talend-job1.sh /talend-task2_0.1/talend-task2_0.1/talend-task2/talend-task2.sh /talend-job3_0.1/talend-job3_0.1/talend-job3/talend-job3.sh 

how can determine /scripts/talend.sh started from, can cd path within /scripts/talend.sh.

the talend scripts not run within directory itself, cronjob, or different users home directory.

edit: question marked duplicate, getting source directory of bash script within not answering question 100%. problem is: - basic script being called different scripts - different scripts can run command line, with, , or without symbolic link. - $0, $bash_source , pwd things, no solution mentioned covers difficulties.

example:

/scripts/talend.sh    

in script want configure $path , $home_path of java, , cd place talend job placed. (it's package, script must run location).

paths jobs are, example:

/u/talend/talendjob1/sub../../talendjob1.sh  /u/talend/talendjob2/sub../../talendjob2.sh /u/talend/talendjob3/sub../../talendjob3.sh 

multiple jobs run tms application. application cannot run these scripts whol name (to long, name can 6 long), in different location have symbolic links:

/u/tms/links/p00001 -> /u/talend/talendjob1/sub../../talendjob1.sh /u/tms/links/p00002 -> /u/talend/talendjob1/sub../../talendjob2.sh /u/tms/links/p00003 -> /u/talend/talendjob1/sub../../talendjob3.sh /u/tms/links/p00004 -> /u/talend/talendjob1/sub../../talendjob4.sh 

i think overview of complexity , why want 1 basic talend script, can leave basic stuff. can that, if know source of talend script, because there have to start talend job.

these answers (beyond first) specific linux, should robust there -- working directory names containing spaces, literal newlines, wildcard characters, etc.


to change own source directory (a faq covered elsewhere):

cd "$(basename "$bash_source")" 

to change parent process's current directory:

cd "/proc/$ppid/cwd" 

if want change directory passed first command-line argument parent process:

{ ifs= read -r -d '' _ && ifs= read -r -d '' argv1; } <"/proc/$ppid/cmdline" cd "$argv1" 

that said, personally, i'd export job directory environment variable in parent process, , read environment variable in children. much, simpler, more portable, more accurate, , compliant best process.


Comments

Popular posts from this blog

python 3 IndexError: list index out of range -

android - How to save instance state of selected radiobutton on menu -

IF statement in MySQL trigger -