aspose file tools*
The moose likes Linux / UNIX and the fly likes ksh script - (Not Java related) - Child script name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "ksh script - (Not Java related) - Child script name" Watch "ksh script - (Not Java related) - Child script name" New topic
Author

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

man pat
Greenhorn

Joined: Aug 05, 2011
Posts: 12
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
man pat
Greenhorn

Joined: Aug 05, 2011
Posts: 12
Hi Ernest:

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

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

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Are you sure you're feeding this to ksh and not something else (bash?) What does

echo $SHELL

give you?
man pat
Greenhorn

Joined: Aug 05, 2011
Posts: 12
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Aug 05, 2011
Posts: 12
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Aug 05, 2011
Posts: 12
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ksh script - (Not Java related) - Child script name