Shell Script function to use script name for log file output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script function to use script name for log file output
# 1  
Old 04-03-2016
Shell Script function to use script name for log file output

Hi Team -

I"m very new to Shell Scripting so I have a rather novice question. My forte is Windows Batch Scripting so I was just wondering what the Shell Script equivalent is to the DOS command %~n?

%~n is a DOS variable that dispayed the script name.

For instance (in DOS):
Code:
REM ------------------------------------------------------------------------
REM SET LOG File Path
REM ------------------------------------------------------------------------

SET intrapath=%MAINPATH%%LOGPATH%
SET errorintrapath=%MAINPATH%%ERRORPATH%

FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)

SET exportfile=%exportintrapath%%~n0.csv

SET logfile=%intrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log

Thanks!

---------- Post updated at 02:36 PM ---------- Previous update was at 02:08 PM ----------

Would this be the equivalent?

$(basename $BASH_SOURCE)

or $0

? Thanks!

Last edited by vbe; 04-03-2016 at 04:16 PM.. Reason: code tags
# 2  
Old 04-03-2016
Code:
$(basename $BASH_SOURCE)

This will do what you want however, it is limited to the bash shell. If this is not a problem for you, feel free to use this if you want.
Code:
$0

This will display the full file name of the current script being ran and this special variable will work in both ksh and bash. In order to get the script name on its own, you can use the basename command again or simply use parameter expansion like so:
Code:
"${0##*/}"

This User Gave Thanks to pilnet101 For This Post:
# 3  
Old 04-03-2016
THank you very much!

So, below is my original:

Code:
#!/bin/bash
#:: ------------------------------------------------------------------------
#:: -- SCRIPT NAME: fdmee_act_load.sh
#:: -- 
#:: -- DESCRIPTION: This script executes the FDMEE runbatch utility
#::                 To copy actuals to target intersections 
#:: -- 
#:: -- PARAMETERS:  Call setenv.cmd to get environment variables to determine 
#::                 login info, database, application, etc.
#:: --        
#::  Author:        
#::  Date:            04/02/16
#:: ------------------------------------------------------------------------
source /u01/hyp_app/scripts/setenv.sh

#:: SET LOG & ERROR FILES

_INTRA_PATH=$_MAIN_DIR/$_LOG_DIR/$_FDMEE_LOG_DIR
_ERROR_INTRA_PATH=$_MAIN_DIR/$_ERROR_DIR/$_FDMEE_ERROR_DIR

_LOGFILE=$_INTRA_PATH/$_DATETIMESTAMP_fdmee_act_load.log

And this this would be with your suggestion:

#!/bin/bash
#:: ------------------------------------------------------------------------
#:: -- SCRIPT NAME: fdmee_act_load.sh
#:: -- 
#:: -- DESCRIPTION: This script executes the FDMEE runbatch utility
#::                 To copy actuals to target intersections 
#:: -- 
#:: -- PARAMETERS:  Call setenv.cmd to get environment variables to determine 
#::                 login info, database, application, etc.
#:: --        
#::  Author:        
#::  Date:            04/02/16
#:: ------------------------------------------------------------------------
source /u01/hyp_app/scripts/setenv.sh

#:: SET LOG & ERROR FILES

_INTRA_PATH=$_MAIN_DIR/$_LOG_DIR/$_FDMEE_LOG_DIR
_ERROR_INTRA_PATH=$_MAIN_DIR/$_ERROR_DIR/$_FDMEE_ERROR_DIR

