Ksh: How to get name of script when in same script


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Ksh: How to get name of script when in same script
# 1  
Ksh: How to get name of script when in same script

To get the script name the variable $0 can normally be used.
Problem happens when the command is started with a "dot space" as in: . myscript.sh.
$0 shows -ksh not myscript.sh
From within the script ps -f does not show myscript.sh either.
Someone suggested using readlink but cannot get that to work

Here is a test script:
Code:
#myscript.sh
echo myscript=$0
mypid=$$
echo mypid=$mypid
ps -f
ls /proc/$mypid/exe
readlink /proc/$mypid/exe

Code:
uname -a
Linux svappd02 2.6.32-754.11.1.el6.x86_64 #1 SMP Tue Jan 22 17:25:23 EST 2019 x86_64 x86_64 x86_64 GNU/Linux

Thanks!

Last edited by Don Cragun; 05-22-2019 at 06:42 PM.. Reason: Add CODE and ICODE tags.
# 2  
Quote:
Originally Posted by Michael Fitz
To get the script name the variable $0 can normally be used.
Problem happens when the command is started with a "dot space" as in: . myscript.sh.
$0 shows -ksh not myscript.sh
From within the script ps -f does not show myscript.sh either.
Someone suggested using readlink but cannot get that to work

Here is a test script:
Code:
#myscript.sh
echo myscript=$0
mypid=$$
echo mypid=$mypid
ps -f
ls /proc/$mypid/exe
readlink /proc/$mypid/exe

Code:
uname -a
Linux svappd02 2.6.32-754.11.1.el6.x86_64 #1 SMP Tue Jan 22 17:25:23 EST 2019 x86_64 x86_64 x86_64 GNU/Linux

Thanks!
The . command asks the shell to include the text found in the file named by its operand in the body of the currently running script and execute in the current shell execution environment without starting another shell or sub-shell to process the commands found in that file.

If it is important for the file you are running with the . command to accurately specify the name of the file from which . read those commands, the script calling it will need to cooperate with it something like:
Updated calling calling myscript.sh:
Code:
... ... ...
dotscript="my script.sh"
. "$dotscript"
... ... ...

Updated code in myscript.sh:
Code:
#myscript.sh
mypid=$$
echo "The code in $dotscript is being run by $0 with PID $mypid"

# 3  
Ran new "myscript.sh" still shows "-ksh" not "myscript.sh"
Code:
$ . myscript.sh
The code in  is being run by -ksh with PID 1581

# 4  
Quote:
Originally Posted by Michael Fitz
Ran new "myscript.sh" still shows "-ksh" not "myscript.sh"
Code:
$ . myscript.sh
The code in  is being run by -ksh with PID 1581

No. It is being run by -ksh as I explained in post #2. What it shows (by the two spaces between "in" and "is" in the output you showed us) is that you didn't set dotscript="myscript.sh" before running the command . myscript.sh as I suggested in post #2.
# 5  
Don, I guess a better question is how to derive the script name when it's "sourced" WITHOUT hardwiring its name in the script itself.
I've been dealing with the fullpath to the script name using this paradigm in ksh:
Code:
#!/usr/bin/ksh
#set -x

thisFile="$(whence ${0})"
echo "this file->[${thisFile}]"

Obviously it doesn't work when the script is sourced.

I tried this for testing:
Code:
#!/usr/bin/ksh
set -x

echo "\$0 ->[${0}] \$# -> [${#}]"
#thisFile="$(whence ${0})"
if [[ "${0}" = -* ]]; then
   # cannot get this to work when sourced....
   thisFile="${1}"
else
   # bash-ism
   #thisFile="$(type -P ${0})"
   # ksh-ism
   thisFile="$(whence ${0})"
fi

echo "this file->[${thisFile}]"

# 6  
how about this approach [covering bash/ksh - might need to enhance it for other (zsh/sh/etc)]
Code:
#!/bin/ksh
#set -x

#echo "\$0 ->[${0}] \$# -> [${#}] \$_ -> [${_}] "
case "${0}" in
   -bash) thisFile="${BASH_ARGV[0]}"
          ;;
   *ksh) thisFile="${_}"
          ;;
   *)
      #thisFile="$(type -P ${0})"
      thisFile="$(whence ${0})"
      ;;
esac

echo "this file->[${thisFile}]"

This User Gave Thanks to vgersh99 For This Post:
# 7  
Only bash provides a method:
Code:
echo "I am $BASH_SOURCE"

With ksh you have to stick to a workaround like Don's suggestion
Code:
dotscript="myscript.sh"; . "$dotscript"

Code:
echo "I am ${dotscript:-$0}"

This User Gave Thanks to MadeInGermany For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #835
Difficulty: Medium
SOAPjr is a protocol specification for exchanging structured information in the implementation of web services in computer networks.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to replace lines in ksh Script

Hi All, I am novice to Unix and I need your expert advice for the below task. There is a KSH script file in which I need to replace few line as per the below expectations. So my file look like as # Host Setup Command: Line 1 Line 2 Line 3 Line 4 Line Any... (6 Replies)
Discussion started by: rupid0609
6 Replies

2. Shell Programming and Scripting

Help Create dynamic ksh script from a script

I am currently running 2 scripts to gather data for a 3rd script and would like to combine the 2 scripts into one. Having issues with the final output format. Note cannot post URL so replaced the http stuff with (name) in the examples All scripts contain #!/bin/ksh OS = Red Hat Enterprise... (0 Replies)
Discussion started by: pcpinkerton
0 Replies

3. Shell Programming and Scripting

Capturing stderr in ksh script within a script

I have seen lots of entries on stderr and stdout, but I did not see a solution to my question. In running a script, I call another script. I want to capture output and error messages from the called script. I am able to redirect the stdout from the called script to my output file, but I don't... (2 Replies)
Discussion started by: timtoben
2 Replies

4. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

5. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies

6. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

7. Shell Programming and Scripting

execute a ksh script from perl script!

Hi, I have used exec ("/bin/ksh -c /path/file.ksh arg1"); to execute the file.ksh script from a test.pl script. But it doesnt work.. can anyone tell me what exactly the systax should be?... i have tried system("/path/file.ksh arg1"); too....still no luck... quick replies are highly appreciated (1 Reply)
Discussion started by: meghana
1 Replies

8. Shell Programming and Scripting

tracing a ksh script within a ksh script

I normally trace a script with the ksh -x <script name> and redirect strderr to file. But if you have a script like the examble below...... vi hairy bear=`grep bear animals` if then ksh more_animals fi If I ksh -x hairy it won't trace "more_animals" unless I put a -x in it. Is... (1 Reply)
Discussion started by: shorty
1 Replies

9. Shell Programming and Scripting

how to convert unix .ksh script to windows .batch script

I am using awk in my .ksh script but when I am trying to run in windows its not recognising awk part of the ksh script , even when I changed it to gawk it does not work, this is how my .ksh and .bat files look like. thanx. #!/bin/ksh egrep -v "Rpt 038|PM$|Parameters:|Begin |Date: |End... (1 Reply)
Discussion started by: 2.5lt V8
1 Replies

10. Shell Programming and Scripting

executing a ksh script from another ksh script

Hi, I'm new to unix scripting.How can i call a script from another script. I have a.ksh and b.ksh .I have to call b.ksh from a.ksh after it is successfully exceuted. I tried using #!/bin/ksh -x in a.ksh and at the end i have used /path/b.ksh My problem is it is executing only a.ksh.it... (6 Replies)
Discussion started by: ammu
6 Replies

Featured Tech Videos