How to get it's own file name within the same shell script? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to get it's own file name within the same shell script?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-23-2003
kjaisan kjaisan is offline
Registered User
 
Join Date: Sep 2003
Last Activity: 23 November 2005, 9:42 AM EST
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
How to get it's own file name within the same shell script?

Hello all,

This may be a silly question, but, let me ask... I searched the knowledge base, but couldn't get any help...

My script creates a log file. I need to name log file as whatever the script name with a ".log" extension.

Example: For the script name "start_cmd.sh", the log file should be named "start_cmd.sh.log" in the same script directory.

How do I get the script file name within the shell script?

Thanks.
Sponsored Links
    #2  
Old 10-23-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 20 April 2014, 9:11 PM EDT
Location: Ashburn, Virginia
Posts: 9,833
Thanks: 42
Thanked 378 Times in 225 Posts
It should be in $0. (that is dollar zero)
Sponsored Links
    #3  
Old 10-24-2003
kjaisan kjaisan is offline
Registered User
 
Join Date: Sep 2003
Last Activity: 23 November 2005, 9:42 AM EST
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the help Perderabo.
    #4  
Old 10-28-2003
Simerian Simerian is offline
Registered User
 
Join Date: Oct 2003
Last Activity: 3 November 2006, 8:19 AM EST
Location: United Kingdom
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Belt and braces...

To be sure that you only get the script name, try using:

basename $0

Like so:

LogFile="$(basename $0).log"

This solves the issue of calling the script with a path name attached to the front.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell or perl script needed for ldif file to text file conversion LinuxFriend Shell Programming and Scripting 1 04-23-2011 01:37 AM
Execute unix shell script to text file using the script mastercar Shell Programming and Scripting 4 05-21-2010 05:32 AM
how to read dbf file in shell script and to convert dbf file usinf shell script gauara Shell Programming and Scripting 6 05-12-2009 10:25 AM
(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script praka Shell Programming and Scripting 6 04-15-2009 06:09 AM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 04:25 AM



All times are GMT -4. The time now is 10:03 PM.