• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ksh script - (Not Java related) - Child script name

 
man pat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parent.ksh (Parent script):
#!/usr/bin/ksh
. /export/home/mp8595/test/child.ksh # calls child script

-------------------------------------------------
child.ksh
Question: How in child script i can find child script's name.
When I try any of the following things
SCRIPT_NAME=`basename ${0}`
SCRIPT_NAME=`echo ${0}`
it stores parent.ksh in SCRIPT_NAME rather than child.ksh

I hope I can post such questions here otherwise I apologize.
Thanks!

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "." command executes the contents of child.sh in the parent.sh process, so nothing process-oriented is going to help you. It turns out, though, that ksh sets a parameter to the name of the file that contains the current command. It's the empty string for interactive commands. The parameter is ".sh.file" . So, for example,



will be "child.sh" if the above line is, indeed, in child.sh.



See here for this and many other cool bits.
 
man pat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest:

I tried as per your suggestion and it says...

SCRIPT_NAME=${.sh.file}: bad substitution

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you're feeding this to ksh and not something else (bash?) What does

echo $SHELL

give you?
 
man pat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest:

Please refer below screen shot:
FYI - parent script:test.ksh - it calls child script on line 14

/export/home/mp8595/test>./test.ksh
Test.ksh - scriptName:test.ksh:
./test.ksh[14]: SCRIPT_NAME=${.sh.file}: bad substitution
/export/home/mp8595/test>echo $SHELL
/bin/ksh

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is different from your original example. There's a big difference between "./test.ksh" and ". test.ksh". The first one starts the script in a new process, and that new process will be "/bin/sh -c test.ksh" by default, unless test.ksh started with "#!/bin/ksh". The second one executes the contents of "test.ksh" in the current shell, so the commands would definitely be executed by ksh.

So use ". test.ksh", and you really should find that the command succeeds as advertised.
 
man pat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am including both files:
File1: /export/home/mp8595/test/parent.ksh
#!/usr/bin/ksh
# -------------------------------------------------------------------------------
SCRIPT_NAME=`basename ${0}`
#SCRIPT_NAME=`echo ${0}`
echo "Test.ksh - scriptName:${SCRIPT_NAME}:"

# -------------------------------------------------------------------------------
. /export/home/mp8595/test/child.ksh
# -------------------------------------------------------------------------------

echo "Exiting parent.ksh"
exit 0

# END - SCRIPT -------------------------------------------------------------------

File2: /export/home/mp8595/test/child.ksh
#!/usr/bin/ksh
# -------------------------------------------------------------------------------
DATE=`date +%Y%m%d`

SCRIPT_NAME=${.sh.file}
#SCRIPT_NAME=`basename ${0}`
#SCRIPT_NAME=`echo ${0}`

echo "Test.ksh - scriptName:${SCRIPT_NAME}:"

echo "Exiting child.ksh"

# END - SCRIPT -------------------------------------------------------------------

To get it clear please list steps to follow to see expected results.

Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put these two scripts in files names as you suggest, changed /usr/bin/ksh to /bin/ksh (necessary on my system), removed the hard-coded path of the child script, and "chmod +x *.ksh".

Then I ran the parent with "./parent.ksh", and got

 
man pat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea this is what I am getting:
/export/home/mp8595/test>./parent.ksh
Test.ksh - scriptName:parent.ksh:
./parent.ksh[8]: SCRIPT_NAME=${.sh.file}: bad substitution

Thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic