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):
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
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.
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:
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:
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:
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
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)
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)
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)
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)
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)
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)
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)
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)
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)