_LOGFILE=$_INTRA_PATH/$_DATETIMESTAMP_${0##*/}.log

So ${0##*/} = fdmee_act_load   ?


Last edited by RudiC; 04-04-2016 at 03:43 AM.. Reason: Added code tags.
# 4  
Old 04-03-2016
Please use the code tags.

If the script name is fdmee_act_load.sh as it is mentioned in your comment, then no. The value of "${0##*/}" would be fdmee_act_load.sh. In order to strip the ".sh", you would need to use a temporary variable, for example:
Code:
tmp="${0##*/}"
## Now ${tmp%%.sh*} equals fdmee_act_load providing the script name is fdmee_act_load.sh*

This User Gave Thanks to pilnet101 For This Post:
# 5  
Old 04-03-2016
Pilnet -

Okay great thank you! So my script would then look like the following:

Code:
#!/bin/bash
#:: ------------------------------------------------------------------------
#:: -- SCRIPT NAME: fdmee_act_load.sh
#:: -- 
#:: -- DESCRIPTION: This script executes the FDMEE runbatch utility
#::                 To copy actuals to target intersections 
#:: -- 
#:: -- PARAMETERS:  Call setenv.cmd to get environment variables to determine 
#::                 login info, database, application, etc.
#:: --        
#::  Author:        Chris Takacs (Peloton Group)
#::  Date:            04/02/16
#:: ------------------------------------------------------------------------
source /u01/hyp_app/scripts/setenv.sh

#:: SET FILE NAME
_FN=${0##*/}

#:: SET LOG & ERROR FILES

_INTRA_PATH=$_MAIN_DIR/$_LOG_DIR/$_FDMEE_LOG_DIR
_ERROR_INTRA_PATH=$_MAIN_DIR/$_ERROR_DIR/$_FDMEE_ERROR_DIR

_LOGFILE=$_INTRA_PATH/$_DATETIMESTAMP_${_FN%%.sh*}.log

Is that correct?

Last edited by RudiC; 04-04-2016 at 04:23 AM.. Reason: Code tags (again)
# 6  
Old 04-03-2016
Yes exactly that should work as you expect.

If you don't want to set the file name/use the temp variable, you can use the basename command which you mentioned in the original post. It will invoke a subshell but it will run on one line. So it would look like the below:
Code:
_LOGFILE=$_INTRA_PATH/$_DATETIMESTAMP_$(basename ${_0%%.sh*}).log

Simply comes down to preference, both methods will work.

Also just to let you know, a single period '.' is a synonym for the 'source' built-in, both do the same thing, in case you want to use that as well - again down to preference. I would also recommend enclosing your variables in curly braces, it is good practice and looks a lot cleaner Smilie
This User Gave Thanks to pilnet101 For This Post:
# 7  
Old 04-03-2016
Pilnet -

Very much appreciated - thank you very much!

Also, would this version work too? Or am I still missing something?

Code:
#!/bin/bash
#:: ------------------------------------------------------------------------
#:: -- SCRIPT NAME: fdmee_act_load.sh
#:: -- 
#:: -- DESCRIPTION: This script executes the FDMEE runbatch utility
#::                 To copy actuals to target intersections 
#:: -- 
#:: -- PARAMETERS:  Call setenv.cmd to get environment variables to determine 
#::                 login info, database, application, etc.
#:: --        
#::  Author:        Chris Takacs (Peloton Group)
#::  Date:            04/02/16
#:: ------------------------------------------------------------------------
source /u01/hyp_app/scripts/setenv.sh

#:: SET FILE NAME

_SCRIPTNAME="fdmee_act_load.sh"
echo "SCRIPT NAME: ${_SCRIPTNAME%.*}"

#:: SET LOG & ERROR FILES

_INTRA_PATH=$_MAIN_DIR/$_LOG_DIR/$_FDMEE_LOG_DIR
_ERROR_INTRA_PATH=$_MAIN_DIR/$_ERROR_DIR/$_FDMEE_ERROR_DIR

_LOGFILE=$_INTRA_PATH/$_DATETIMESTAMP_${_SCRIPTNAME%.*}.log


Moderator's Comments:
Mod Comment Please get accustomed to using code tags as required by forum rules which you accepted when you registered!

Last edited by RudiC; 04-04-2016 at 04:28 AM.. Reason: Added code tags (again)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

3. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

4. Programming

How to redirect the output of a shell script to a file?

hi, i have a html form which call a perl program, this perl program calls a shell script. <html> <head> <title>demo</title> </head> <body> <form name="frm1" action="/cgi-bin/perl_script.pl" method="post"> <input type="text" name="fname"> ... (1 Reply)
Discussion started by: Little
1 Replies

5. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

6. Shell Programming and Scripting

Calling a function in cpp file inside shell script

Hi I need to call a function written in a cpp file with arguments inside the shell script..Can anyone help me how to do this:( (1 Reply)
Discussion started by: rkrish
1 Replies

7. Shell Programming and Scripting

Call shell script function from awk script

hi everyone i am trying to do this bash> cat abc.sh deepak() { echo Deepak } deepak bash>./abc.sh Deepak so it is giving me write simply i created a func and it worked now i modified it like this way bash> cat abc.sh (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

8. Shell Programming and Scripting

getting garbage in the output file of shell script

Hi Everyone, The problem is that I am getting messages other than the script in the current log file. Ideally the script should contain only the messages that are redirected to the log file. How to remove these unwanted data from the log file. Please help if you have any idea how to remove the... (0 Replies)
Discussion started by: vsachan
0 Replies

9. Shell Programming and Scripting

Execution Output of a shell script into a file.

Hi Experts, I have a script called test.sh. I am trying to execute it with sh -x test.sh. Where i can find sequence of steps executed one by one. Now i want to these executions to be captured in a file. i.e sh -x test.sh > output.txt the above one is notworking. can anyone help me... (6 Replies)
Discussion started by: naree
6 Replies

10. Shell Programming and Scripting

extracting function headers in a c/c++ file using shell script

Hi, Is there any way to extract function headers from c and c++ files using a shell script? I tried to do it by reading the C/C++ file line by line and if a line matches a particular pattern (pattern of function header) i extracted it otherwise moved to next line. The problem here is, some... (3 Replies)
Discussion started by: priyadarshini
3 Replies
Login or Register to Ask a Question