Function to get the current script name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function to get the current script name
# 1  
Old 12-05-2019
Function to get the current script name

I have a function to get the executing scripts name.
Code:
Script_nm() {
Parent=$( ps -f -p $PPID -o and | tail -l | sed -e 's/ *[bak]\{1,\}sh *//' | awk '{print $1}' )
If [ $parent = '-bash' ] ; then
Script_nm='bash_test.sh'
Else
Script_nm=$( basename $parent )
Fi
Echo "$script_nm"
Return 0
}

We are calling this function in another script which is used to load the values in the database. This function is not getting the name of the scripts properly. Is there any other logic that can be used for this function ? We have multiple scripts and using the same function to collect the scripts name when they are getting executed. Does adding script_nm=`basename $0` ? work if I add this in all the scripts ?

Last edited by vbe; 12-05-2019 at 02:07 PM..
# 2  
Old 12-05-2019
Would the $BASHPID help? Like
Code:
ps hocomm p$BASHPID

# 3  
Old 12-06-2019
this topic has been covered in a couple of threads - including this one.
Also look into the 10 More Discussions You Might Find Interesting section at the bottom of this thread.
This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 01-21-2020
Since one can't reply to the other thread - closed - I'm curious why none suggested:
Code:
script_name=${0##*/}

It does work in bash for sure.
Not so sure about KSH.
# 5  
Old 01-21-2020
Quote:
Originally Posted by sea
Since one cant reply to the other thread ....
Because this kind of question has been asked and answered many times on this site.

That is why no one answered.

It is a forum rule to search the forums before asking questions (like other forums and site) and not ask the same question, over and over again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

2. Shell Programming and Scripting

Finish current script and execute next script

Hi, I've come accross a situation where I need to exit from current shell script at the same time I need to start/activate another shell script. How can I do that in KSH ?? Need help !! For example, my script is as below #!/bin/ksh paramFile="/home/someXfile.lst" ] && <<Here I... (1 Reply)
Discussion started by: R0H0N
1 Replies

3. Shell Programming and Scripting

sed script- current and next line

Hi I have been using sed -n -e '/regex/{N;p;}' to print the matching line and the next line. In cases where I have a matching line at the end of the document with no next line, that line is skipped. How do I print that line as well? Thanks. (10 Replies)
Discussion started by: jamie_123
10 Replies

4. Shell Programming and Scripting

Get current and parent script name

Hi, i have a script a.sh that calls b.sh Both take parameters a.sh { b.sh p1 p2 p3 } b.sh { GIVEN_CMD="`basename $0` $@" echo "${GIVEN_CMD} } Now when i run: a.sh q1 q2 It prints only a.sh q1 q2 Inside b.sh, how can i print both the script names and their parameters passed? (6 Replies)
Discussion started by: ysrini
6 Replies

5. UNIX for Dummies Questions & Answers

Determine FULL name of current script

Hi everyone, Is there a slick way to determine the FULL name of a script that is running? The variable ${0} just gives the relative path name. I guess I could just do the following: FULL_SCRIPT_NAME=${PWD}${0}Although that's pretty simple is there another way that I am missing? ... (4 Replies)
Discussion started by: msb65
4 Replies

6. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

7. Shell Programming and Scripting

Ksh script function, how to "EXIT 2" without killing the current process?

Hi, Using AIX 5.3 and Ksh. />ls -al /usr/bin/ksh -r-xr-xr-x 5 bin bin 237420 Apr 10 2007 /usr/bin/ksh /> I recently started working for a new employer. I have written UNIX K-Shell scripts for many years and have never had this particular issue before. Its perplexing me. I have... (2 Replies)
Discussion started by: troym72
2 Replies

8. Shell Programming and Scripting

Trigger Shell Script from Current Script

Hello all, I'm new to shell programming and need some help. I would like to set up a step within a shell script to trigger another shell script to run, based on the highest return code generated in the current script. For example, if the highes return code value in the current script is less... (1 Reply)
Discussion started by: mmignot
1 Replies

9. Shell Programming and Scripting

How do i execute script in the current shell

How do i run a shell script or perl script with in the context of a current shell. I know that i can use command source. but we can't pass any arguments to our script if we use source command as it takes only one argement i.e filename Is there any way to run a script in the current shell... (5 Replies)
Discussion started by: Naresh Kumar
5 Replies

10. Shell Programming and Scripting

addtion to the current script !!

How can I add to the current oraenv.sh so that is an oracle_sid is passed then it won't prompt for the vaid sid and just take it. If it is not passed then do what it is doing right now ?? if # Command executed from a terminal then ORACLE_SID="" ... (1 Reply)
Discussion started by: uuser
1 Replies
Login or Register to Ask a Question